Module:Transform infobox declarations

local exports = {};

local ERROR = "Infobox syntax error."

function error(error_, frame) return "echo"; end

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, var_parameters) local out = ""; local current = text; local depth = 0; local variable_stack = {}; local on_return = {}; local open; local close; local other; local arg; while current:find("") do       if depth < 0 then return out .. error(ERROR, frame), current; end open, _ = current:find("") or 5000000; if return_on and depth == 0 then other, _ = current:find(return_on); if other and other < open and other < close then return out .. current:sub(1, other - 1), current:sub(other); end end if not close then return out .. error(ERROR, frame), current; 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 .. "", frame, var_parameters);              if arg:match("{") then                    return out .. error("Cannot use templates or other parameters within parameter accesses.", frame), current;                end                variable_stack[depth] = arg;                arg = var_parameters[arg] or "";                out = out .. tostring(var_parameters[variable_stack[depth]] ~= nil or "") .. "|" .. arg;            else                out = out .. "{{";                current = current:sub(3);                if current:sub(1, 8):match("[Ii]nfobox ") then                    if contains({"group", "panel", "section"}, current:sub(9, current:find("|") - 1)) then                        out = out .. "subst:";                    elseif current:sub(9, current:find("|") - 1) == "label" then                        out = out .. "__IB_SUBST:" .. current:sub(1, current:find("|"));                        arg, current = _main(current:sub(current:find("|") + 1), "|", frame, var_parameters); current = current:sub(2); out = out .. arg .. "|{{ib:{{"; on_return[depth] = "}}}}"; elseif current:sub(9, current:find("|") - 1) == "image" then out = out .. "__IB_SUBST:"; out = out .. current:sub(1, current:find("|")); arg, current = _main(current:sub(current:find("|") + 1), "|", frame, var_parameters); out = out .. "{{ib:{{" .. arg .. "}}}}";                   else out = out .. "__IB_SUBST:"; out = out .. current:sub(1, current:find("|")) .. "{{ib:{{"; current = current:sub(current:find("|") + 1); on_return[depth] = "}}}}"; end else out = out .. "__IB_SUBST:"; end variable_stack[depth] = false; end depth = depth + 1; else out = out .. current:sub(1, close - 1); current = current:sub(close); depth = depth - 1; if variable_stack[depth] then if current:sub(3, 3) ~= "}" then return out .. error(ERROR, frame), current; end current = current:sub(4); out = out .. "|";           else current = current:sub(3); end out = out .. "}}";           if on_return[depth] then out = out .. on_return[depth]; on_return[depth] = nil; end end if return_on and depth == 0 then return out, current; elseif return_on and depth == -1 then if variable_stack[0] == true then return out:sub(1, -4), "|undefined}" .. current, current; end return out:sub(1, -3), "}}" .. current, 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) local var_parameters = {}; for key, value in pairs(frame.args) do       if type(key) == "number" then var_parameters[tostring(key - 1)] = value; else var_parameters[key] = value; end end out, current = _main(mw.text.decode(frame:preprocess("")), false, frame, var_parameters); return out .. current; end

exports.parse = _main;

return exports;