Module:Shortcut: Difference between revisions

m
1 revision imported
Enwikipedia>Mr. Stradivarius
(move messages to the config module and make categories optional)
m (1 revision imported)
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(19 intermediate revisions by 12 users not shown)
Line 24:
frame = frame or mw.getCurrentFrame()
cfg = cfg or mw.loadData(CONFIG_MODULE)
local isCategorizedtemplateMode = options.template and yesno(options.categorytemplate) ~= false
local redirectMode = options.redirect and yesno(options.redirect)
local isCategorized = not options.category or yesno(options.category) ~= false
 
-- Validate shortcuts
Line 37 ⟶ 39:
local listItems = {}
for i, shortcut in ipairs(shortcuts) do
local templatePath, prefix
listItems[i] = string.format('[[%s]]', shortcut)
if templateMode then
-- Namespace detection
local titletitleObj = mw.title.new(shortcuts[1]shortcut, 10)
if titleObj.namespace == 10 then
templatePath = titleObj.fullText
else
templatePath = shortcut
end
prefix = options['pre' .. i] or options.pre or ''
end
if options.target and yesno(options.target) then
listItems[i] = templateMode
and string.format("{{%s[[%s|%s]]}}", prefix, templatePath, shortcut)
listItems[i] = or string.format('"[[%s]]'", shortcut)
else
listItems[i] = frame:expandTemplate{
title = 'No redirect',
args = templateMode and {templatePath, shortcut} or {shortcut, shortcut}
}
if templateMode then
listItems[i] = string.format("{{%s%s}}", prefix, listItems[i])
end
end
end
table.insert(listItems, options.msg)
Line 52 ⟶ 77:
 
local root = mw.html.create()
root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Shortcut/styles.css'} })
 
-- Anchors
local anchorDiv = root
:tag('div')
:addClass('module-shortcutanchordiv')
:css('position', 'relative')
:css('top', '-3em')
for i, shortcut in ipairs(shortcuts) do
local anchor = mw.uri.anchorEncode(shortcut)
anchorDiv:tag('span'):attr('id', anchor)
end
 
root:newline() -- To match the old [[Template:Shortcut]]
 
-- Shortcut heading
Line 70 ⟶ 92:
local nShortcuts = #shortcuts
if nShortcuts > 0 then
shortcutHeadinglocal headingMsg = message(cfgoptions['shortcut-heading'], nShortcuts)or
redirectMode and cfg['redirect-heading'] or
cfg['shortcut-heading']
shortcutHeading = shortcutHeadingmessage(headingMsg, .. '\n'nShortcuts)
shortcutHeading = frame:preprocess(shortcutHeading)
shortcutHeading = shortcutHeading .. '\n'
end
end
Line 78 ⟶ 102:
-- Shortcut box
local shortcutList = root
:tag('tablediv')
:addClass('shortcutboxmodule-shortcutboxplain noprint')
:cssattr('floatrole', 'rightnote')
if options.float and options.float:lower() == 'left' then
:css('border', '1px solid #aaa')
shortcutList:addClass('module-shortcutboxleft')
:css('background', '#fff')
:css('margin', '.3em .3em .3em 1em')
:css('padding', '3px')
:css('text-align', 'center')
:tag('tr')
:tag('th')
:addClass('plainlist')
:css('border', 'none')
:css('background', 'transparent')
:tag('small')
:wikitext(shortcutHeading)
:tag('ul')
for i, item in ipairs(listItems) do
shortcutList:tag('li'):wikitext(item)
end
if options.clear and options.clear ~= '' then
 
shortcutList:css('clear', options.clear)
-- Output an error category if the first shortcut doesn't exist
if isCategorized
and shortcuts[1]
and cfg['first-parameter-error-category']
then
local title = mw.title.new(shortcuts[1])
if not title or not title.exists then
root:wikitext(makeCategoryLink(cfg['first-parameter-error-category']))
end
end
if shortcutHeading then
 
shortcutList
:tag('trdiv')
:addClass('plainlistmodule-shortcutlist')
:wikitext(shortcutHeading)
end
local ubl = require('Module:List').unbulleted(listItems)
shortcutList:tag('li'):wikitext(itemubl)
return tostring(root)
end
 
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {)
wrappers = 'Template:Shortcut'
})
 
-- Separate shortcuts from options