Module:Template invocation: Difference between revisions

m
12 revisions imported from wikipedia:Module:Template_invocation
(add a p.name function to find the template name, use checkType with p.invocation, and add some function description comments)
m (12 revisions imported from wikipedia:Module:Template_invocation)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
-- This module makes a template invocation from a template name and a table
-- This module provides functions for making MediaWiki template invocations.
-- of arguments.


local checkType = require('libraryUtil').checkType
local checkType = require('libraryUtil').checkType
Line 13: Line 12:
--                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 obejct for the
--  Parameters:  title - full page name or mw.title object for the
--                template (string or mw.title object)
--                template (string or mw.title object)
--      Returns:  String
--      Returns:  String
Line 42: Line 41:
--                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 order of the parameters
--                in the original invocation, the named parameters will be
--                may be changed, and any parameters with duplicate keys
--                alphabetically sorted, 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 60: Line 59:
checkType('invocation', 3, format, 'string', true)
checkType('invocation', 3, format, 'string', true)


-- Copy the invocation args and convert magic words.
-- Validate the args table and make a copy to work from. We need to
-- We need to make a copy of the table rather than just using the original,
-- make a copy of the table rather than just using the original, as
-- as some of the values may be erased when building the invocation.
-- some of the values may be erased when building the invocation.
local invArgs = {}
local invArgs = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
local typek = type(k)
local typev = type(v)
if typek ~= 'string' and typek ~= 'number'
or typev ~= 'string' and typev ~= 'number'
then
error("invalid arguments table in parameter #2 of " ..
"'invocation' (keys and values must be strings or numbers)", 2)
end
invArgs[k] = v
invArgs[k] = v
end
end
Line 86: Line 93:
ret[#ret + 1] = name
ret[#ret + 1] = name
for k, v in ipairs(invArgs) do
for k, v in ipairs(invArgs) do
if v:find('=', 1, true) then
if type(v) == 'string' and v:find('=', 1, true) then
-- Likely something like 1=foo=bar, we need to do it as a named arg
-- Likely something like 1=foo=bar which needs to be displayed as a named arg.
break
else
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
for k, v in pairs(invArgs) do
local keys = {} -- sort parameter list; better than arbitrary order
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] = k
ret[#ret + 1] = tostring(v)
ret[#ret + 1] = seps.equals
ret[#ret + 1] = seps.equals
ret[#ret + 1] = v
ret[#ret + 1] = invArgs[v]
end
end
ret[#ret + 1] = seps.closeb
ret[#ret + 1] = seps.closeb