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, frame) local out = ""; local current = text; local depth = 0; local variable_stack = {}; local on_return = {}; local open; local close; local arg; while current:find("{{") do       if depth < 0 then return out .. frame:expandTemplate({title="error", {error}}), current; end open, _ = current:find("{{"); close, _ = current:find("}}"); if return_on then other, _ = current:find(return_on); end if not close then return out .. frame:expandTemplate({title="error", {error}}), current; end if return_on and depth == 0 and other < open and other < close then return out .. current:sub(1, other - 1), current:sub(other); end if open < close then out = out .. current:sub(1, open - 1); current = current:sub(open); if current:sub(3, 3) == "{" and current:sub(4, 4) ~= "{" then out = out .. "{{{";               current = current:sub(4); variable_stack[depth] = true; else out = out .. "{{subst:"; current = current:sub(3); if current:sub(1, 8):match("[Ii]nfobox ") then out = out .. current:sub(1, 8); current = current:sub(9); 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, "|", 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, "|", frame); out = out .. "{{ib:{{" .. arg .. "}}}}";                   else out = out .. current:sub(1, current:find("|")) .. "{{ib:{{"; current = current:sub(current:find("|") + 1); on_return[depth] = "}}}}"; end end variable_stack[depth] = false; end depth = depth + 1; else out = out .. current:sub(1, close - 1); current = current:sub(close); if variable_stack[depth - 1] then if current:sub(3, 3) ~= "}" then return out .. frame:expandTemplate({title="error", {error}}), current; end out = out .. "}}}";               current = current:sub(4); else if on_return[depth - 1] then out = out .. on_return[depth - 1]; on_return[depth - 1] = nil; end out = out .. "}}";               current = current:sub(3); end depth = depth - 1; end if return_on and depth == 0 then return out, current; elseif return_on and depth == -1 then return out:sub(1, -2), "}}" .. current; end end if return_on then return out .. current:sub(1, current:find(return_on) - 1), current:sub(current:find(return_on)); end return out, current; end

function exports.main(frame) out, current = frame:preprocess(_main(frame.args[1], false, frame)); return out .. current; end

return exports;