Editing Module:Shortcut

From MINR.ORG WIKI

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
-- This module implements {{shortcut}}.
 
-- This module implements {{shortcut}}.
  
-- Set constants
+
local cfg = {}
local CONFIG_MODULE = 'Module:Shortcut/config'
 
  
-- Load required modules
+
--------------------------------------------------------------------------------
local checkType = require('libraryUtil').checkType
+
-- Configuration
local yesno = require('Module:Yesno')
+
--------------------------------------------------------------------------------
  
local p = {}
+
-- 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
 +
--------------------------------------------------------------------------------
  
local function message(msg, ...)
+
-- Load external modules
return mw.message.newRawMessage(msg, ...):plain()
+
local mArguments = require('Module:Arguments')
end
+
local mTableTools = require('Module:TableTools')
 +
local mList = require('Module:List')
  
local function makeCategoryLink(cat)
+
-- Define often-used functions
return string.format('[[%s:%s]]', mw.site.namespaces[14].name, cat)
+
local anchorEncode = mw.uri.anchorEncode
end
+
local format = string.format
  
function p._main(shortcuts, options, frame, cfg)
+
--------------------------------------------------------------------------------
checkType('_main', 1, shortcuts, 'table')
+
-- Main functions
checkType('_main', 2, options, 'table', true)
+
--------------------------------------------------------------------------------
options = options or {}
 
frame = frame or mw.getCurrentFrame()
 
cfg = cfg or mw.loadData(CONFIG_MODULE)
 
local isCategorized = yesno(options.category) ~= false
 
  
-- Validate shortcuts
+
local p = {}
for i, shortcut in ipairs(shortcuts) do
 
if type(shortcut) ~= 'string' or #shortcut < 1 then
 
error(message(cfg['invalid-shortcut-error'], i), 2)
 
end
 
end
 
  
-- Make the list items. These are the shortcuts plus any extra lines such
+
function p.main(frame)
-- as options.msg.
+
local args = mArguments.getArgs(frame)
local listItems = {}
+
return p._main(args)
for i, shortcut in ipairs(shortcuts) do
+
end
listItems[i] = frame:expandTemplate{
 
title = 'No redirect',
 
args = {shortcut}
 
}
 
end
 
table.insert(listItems, options.msg)
 
  
-- Return an error if we have nothing to display
+
function p._main(args)
if #listItems < 1 then
+
local shortcuts = p.getShortcuts(args)
local msg = cfg['no-content-error']
+
local nShortcuts = #shortcuts
msg = string.format('<strong class="error">%s</strong>', msg)
+
if nShortcuts < 1 then
if isCategorized and cfg['no-content-error-category'] then
+
-- Don't output anything if {{shortcut}} was called with no arguments.
msg = msg .. makeCategoryLink(cfg['no-content-error-category'])
+
return ''
end
 
return msg
 
 
end
 
end
 +
local anchors = p.makeAnchorList(shortcuts)
 +
local shortcutList = p.makeShortcutList(shortcuts)
 +
local errorCategories = p.getErrorCategories(shortcuts)
 +
return p.export(anchors, nShortcuts, shortcutList, errorCategories)
 +
end
  
local root = mw.html.create()
+
function p.getShortcuts(args)
 +
local shortcuts = mTableTools.compressSparseArray(args)
 +
return shortcuts
 +
end
  
-- Anchors
+
function p.makeAnchorList(shortcuts)
local anchorDiv = root
+
local makeAnchor = p.makeAnchor
:tag('div')
+
local anchors = {}
:css('position', 'relative')
 
:css('top', '-3em')
 
 
for i, shortcut in ipairs(shortcuts) do
 
for i, shortcut in ipairs(shortcuts) do
local anchor = mw.uri.anchorEncode(shortcut)
+
anchors[#anchors + 1] = makeAnchor(shortcut)
anchorDiv:tag('span'):attr('id', anchor)
 
 
end
 
end
 +
return table.concat(anchors)
 +
end
  
root:newline() -- To match the old [[Template:Shortcut]]
+
function p.makeAnchor(s)
 +
s = anchorEncode(s)
 +
local anchor = format('<span id="%s"></span>', s)
 +
return anchor
 +
end
  
-- Shortcut heading
+
function p.makeShortcutList(shortcuts)
local shortcutHeading
+
local makeShortcutLink = p.makeShortcutLink
do
+
local listArgs = {}
local nShortcuts = #shortcuts
+
for i, shortcut in ipairs(shortcuts) do
if nShortcuts > 0 then
+
local link = makeShortcutLink(shortcut)
shortcutHeading = message(cfg['shortcut-heading'], nShortcuts)
+
listArgs[#listArgs + 1] = link
shortcutHeading = frame:preprocess(shortcutHeading)
 
shortcutHeading = shortcutHeading .. '\n'
 
end
 
 
end
 
end
 +
return mList.makeList('bulleted', listArgs)
 +
end
  
-- Shortcut box
+
function p.makeShortcutLink(s)
local shortcutList = root
+
return format('[[%s]]', s)
:tag('div')
 
:addClass('shortcutbox plainlist noprint')
 
:attr('role', 'note')
 
:css('float', 'right')
 
:css('border', '1px solid #aaa')
 
:css('background', '#fff')
 
:css('margin', '.3em .3em .3em 1em')
 
:css('padding', '.4em .6em')
 
:css('text-align', 'center')
 
:css('font-size', 'smaller')
 
:css('line-height', '2em')
 
:css('font-weight', 'bold')
 
:wikitext(shortcutHeading)
 
:tag('ul')
 
for i, item in ipairs(listItems) do
 
shortcutList:tag('li'):wikitext(item)
 
end
 
 
 
return tostring(root)
 
 
end
 
end
  
function p.main(frame)
+
function p.getErrorCategories(shortcuts)
local args = require('Module:Arguments').getArgs(frame, {
+
local shortcut1 = shortcuts[1]
wrappers = 'Template:Shortcut'
+
local title = mw.title.new(shortcut1)
})
+
if not title or not title.exists then
 
+
local categoryNsName = mw.site.namespaces[14].name
-- Separate shortcuts from options
+
return format('[[%s:%s]]', categoryNsName, cfg.errorCategory)
local shortcuts, options = {}, {}
+
else
for k, v in pairs(args) do
+
return nil
if type(k) == 'number' then
 
shortcuts[k] = v
 
else
 
options[k] = v
 
end
 
 
end
 
end
 +
end
  
-- Compress the shortcut array, which may contain nils.
+
function p.export(anchors, nShortcuts, shortcutList, errorCategories)
local function compressArray(t)
+
local root = mw.html.create('')
local nums, ret = {}, {}
+
root:tag('div')
for k in pairs(t) do
+
:css{position = 'relative', top = '-3em'}
nums[#nums + 1] = k
+
:wikitext(anchors)
end
+
:done()
table.sort(nums)
+
:tag('table')
for i, num in ipairs(nums) do
+
:addClass('shortcutbox noprint')
ret[i] = t[num]
+
:css{
end
+
float = 'right',
return ret
+
border = '1px solid #aaa',
end
+
background = '#fff',
shortcuts = compressArray(shortcuts)
+
margin = '.3em .3em .3em 1em',
 
+
padding = '3px',
return p._main(shortcuts, options, frame)
+
['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
 
end
  
 
return p
 
return p

Please note that all contributions to MINR.ORG WIKI may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see MINR.ORG WIKI:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)