Module:Transform infobox declarations

local exports = {};

local ERROR = "Infobox syntax error.";

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

function concat(array, array2) local out = {}; local index = 1; for _, value in ipairs(array) do index = index + 1; out[index] = value; end for _, value in ipairs(array2) do index = index + 1; out[index] = value; end return out; end

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

function closeall(out, variable_stack) local depth = #variable_stack - 1; while depth >= 0 do       if variable_stack[depth] == "|L" then out = out .. "]]";       elseif variable_stack[depth] then out = out .. "}}}";       else out = out .. "}}";       end depth = depth - 1; end return out; end

function _main(text, return_on, frame, var_parameters, previous_var_parameters) local out = ""; local current = text; local depth = 0; local variable_stack = {}; local on_return = {}; local open; local close; local open2; local close2; local other; local arg; local random; previous_var_parameters = previous_var_parameters or {}; while current:find("") or current:find("%[%[") or current:find("%]%]") do       if depth < 0 then return out .. error(ERROR, frame), current, true; end open, _ = current:find("") or 5000000; open2, _ = current:find("%[%[") or 5000000; close2, _ = current:find("%]%]") or 5000000; if return_on and depth == 0 then other, _ = current:find(return_on); if other and other < open and other < close and other < open2 and other < close2 then return out .. current:sub(1, other - 1), current:sub(other), false; end end if open2 < open and open2 < close and open2 < close2 then out = out .. current:sub(1, open2 - 1) .. "($C)"; current = current:sub(open2 + 2); variable_stack[depth] = "|L"; depth = depth + 1; arg, current, error_ = _main(current, "|", frame, var_parameters, concat(previous_variable_stack, variable_stack)); if error_ then return closeall(out .. arg, concat(previous_variable_stack, variable_stack)), "", true; end out = out .. arg .. "($E)"; current = current:sub(2); elseif close2 < open and close2 < close and close2 < open2 then out = out .. current:sub(1, close2 - 1) .. "($D)"; current = current:sub(close2 + 2); depth = depth - 1; if variable_stack[depth] ~= "|L" then return closeall(out, concat(previous_variable_stack, variable_stack)), "", true; end elseif 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, false; end return out:sub(1, -3), "}}" .. current, current, false; end end if return_on then return out .. current:sub(1, current:find(return_on) - 1), current:sub(current:find(return_on)), false; end return out, current, false; 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;