Module:Infobox: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(use tostring() in getArgNums() - this is quicker than forcing concatenation of numbers)
(better comments)
Line 11: Line 11:
function union(t1, t2)
function union(t1, t2)
-- return the union of the values of two tables, as a sequence
-- Returns the union of the values of two tables, as a sequence.
local vals = {}
local vals = {}
for k, v in pairs(t1) do
for k, v in pairs(t1) do
Line 27: Line 27:


local function getArgNums(prefix)
local function getArgNums(prefix)
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local nums = {}
local nums = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
Line 37: Line 40:


local function addRow(rowArgs)
local function addRow(rowArgs)
-- Adds a row to the infobox, with either a header cell
-- or a label/data cell combination.
if rowArgs.header then
if rowArgs.header then
root
root
Line 159: Line 164:


local function renderRows()
local function renderRows()
-- Gets the union of the header and data argument numbers,
-- and renders them all in order using addRow.
local rownums = union(getArgNums('header'), getArgNums('data'))
local rownums = union(getArgNums('header'), getArgNums('data'))
table.sort(rownums)
table.sort(rownums)
Line 206: Line 213:


local function _infobox()
local function _infobox()
-- Specify the overall layout of the infobox, with special settings
-- if the infobox is used as a 'child' inside another infobox.
if args.child ~= 'yes' then
if args.child ~= 'yes' then
root = HtmlBuilder.create('table')
root = HtmlBuilder.create('table')
Line 249: Line 258:
end
end


-- This function parses the parameters with the given prefixes, in order, in batches of the step size specified.
-- If the step size is not given, the default is 20.
local function touchParameters(prefixTable, origArgs, step)
local function touchParameters(prefixTable, origArgs, step)
-- Parse the parameters with the given prefixes, in order, in batches
-- of the step size specified. This is to prevent references etc. from
-- appearing in the wrong order.
if type(prefixTable) ~= 'table' or type(origArgs) ~= 'table' then
if type(prefixTable) ~= 'table' or type(origArgs) ~= 'table' then
error("Invalid input to the touchParameters function detected. Both parameters must be tables.", 2)
error("Invalid input to the touchParameters function detected. Both parameters must be tables.", 2)
Line 259: Line 269:
end
end
step = step or 20
step = step or 20 -- If the step size is not given, the default is 20.
local temp
local temp
local a = 1
local a = 1 -- Counter variable.
local moreArgumentsExist = true
local moreArgumentsExist = true
for j,v in ipairs(prefixTable) do
for j,v in ipairs(prefixTable) do