Title: Module:Shortcut - freem URL Source: https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut Markdown Content: _Documentation for this module may be created at [Module:Shortcut/doc](https://freemwiki.com/index.php?title=Module:Shortcut/doc&action=edit&redlink=1 "Module:Shortcut/doc (page does not exist)")_ [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-1)-- This module implements {{shortcut}}.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-3)-- Set constants[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-4)local CONFIG_MODULE = 'Module:Shortcut/config'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-5)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-6)-- Load required modules[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-7)local checkType = require('libraryUtil').checkType[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-8)local yesno = require('Module:Yesno')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-9)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-10)local p = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-11)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-12)local function message(msg, ...)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-13)return mw.message.newRawMessage(msg, ...):plain()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-14)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-15)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-16)local function makeCategoryLink(cat)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-17)return string.format('[[%s:%s]]', mw.site.namespaces[14].name, cat)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-18)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-19)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-20)function p._main(shortcuts, options, frame, cfg)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-21)checkType('_main', 1, shortcuts, 'table')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-22)checkType('_main', 2, options, 'table', true)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-23)options = options or {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-24)frame = frame or mw.getCurrentFrame()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-25)cfg = cfg or mw.loadData(CONFIG_MODULE)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-26)local templateMode = options.template and yesno(options.template)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-27)local redirectMode = options.redirect and yesno(options.redirect)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-28)local isCategorized = not options.category or yesno(options.category) ~= false[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-29)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-30)-- Validate shortcuts[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-31)for i, shortcut in ipairs(shortcuts) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-32)if type(shortcut) ~= 'string' or #shortcut < 1 then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-33)error(message(cfg['invalid-shortcut-error'], i), 2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-34)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-35)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-36)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-37)-- Make the list items. These are the shortcuts plus any extra lines such[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-38)-- as options.msg.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-39)local listItems = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-40)for i, shortcut in ipairs(shortcuts) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-41)local templatePath, prefix[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-42)if templateMode then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-43)-- Namespace detection[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-44)local titleObj = mw.title.new(shortcut, 10)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-45)if titleObj.namespace == 10 then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-46)templatePath = titleObj.fullText[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-47)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-48)templatePath = shortcut[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-49)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-50)prefix = options['pre' .. i] or options.pre or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-51)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-52)if options.target and yesno(options.target) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-53)listItems[i] = templateMode[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-54)and string.format("{{%s[[%s|%s]]}}", prefix, templatePath, shortcut)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-55)or string.format("[[%s]]", shortcut)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-56)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-57)listItems[i] = frame:expandTemplate{[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-58)title = 'No redirect',[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-59)args = templateMode and {templatePath, shortcut} or {shortcut, shortcut}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-60)}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-61)if templateMode then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-62)listItems[i] = string.format("{{%s%s}}", prefix, listItems[i])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-63)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-64)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-65)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-66)table.insert(listItems, options.msg)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-67)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-68)-- Return an error if we have nothing to display[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-69)if #listItems < 1 then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-70)local msg = cfg['no-content-error'][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-71)msg = string.format('%s', msg)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-72)if isCategorized and cfg['no-content-error-category'] then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-73)msg = msg .. makeCategoryLink(cfg['no-content-error-category'])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-74)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-75)return msg[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-76)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-77)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-78)local root = mw.html.create()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-79)root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Shortcut/styles.css'} })[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-80)-- Anchors[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-81)local anchorDiv = root[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-82):tag('div')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-83):addClass('module-shortcutanchordiv')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-84)for i, shortcut in ipairs(shortcuts) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-85)local anchor = mw.uri.anchorEncode(shortcut)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-86)anchorDiv:tag('span'):attr('id', anchor)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-87)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-88)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-89)-- Shortcut heading[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-90)local shortcutHeading[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-91)do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-92)local nShortcuts = #shortcuts[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-93)if nShortcuts > 0 then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-94)local headingMsg = options['shortcut-heading'] or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-95)redirectMode and cfg['redirect-heading'] or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-96)cfg['shortcut-heading'][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-97)shortcutHeading = message(headingMsg, nShortcuts)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-98)shortcutHeading = frame:preprocess(shortcutHeading)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-99)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-100)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-101)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-102)-- Shortcut box[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-103)local shortcutList = root[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-104):tag('div')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-105):addClass('module-shortcutboxplain noprint')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-106):attr('role', 'note')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-107)if options.float and options.float:lower() == 'left' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-108)shortcutList:addClass('module-shortcutboxleft')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-109)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-110)if options.clear and options.clear ~= '' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-111)shortcutList:css('clear', options.clear)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-112)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-113)if shortcutHeading then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-114)shortcutList[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-115):tag('div')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-116):addClass('module-shortcutlist')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-117):wikitext(shortcutHeading)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-118)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-119)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-120)local ubl = require('Module:List').unbulleted(listItems)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-121)shortcutList:wikitext(ubl)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-122)return tostring(root)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-123)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-124)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-125)function p.main(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-126)local args = require('Module:Arguments').getArgs(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-127)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-128)-- Separate shortcuts from options[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-129)local shortcuts, options = {}, {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-130)for k, v in pairs(args) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-131)if type(k) == 'number' then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-132)shortcuts[k] = v[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-133)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-134)options[k] = v[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-135)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-136)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-137)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-138)-- Compress the shortcut array, which may contain nils.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-139)local function compressArray(t)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-140)local nums, ret = {}, {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-141)for k in pairs(t) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-142)nums[#nums + 1] = k[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-143)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-144)table.sort(nums)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-145)for i, num in ipairs(nums) do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-146)ret[i] = t[num][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-147)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-148)return ret[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-149)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-150)shortcuts = compressArray(shortcuts)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-151)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-152)return p._main(shortcuts, options, frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-153)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-154)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AShortcut#L-155)return p