Module:Template invocation: Difference between revisions

Content deleted Content added
fixes requested at Module talk:Template invocation: keys of parameters may be numbers or strings so need a custom sort; that break was wrong as more could be following
Update name call to fix a few edge cases per request
 
(2 intermediate revisions by 2 users not shown)
Line 20:
if type(title) == 'string' then
title = mw.title.new(title)
if not title or #title.prefixedText == 0 or #title.interwiki > 0 then
error("invalid title in parameter #1 of function 'name'", 2)
end
Line 27:
end
if title.namespace == 10 then
returnlocal text = title.text
local check = mw.title.new(text, 10)
-- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo".
return check and mw.title.equals(title, check) and text or title.prefixedText
elseif title.namespace == 0 then
return ':' .. title.prefixedText
Line 34 ⟶ 37:
end
end
 
------------------------------------------------------------------------
-- Name: p.invocation
Line 105 ⟶ 107:
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
Line 112 ⟶ 115:
return true
end
end)
)
local maybeSpace = '' -- First named parameter should not be separated by a space
for _, v in ipairs(keys) do -- Add named args based on sorted parameter list
ret[#ret + 1] = maybeSpace .. seps.pipe
ret[#ret + 1] = tostring(v)
ret[#ret + 1] = seps.equals
ret[#ret + 1] = invArgs[v]
maybeSpace = ' '
end
ret[#ret + 1] = seps.closeb