Module:Shortcut
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 26,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
Related pages |
---|
This module makes a box showing the shortcut links to a page.
Usage
From wikitext
From wikitext, this module should be called from a template, usually {{shortcut}}. Please see the template page for documentation. However, it can also be called using the syntax {{#invoke:shortcut|main|arguments}}
.
From Lua
To use this module from Lua, first load it.
local mShortcut = require('Module:Shortcut')
Then you can create shortcut boxes with the following syntax:
mShortcut._main(shortcuts, options, frame, cfg)
- shortcuts is an array of shortcut page names. (required)
- options is a table of options. The following keys are supported:
msg
- a message to leave after the list of shortcuts.category
- if set to false (or a value regarded as false by Module:Yesno, such as "no"), categories are suppressed.
- frame is a frame object. This is optional, and only intended to be used internally.
- cfg is a table of config values. This is optional, and is only intended for testing.
Technical details
This module has a configuration file at Module:Shortcut/config. It can be used to translate this module into different languages or to change details like category names.
-- This module implements {{shortcut}}.
local cfg = {}
--------------------------------------------------------------------------------
-- Configuration
--------------------------------------------------------------------------------
-- Name for the error category produced if the first shortcut is not an existing
-- page on the wiki.
cfg.errorCategory = 'Wikipedia shortcut box first parameter needs fixing'
-- The header text for the list of shortcuts.
-- cfg.shortcutHeaderSingular will be displayed if there is one shortcut, and
-- cfg.shortcutHeaderPlural will be displayed if there are multiple shortcuts.
cfg.shortcutHeaderSingular = '[[Wikipedia:Shortcut|Shortcut]]:'
cfg.shortcutHeaderPlural = '[[Wikipedia:Shortcut|Shortcuts]]:'
--------------------------------------------------------------------------------
-- Load external modules and define often-used functions
--------------------------------------------------------------------------------
-- Load external modules
local mArguments = require('Module:Arguments')
local mTableTools = require('Module:TableTools')
local mList = require('Module:List')
-- Define often-used functions
local anchorEncode = mw.uri.anchorEncode
local format = string.format
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------
local p = {}
function p.main(frame)
local args = mArguments.getArgs(frame)
return p._main(args)
end
function p._main(args)
local shortcuts = p.getShortcuts(args)
local nShortcuts = #shortcuts
if nShortcuts < 1 then
-- Don't output anything if {{shortcut}} was called with no arguments.
return ''
end
local anchors = p.makeAnchorList(shortcuts)
local shortcutList = p.makeShortcutList(shortcuts)
local errorCategories = p.getErrorCategories(shortcuts)
return p.export(anchors, nShortcuts, shortcutList, errorCategories)
end
function p.getShortcuts(args)
local shortcuts = mTableTools.compressSparseArray(args)
return shortcuts
end
function p.makeAnchorList(shortcuts)
local makeAnchor = p.makeAnchor
local anchors = {}
for i, shortcut in ipairs(shortcuts) do
anchors[#anchors + 1] = makeAnchor(shortcut)
end
return table.concat(anchors)
end
function p.makeAnchor(s)
s = anchorEncode(s)
local anchor = format('<span id="%s"></span>', s)
return anchor
end
function p.makeShortcutList(shortcuts)
local makeShortcutLink = p.makeShortcutLink
local listArgs = {}
for i, shortcut in ipairs(shortcuts) do
local link = makeShortcutLink(shortcut)
listArgs[#listArgs + 1] = link
end
return mList.makeList('bulleted', listArgs)
end
function p.makeShortcutLink(s)
return format('[[%s]]', s)
end
function p.getErrorCategories(shortcuts)
local shortcut1 = shortcuts[1]
local title = mw.title.new(shortcut1)
if not title or not title.exists then
local categoryNsName = mw.site.namespaces[14].name
return format('[[%s:%s]]', categoryNsName, cfg.errorCategory)
else
return nil
end
end
function p.export(anchors, nShortcuts, shortcutList, errorCategories)
local root = mw.html.create('')
root:tag('div')
:css{position = 'relative', top = '-3em'}
:wikitext(anchors)
:done()
:tag('table')
:addClass('shortcutbox noprint')
:css{
float = 'right',
border = '1px solid #aaa',
background = '#fff',
margin = '.3em .3em .3em 1em',
padding = '3px',
['text-align'] = 'center'
}
:tag('tr')
:tag('th')
:addClass('plainlist')
:css{border = 'none', background = 'transparent'}
:tag('small')
:wikitext(
nShortcuts <= 1
and cfg.shortcutHeaderSingular
or cfg.shortcutHeaderPlural
)
:newline()
:wikitext(shortcutList)
root:wikitext(errorCategories)
return tostring(root)
end
return p