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 | -- Likely something like 1=foo=bar which needs to be displayed as a named arg. | ||
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 | ||
end | end | ||
local | local keys = {} -- sort parameter list; better than arbitrary order | ||
for k, | for k, _ in pairs(invArgs) do | ||
keys[#keys + 1] = k | |||
end | end | ||
table.sort( | table.sort(keys, function (a, b) | ||
for | -- 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] |