Module:Transform infobox declarations

local exports = {};

local error = "Infobox syntax error."

function contains(list, value) for _, item in pairs(list) do       if item == value then return true; end end return false; end

function _main(text, return_on_zero) local out = ""; local current = text; local depth = 0; local argument = false; local on_return = {}; local open; local close; local arg; while current:find("{{") do       if depth < 0 then return frame:expandTemplate({title="error", {error}}); end open, _ = current:find("{{"); close, _ = current:find("}}"); if open < close then out = out .. current:sub(1, open); current = current:sub(open); if argument then return frame:expandTemplate({title="error", {error}}); end if current:sub(3, 3) == "{" and current:sub(4, 4) ~= "{" then out = out .. "{{{";               current = current:sub(4); argument = true; else out = out .. "{{";               current = current:sub(3); depth = depth + 1; if current:sub(1, 15):match("[Ss][Uu][Bb][Ss][Tt]:[Ii]nfobox ") then out = out .. current:sub(1, 15); current = current:sub(16); if contains({"group", "panel", "section"}, current:sub(1, current:find("|") - 1)) then -- Do nothing. elseif current:sub(1, current:find("|") - 1) == "label" then out = out .. current:sub(1, current:find("|")); current = current:sub(current:find("|") + 1); arg, current = _main(current, true, frame); current = current:sub(2); out = out .. arg .. "|{{ib:{{"; on_return[depth] = "}}}}"; elseif current:sub(1, current:find("|") - 1) == "image" then out = out .. current:sub(1, current:find("|")); current = current:sub(current:find("|") + 1); arg, current = _main(current, true, frame); out = out .. arg .. "}}}}";                   else out = out .. current:sub(1, current:find("|")) .. "{{ib:{{"; current = current:sub(current:find("|") + 1); on_return[depth] = "}}}}"; end end end else out = out .. current:sub(1, close); current = current:sub(close); if argument then if current:sub(3, 3) ~= "}" then return frame:expandTemplate({title="error", {error}}); end out = out .. "}}}";               current = current:sub(4); argument = false; else if on_return[depth] then out = out .. on_return[depth]; on_return[depth] = nil; end out = out .. "}}";               current = current:sub(3); depth = depth - 1; end end if return_on_zero and depth == 0 then return out, current; end end return out; end

function exports.main(frame) return _main(frame:expandTemplate({title=":" .. frame.args[1]}), false, frame); end

return exports;