Module:Infobox: Difference between revisions
m update todo |
move preprocess function as a nested function; refactor link-detector code |
||
| Line 12: | Line 12: | ||
-------------------------------- MAIN FUNCTIONS -------------------------------- | -------------------------------- MAIN FUNCTIONS -------------------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Function to be called by other modules; also called by wrapper function | -- Function to be called by other modules; also called by wrapper function | ||
| Line 35: | Line 21: | ||
local footer_row = args["Footer Row"] | local footer_row = args["Footer Row"] | ||
local name = args["name"] | local name = args["name"] | ||
-- Helper function; preprocess rows | |||
function preprocess_rows() | |||
-- Preproces rows | |||
local is_jagged = true | |||
for i = 1, #rows do | |||
local row = rows[i] | |||
is_jagged = is_jagged and (#row == 1 or #row == 2) | |||
end | |||
if is_jagged then return tiu.jagged_array_to_header_data_pairs(rows) | |||
else return rows end | |||
end | |||
-- Preprocess rows | -- Preprocess rows | ||
rows = | rows = preprocess_rows() | ||
-- Nested helper function | -- Nested helper function | ||
| Line 187: | Line 186: | ||
-- placed on the left and right of the title: | -- placed on the left and right of the title: | ||
-- [Link 1] Title [Link 2] | -- [Link 1] Title [Link 2] | ||
-- If there are eight adjacent links (such as with mosses), then links | -- If there are eight adjacent links (such as with mosses), then links | ||
-- surround the title in a 3x3 grid: | -- surround the title in a 3x3 grid: | ||
| Line 193: | Line 191: | ||
-- [Link 4] Title [Link 5] | -- [Link 4] Title [Link 5] | ||
-- [Link 6] [Link 7] [Link 8] | -- [Link 6] [Link 7] [Link 8] | ||
-- | local function any_keys_present(tbl, keys) | ||
-- present, | for _, key in ipairs(keys) do | ||
-- | if tbl[key] ~= nil then | ||
local | return true | ||
end | |||
local | end | ||
return false | |||
end | |||
-- Keys to links | |||
-- Left and right are added if at least one is present -> 2-element table. | |||
-- All three rows are added if at least one link from the upper or lower | |||
-- rows is present, regardless of side links -> 8-element table. | |||
-- No links are added if none of them are present -> 0-element table. | |||
local upper_links = { "Upper Left Link", "Upper Link", "Upper Right Link" } | |||
local side_links = { "Left Link", "Right Link" } | |||
local lower_links = { "Lower Left Link", "Lower Link", "Lower Right Link" } | |||
-- Check which links are present | |||
local is_upper_lower_links_present = | |||
any_keys_present(args, upper_links) or any_keys_present(args, lower_links) | |||
-- Build adjacent_links | |||
local adjacent_links = {} | local adjacent_links = {} | ||
if is_upper_lower_links_present then | if is_upper_lower_links_present then | ||
table.insert(adjacent_links, args[ | -- Upper row of links | ||
for _, key in ipairs(upper_links) do | |||
table.insert(adjacent_links, args[key] or "") | |||
end | |||
-- Middle row of links | |||
for _, key in ipairs(side_links) do | |||
table.insert(adjacent_links, args[key] or "") | |||
end | |||
-- Bottom row of links | |||
for _, key in ipairs(lower_links) do | |||
table.insert(adjacent_links, args[key] or "") | |||
end | |||
else | |||
-- Left and right links only | |||
for _, key in ipairs(side_links) do | |||
table.insert(adjacent_links, args[key] or "") | |||
end | |||
end | end | ||
args["Adjacent Links"] = adjacent_links | args["Adjacent Links"] = adjacent_links | ||