Module:Convert/tester: Difference between revisions

Content deleted Content added
check_sandbox includes a diff link
better emulation of how MediaWiki parses parameters to a template
Line 78:
 
local function run_template(frame, template, collapse_multiline)
-- Template "{{ example | 2 = def | abc | name = ghi jkl }}"
local title, argstr = template:match('^{{%s*(.-)%s*|(.*)}}$')
if-- titlegives ==args nil{ or" title ==abc '' or", argstr"def", name == ''"ghi jkl" then}.
if template:sub(1, 2) == '{{' and template:sub(-2, -1) == '}}' then
template = template:sub(3, -3) .. '|' -- append sentinel to get last field
else
return '(invalid template)'
end
local args = {}
local index = 1
for item in string.gmatch(argstr .. '|', '(.-)|') do
local p = item:find('=', 1, true)templatename
for itemfield in string.template:gmatch(argstr .. '|', '(.-)|') do
if p then
if templatename == nil then
args[item:sub(1, p-1)] = item:sub(p+1)
templatename = strip(field)
if templatename == '' then
return '(invalid template)'
end
else
table.insert(argslocal k, itemeq, v = field:match("^(.-)(=)(.*)$")
if peq then
k, v = strip(k), strip(v) -- k and/or v can be empty
local i = tonumber(k)
if i and i > 0 and string.match(k, '^%d+$') then
args[i] = v
else
args[k] = v
end
else
while args[index] ~= nil do
-- Skip any explicit numbered parameters like "|5=five".
index = index + 1
end
args[index] = field
end
end
end
Line 94 ⟶ 116:
return frame:expandTemplate(t)
end
local ok, result = pcall(expand, { title = titletemplatename, args = args })
if not ok then
result = 'Error: ' .. result