Module:Parameter names example/sandbox: Difference between revisions
Appearance
Content deleted Content added
local function plain(s), extra conditional (function p._main) |
Restored last version from 29 August |
||
Line 13: | Line 13: | ||
end |
end |
||
function p._main(origArgs, frame) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- Separate the args and the options. |
|||
local args, options = {}, {} |
|||
for k, v in pairs(origArgs) do |
|||
⚫ | |||
args[k] = v |
|||
⚫ | |||
options[k:gsub('^_', '')] = v |
|||
⚫ | |||
⚫ | |||
function p._main(args, frame) |
|||
-- Find how we want to format the arguments to the template. |
-- Find how we want to format the arguments to the template. |
||
local formatFunc |
local formatFunc |
||
if |
if options.display == 'italics' or options.display == 'italic' then |
||
formatFunc = italicize |
formatFunc = italicize |
||
else |
else |
||
⚫ | |||
if args._display == 'plain' then |
|||
formatFunc = plain |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
-- Build the table of template arguments |
-- Build the table of template arguments, and record whether any named |
||
-- arguments are being used. |
|||
local targs = {} |
local targs = {} |
||
local hasNamedArgs = false |
|||
for k, v in pairs(args) do |
for k, v in pairs(args) do |
||
if type(k) == 'number' then |
if type(k) == 'number' then |
||
targs[v] = formatFunc(v) |
targs[v] = formatFunc(v) |
||
⚫ | |||
⚫ | |||
hasNamedArgs = true |
|||
targs[k] = v |
targs[k] = v |
||
end |
end |
||
Line 41: | Line 48: | ||
-- Find the template name. |
-- Find the template name. |
||
local |
local templateName |
||
if |
if options.template then |
||
templateName = options.template |
|||
template = args._template |
|||
else |
else |
||
-- Find the title object for the template. |
|||
local templateTitle |
|||
local currentTitle = mw.title.getCurrentTitle() |
local currentTitle = mw.title.getCurrentTitle() |
||
if currentTitle.prefixedText:find('/sandbox$') then |
if currentTitle.prefixedText:find('/sandbox$') then |
||
templateTitle = currentTitle |
|||
else |
else |
||
templateTitle = currentTitle.basePageTitle |
|||
end |
end |
||
-- Format the template name according to the namespace we are in. |
|||
if templateTitle.namespace == 10 then -- NS_TEMPLATE |
|||
templateName = templateTitle.text |
|||
elseif templateTitle.namespace == 0 then -- NS_MAIN |
|||
templateName = ':' .. templateTitle.text |
|||
else |
|||
templateName = templateTitle.prefixedText |
|||
end |
|||
end |
|||
local ret = '' |
|||
-- Build the template invocation. |
|||
if options.code then |
|||
local code = { templateName } |
|||
if hasNamedArgs then |
|||
-- Put the args into alphabetical order. |
|||
local keys = {} |
|||
for k in pairs(targs) do |
|||
keys[#keys + 1] = k |
|||
end |
|||
table.sort(keys, function (a, b) |
|||
local typeA = type(a) |
|||
local typeB = type(b) |
|||
if typeA ~= typeB then |
|||
return typeA == 'number' -- numbers go first |
|||
else |
|||
⚫ | |||
end |
|||
end) |
|||
-- Assemble the invocation. |
|||
for _, k in ipairs(keys) do |
|||
code[#code + 1] = ' ' .. tostring(k) .. ' = ' .. targs[k] |
|||
end |
|||
else |
|||
-- Args are only numbers, but it is possible ipairs will not work |
|||
-- if any of them use the "1 = foo" syntax, so sort them using |
|||
-- pairs. |
|||
local keys = {} |
|||
for k in pairs(args) do |
|||
keys[#keys + 1] = k |
|||
end |
|||
table.sort(keys) |
|||
-- Assemble the invocation. |
|||
for _, k in ipairs(keys) do |
|||
code[#code + 1] = ' ' .. args[k] .. ' = ' .. targs[args[k]] |
|||
end |
|||
end |
|||
code = '{{' .. table.concat(code, '\n|') .. '\n}}' |
|||
code = '<pre style="white-space: pre-wrap;">\n' .. code .. '\n</pre>\n\n' |
|||
ret = ret .. frame:preprocess(code) |
|||
end |
end |
||
-- Call the template with the arguments. |
-- Call the template with the arguments. |
||
⚫ | |||
local success, result = pcall( |
local success, result = pcall( |
||
frame.expandTemplate, |
frame.expandTemplate, |
||
frame, |
frame, |
||
{title = |
{title = templateName, args = targs} |
||
) |
) |
||
if success then |
if success then |
||
ret = ret .. result |
|||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
end |
end |
||
Revision as of 15:39, 14 September 2014
![]() | This is the module sandbox page for Module:Parameter names example (diff). |
{{{above}}} | |
---|---|
{{{subheader}}} | |
{{{image}}} {{{caption}}} | |
{{{header1}}} | |
{{{label2}}} | {{{data2}}} |
{{{label3}}} | {{{data3}}} |
{{{below}}} |
This module implements {{Parameter names example}} (also known as {{Generic template demo}}). It creates a template demonstration such as that shown opposite. Please see the template page for full documentation.
-- This module implements {{parameter names example}}.
local p = {}
local function makeParam(s)
local lb = '{'
local rb = '}'
return lb:rep(3) .. s .. rb:rep(3)
end
local function italicize(s)
return "''" .. s .. "''"
end
function p._main(origArgs, frame)
frame = frame or mw.getCurrentFrame()
-- Separate the args and the options.
local args, options = {}, {}
for k, v in pairs(origArgs) do
if type(k) == 'number' or not k:find('^_') then
args[k] = v
else
options[k:gsub('^_', '')] = v
end
end
-- Find how we want to format the arguments to the template.
local formatFunc
if options.display == 'italics' or options.display == 'italic' then
formatFunc = italicize
else
formatFunc = makeParam
end
-- Build the table of template arguments, and record whether any named
-- arguments are being used.
local targs = {}
local hasNamedArgs = false
for k, v in pairs(args) do
if type(k) == 'number' then
targs[v] = formatFunc(v)
else
hasNamedArgs = true
targs[k] = v
end
end
-- Find the template name.
local templateName
if options.template then
templateName = options.template
else
-- Find the title object for the template.
local templateTitle
local currentTitle = mw.title.getCurrentTitle()
if currentTitle.prefixedText:find('/sandbox$') then
templateTitle = currentTitle
else
templateTitle = currentTitle.basePageTitle
end
-- Format the template name according to the namespace we are in.
if templateTitle.namespace == 10 then -- NS_TEMPLATE
templateName = templateTitle.text
elseif templateTitle.namespace == 0 then -- NS_MAIN
templateName = ':' .. templateTitle.text
else
templateName = templateTitle.prefixedText
end
end
local ret = ''
-- Build the template invocation.
if options.code then
local code = { templateName }
if hasNamedArgs then
-- Put the args into alphabetical order.
local keys = {}
for k in pairs(targs) do
keys[#keys + 1] = k
end
table.sort(keys, function (a, b)
local typeA = type(a)
local typeB = type(b)
if typeA ~= typeB then
return typeA == 'number' -- numbers go first
else
return a < b
end
end)
-- Assemble the invocation.
for _, k in ipairs(keys) do
code[#code + 1] = ' ' .. tostring(k) .. ' = ' .. targs[k]
end
else
-- Args are only numbers, but it is possible ipairs will not work
-- if any of them use the "1 = foo" syntax, so sort them using
-- pairs.
local keys = {}
for k in pairs(args) do
keys[#keys + 1] = k
end
table.sort(keys)
-- Assemble the invocation.
for _, k in ipairs(keys) do
code[#code + 1] = ' ' .. args[k] .. ' = ' .. targs[args[k]]
end
end
code = '{{' .. table.concat(code, '\n|') .. '\n}}'
code = '<pre style="white-space: pre-wrap;">\n' .. code .. '\n</pre>\n\n'
ret = ret .. frame:preprocess(code)
end
-- Call the template with the arguments.
local success, result = pcall(
frame.expandTemplate,
frame,
{title = templateName, args = targs}
)
if success then
ret = ret .. result
end
return ret
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Parameter names example'
})
return p._main(args, frame)
end
return p