Module:Template input utils: Difference between revisions

Ganaram inukshuk (talk | contribs)
Created page with "-- This module follows User:Ganaram inukshuk/Provisional style guide for Lua local p = {} -- TODO: move tip functions here? See Module:Template input parse -- Library module for common operations with handling template input. -- Extracts numbered args (from frame.args) and stores them into one table. -- Removes original numbered args from args table. Sequence of numbered args -- may have gaps. -- Table of entries is returned without being inserted into the ori..."
 
Ganaram inukshuk (talk | contribs)
add jagged_array_to_header_data_pairs function; to be added to infobox to autodetect between jagged array and header-data pairs, for backwards-compatibility
Line 78: Line 78:
test_args["Entries"] = p.header_data_pairs_to_table(test_args, 10, "Header %d", "Entry %d")
test_args["Entries"] = p.header_data_pairs_to_table(test_args, 10, "Header %d", "Entry %d")
return test_args
return test_args
end
-- Some older infoboxes use a jagged array instead of an assoc-array, so convert
-- two-element tables into a header-data pair, and one-element tables into
-- either a headerless data row, or a dataless header row. This may also be
-- easier to work with.
-- TODO: decide on default value
function p.jagged_array_to_header_data_pairs(rows, is_header_row)
local is_header_row = is_header_row or false
local new_rows = {}
for i = 1, #rows do
local row = rows[i]
if #row == 2 then
table.insert(new_rows, { ["Header"] = row[1], ["Data"] = row[2] })
elseif #row == 1 and not is_header_row then
table.insert(new_rows, { ["Data"  ] = row[1] })
elseif #row == 1 and is_header_row then
table.insert(new_rows, { ["Header"] = row[1] })
end
end
return new_rows
end
end


return p
return p