Module:Template invocation: Difference between revisions

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
m (Protected "Module:Template invocation": Highly visible template ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)))
(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)
Line 94: Line 94:
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 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
local invArgs_list = {} -- sort a parameter list; preferable to randomly sorted output
local keys = {} -- sort parameter list; better than arbitrary order
for k, v in pairs(invArgs) do
for k, _ in pairs(invArgs) do
invArgs_list[#invArgs_list + 1] = k
keys[#keys + 1] = k
end
end
table.sort(invArgs_list)
table.sort(keys, function (a, b)
for i, v in ipairs(invArgs_list) do -- Add named args based on sorted parameter list
-- 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] = v
ret[#ret + 1] = tostring(v)
ret[#ret + 1] = seps.equals
ret[#ret + 1] = seps.equals
ret[#ret + 1] = invArgs[v]
ret[#ret + 1] = invArgs[v]
Anonymous user