Module:Shortcut and Template:Markup: Difference between pages

From Frontierpedia, the Microsoft Agent encyclopedia
(Difference between pages)
en>Edokter
(Change table to div, per talk)
 
en>Primefac
m (Protected "Template:Markup": semi-protecting highly-visible templates in response to recent template-space vandalism ([Edit=Require autoconfirmed or confirmed access] (indefinite) [Move=Require autoconfirmed or confirmed access] (indefi...)
 
Line 1: Line 1:
-- This module implements {{shortcut}}.
<includeonly>{| style="table-layout:fixed; width:{{{width|100%}}}; margin-top:0;margin-left:{{{margin-left|{{{margin|0}}}}}}; border-width:medium; padding:0; {{{style|}}}"
 
{{#if:{{{title|}}} |{{!}}+ '''{{{title}}}'''}}
-- Set constants
{{#if:{{{noheaders|}}}{{{notitle|}}} |
local CONFIG_MODULE = 'Module:Shortcut/config'
|!  scope="col" style="width:50%;{{{colheaderstyle|}}}{{{col1headerstyle|}}}"{{!}} {{{col1|{{{t1|Markup}}}}}} {{!!}}<!--
 
  -->scope="col" style="width:50%;{{{colheaderstyle|}}}{{{col2headerstyle|}}}"{{!}} {{{col2|{{{t2|Renders as}}}}}}
-- Load required modules
}}<!--
local checkType = require('libraryUtil').checkType
-->{{#if:{{{1|}}}  |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{1|}}}  |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{2|}}}  }} }}<!--
local yesno = require('Module:Yesno')
-->{{#if:{{{3|}}}  |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{3|}}}  |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{4|}}}  }} }}<!--
 
-->{{#if:{{{5|}}}  |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{5|}}}  |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{6|}}}  }} }}<!--
local p = {}
-->{{#if:{{{7|}}}  |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{7|}}}  |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{8|}}}  }} }}<!--
 
-->{{#if:{{{9|}}}  |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{9|}}}  |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{10|}}} }} }}<!--
local function message(msg, ...)
-->{{#if:{{{11|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{11|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{12|}}} }} }}<!--
return mw.message.newRawMessage(msg, ...):plain()
-->{{#if:{{{13|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{13|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{14|}}} }} }}<!--
end
-->{{#if:{{{15|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{15|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{16|}}} }} }}<!--
 
-->{{#if:{{{17|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{17|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{18|}}} }} }}<!--
local function makeCategoryLink(cat)
-->{{#if:{{{19|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{19|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{20|}}} }} }}<!--
return string.format('[[%s:%s]]', mw.site.namespaces[14].name, cat)
-->{{#if:{{{21|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{21|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{22|}}} }} }}<!--
end
-->{{#if:{{{23|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{23|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{24|}}} }} }}<!--
 
-->{{#if:{{{25|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{25|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{26|}}} }} }}<!--
function p._main(shortcuts, options, frame, cfg)
-->{{#if:{{{27|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{27|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{28|}}} }} }}<!--
checkType('_main', 1, shortcuts, 'table')
-->{{#if:{{{29|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{29|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{30|}}} }} }}<!--
checkType('_main', 2, options, 'table', true)
-->{{#if:{{{31|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{31|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{32|}}} }} }}<!--
options = options or {}
-->{{#if:{{{33|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{33|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{34|}}} }} }}<!--
frame = frame or mw.getCurrentFrame()
-->{{#if:{{{35|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{35|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{36|}}} }} }}<!--
cfg = cfg or mw.loadData(CONFIG_MODULE)
-->{{#if:{{{37|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{37|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{38|}}} }} }}<!--
local isCategorized = yesno(options.category) ~= false
-->{{#if:{{{39|}}} |{{Markup/row |c1style={{{col1style|{{{codestyle|{{{markupstyle|}}}}}}}}}|c1={{{39|}}} |c2style={{{col2style|{{{outputstyle|}}}}}}|c2={{{40|}}} }} }}
 
|-
-- Validate shortcuts
|style="width:50%; border-width:1px;border-style:solid none none none;border-color:#ddd; padding:5px; vertical-align:top;"|
for i, shortcut in ipairs(shortcuts) do
|style="width:50%; border-width:1px;border-style:solid none none none;border-color:#ddd; padding:5px; vertical-align:top;"|
if type(shortcut) ~= 'string' or #shortcut < 1 then
|}</includeonly><noinclude>
error(message(cfg['invalid-shortcut-error'], i), 2)
{{Documentation}}
end
</noinclude>
end
 
-- Make the list items. These are the shortcuts plus any extra lines such
-- as options.msg.
local listItems = {}
for i, shortcut in ipairs(shortcuts) do
listItems[i] = string.format('[[%s]]', shortcut)
end
table.insert(listItems, options.msg)
 
-- Return an error if we have nothing to display
if #listItems < 1 then
local msg = cfg['no-content-error']
msg = string.format('<strong class="error">%s</strong>', msg)
if isCategorized and cfg['no-content-error-category'] then
msg = msg .. makeCategoryLink(cfg['no-content-error-category'])
end
return msg
end
 
local root = mw.html.create()
 
-- Anchors
local anchorDiv = root
:tag('div')
: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
local shortcutHeading
do
local nShortcuts = #shortcuts
if nShortcuts > 0 then
shortcutHeading = message(cfg['shortcut-heading'], nShortcuts)
shortcutHeading = frame:preprocess(shortcutHeading)
shortcutHeading = shortcutHeading .. '\n'
end
end
 
-- Shortcut box
local shortcutList = root
: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
 
-- 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
 
return tostring(root)
end
 
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Shortcut'
})
 
-- Separate shortcuts from options
local shortcuts, options = {}, {}
for k, v in pairs(args) do
if type(k) == 'number' then
shortcuts[k] = v
else
options[k] = v
end
end
 
-- Compress the shortcut array, which may contain nils.
local function compressArray(t)
local nums, ret = {}, {}
for k in pairs(t) do
nums[#nums + 1] = k
end
table.sort(nums)
for i, num in ipairs(nums) do
ret[i] = t[num]
end
return ret
end
shortcuts = compressArray(shortcuts)
 
return p._main(shortcuts, options, frame)
end
 
return p

Revision as of 19:17, 10 January 2018