Editing Module:Template invocation

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. Read the Privacy Policy to learn what information we collect about you and how we use it.

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 publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
-- This module provides functions for making MediaWiki template invocations.
-- This module makes a template invocation from a template name and a table
-- of arguments.


local checkType = require('libraryUtil').checkType
local checkType = require('libraryUtil').checkType
Line 12: Line 13:
--                object has been passed in, and uses that to find a
--                object has been passed in, and uses that to find a
--                template name as it is used in template invocations.
--                template name as it is used in template invocations.
--  Parameters:  title - full page name or mw.title object for the
--  Parameters:  title - full page name or mw.title obejct for the
--                template (string or mw.title object)
--                template (string or mw.title object)
--      Returns:  String
--      Returns:  String
Line 41: Line 42:
--                name and the arguments given. Note that it isn't
--                name and the arguments given. Note that it isn't
--                perfect: we have no way of knowing what whitespace was
--                perfect: we have no way of knowing what whitespace was
--                in the original invocation, the named parameters will be
--                in the original invocation, the order of the parameters
--                alphabetically sorted, and any parameters with duplicate keys
--                may be changed, and any parameters with duplicate keys
--                will be removed.
--                will be removed.
--  Parameters:  name - the template name, formatted as it will appear
--  Parameters:  name - the template name, formatted as it will appear
Line 93: Line 94:
ret[#ret + 1] = name
ret[#ret + 1] = name
for k, v in ipairs(invArgs) do
for k, v in ipairs(invArgs) do
if type(v) == 'string' and v:find('=', 1, true) then
if v:find('=', 1, true) then
-- Likely something like 1=foo=bar which needs to be displayed as a named arg.
-- Likely something like 1=foo=bar, we need to do it as a named arg
else
break
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = v
invArgs[k] = nil -- Erase the key so that we don't add the value twice
end
end
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = v
invArgs[k] = nil -- Erase the key so that we don't add the value twice
end
end
local keys = {} -- sort parameter list; better than arbitrary order
for k, v in pairs(invArgs) do
for k, _ in pairs(invArgs) do
keys[#keys + 1] = k
end
table.sort(keys, function (a, b)
-- Sort with keys of type number first, then string.
if type(a) == type(b) then
return a < b
elseif type(a) == 'number' then
return true
end
end)
for _, v in ipairs(keys) do -- Add named args based on sorted parameter list
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = tostring(v)
ret[#ret + 1] = k
ret[#ret + 1] = seps.equals
ret[#ret + 1] = seps.equals
ret[#ret + 1] = invArgs[v]
ret[#ret + 1] = v
end
end
ret[#ret + 1] = seps.closeb
ret[#ret + 1] = seps.closeb
Please note that all contributions to Nonbinary Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see Nonbinary Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, 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. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)

Template used on this page: