440
edits
(add a p.name function to find the template name, use checkType with p.invocation, and add some function description comments) |
m (11 revisions imported from wikipedia:Module:Template_invocation) |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 1:
-- This module
local checkType = require('libraryUtil').checkType
Line 13 ⟶ 12:
-- object has been passed in, and uses that to find a
-- template name as it is used in template invocations.
-- Parameters: title - full page name or mw.title
-- template (string or mw.title object)
-- Returns: String
Line 42 ⟶ 41:
-- name and the arguments given. Note that it isn't
-- perfect: we have no way of knowing what whitespace was
-- in the original invocation, the
--
-- will be removed.
-- Parameters: name - the template name, formatted as it will appear
Line 60 ⟶ 59:
checkType('invocation', 3, format, 'string', true)
--
--
--
local invArgs = {}
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
end
Line 86 ⟶ 93:
ret[#ret + 1] = name
for k, v in ipairs(invArgs) do
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
break
Line 94 ⟶ 101:
invArgs[k] = nil -- Erase the key so that we don't add the value twice
end
local invArgs_list = {} -- sort a parameter list; preferable to randomly sorted output
for k, v in pairs(invArgs) do
invArgs_list[#invArgs_list + 1] = k
end
table.sort(invArgs_list)
for i, v in ipairs(invArgs_list) do -- Add named args based on sorted parameter list
ret[#ret + 1] = seps.pipe
ret[#ret + 1] = k▼
ret[#ret + 1] = seps.equals▼
ret[#ret + 1] = v
▲ ret[#ret + 1] = seps.equals
end
ret[#ret + 1] = seps.closeb
|