Module:Infobox: Difference between revisions

Ganaram inukshuk (talk | contribs)
add jagged-array preprocess function
Sintel (talk | contribs)
Add infobox class
 
(9 intermediate revisions by one other user not shown)
Line 7: Line 7:
local p = {}
local p = {}


-- TODO (high priority):
-- TODO (medium priority):
-- - Add the following backwards-compatibility feature: if rows are entered as
-- - Use templatestyles
--  a jagged array instead of the expected format of header-data pairs, convert
--  it from a jagged array to a table of header-data pairs.
 
-- TODO (medium priority): use templatestyles


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------- MAIN FUNCTIONS --------------------------------
-------------------------------- MAIN FUNCTIONS --------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Helper function
-- Preprocess rows from jagged array to header-data pairs
function p.preprocess_rows(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


-- 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 40: Line 22:
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 = p.preprocess_rows(rows)
rows = preprocess_rows()
-- Nested helper function
-- Nested helper function
Line 137: Line 132:
-- Infobox boilerplate
-- Infobox boilerplate
table.insert(lines,  
table.insert(lines,  
[[<div style="
[[<div class="infobox" style="
border: 1px solid #999;  
border: 1px solid #999;  
margin: 0;  
margin: 0;  
Line 192: Line 187:
-- 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]
-- - Link 1 is previous, link 2 is next
-- 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 198: Line 192:
-- [Link 4]  Title  [Link 5]
-- [Link 4]  Title  [Link 5]
-- [Link 6] [Link 7] [Link 8]
-- [Link 6] [Link 7] [Link 8]
-- Individual links can be omitted. If at least one upper or lower link is
local function any_keys_present(tbl, keys)
-- present, then rows for those two sets of links are added. Having no links
for _, key in ipairs(keys) do
-- defaults to having only the title.
if tbl[key] ~= nil then
local is_upper_lower_links_present = args["Upper Left Link"] ~= nil or args["Upper Link"] ~= nil or args["Upper Right Link"] ~= nil
return true
or args["Lower Left Link"] ~= nil or args["Lower Link"] ~= nil or args["Lower Right Link"] ~= nil
end
local is_side_links_present  = args["Left Link"] ~= nil or args["Right Link"] ~= nil
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)
local is_side_links_present = any_keys_present(args, side_links)
-- Build adjacent_links
-- Links from args are removed, as they're stored in a separate table
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 Left Link" ] or "")
-- Upper row of links
table.insert(adjacent_links, args["Upper Link"      ] or "")
for _, key in ipairs(upper_links) do
table.insert(adjacent_links, args["Upper Right Link"] or "")
table.insert(adjacent_links, args[key] or "")
end
args[key] = nil
if is_side_links_present then
end
table.insert(adjacent_links, args["Left Link" ] or "")
-- Middle row of links
table.insert(adjacent_links, args["Right Link"] or "")
for _, key in ipairs(side_links) do
end
table.insert(adjacent_links, args[key] or "")
if is_upper_lower_links_present then
args[key] = nil
table.insert(adjacent_links, args["Lower Left Link" ] or "")
end
table.insert(adjacent_links, args["Lower Link"      ] or "")
-- Bottom row of links
table.insert(adjacent_links, args["Lower Right Link"] or "")
for _, key in ipairs(lower_links) do
table.insert(adjacent_links, args[key] or "")
args[key] = nil
end
elseif is_side_links_present then
-- Left and right links only
for _, key in ipairs(side_links) do
table.insert(adjacent_links, args[key] or "")
args[key] = nil
end
end
end
args["Adjacent Links"] = adjacent_links
args["Adjacent Links"] = adjacent_links
-- Cleanup individual links, as they've been combined into one table
args["Upper Left Link" ] = nil
args["Upper Link"      ] = nil
args["Upper Right Link"] = nil
args["Left Link"      ] = nil
args["Right Link"      ] = nil
args["Lower Left Link" ] = nil
args["Lower Link"      ] = nil
args["Lower Right Link"] = nil
-- Preprocess rows
-- Preprocess rows
-- Set row count to 30, under the reasoning that an infobox may need more
-- Set row count to 30, under the reasoning that an infobox may need more
-- rows and/or headers. This may be increased to 40 if needed.
-- rows and/or headers. This may be increased to 40 if needed.
args["Rows"] = tiu.header_data_pairs_to_table(args, 30)
args["Rows"] = tiu.numbered_header_data_args_to_table(args, 30)
local result = p._infobox(args)
local result = p._infobox(args)
Line 258: Line 269:
["Adjacent Links"] = { (prev_link or ""), (next_link or "") },
["Adjacent Links"] = { (prev_link or ""), (next_link or "") },
["Title"] = title,
["Title"] = title,
["Rows"] = tiu.jagged_array_to_header_data_rows(entries)
["Rows"] = entries
}
}


Line 269: Line 280:
["Adjacent Links"] = adjacent_links,
["Adjacent Links"] = adjacent_links,
["Title"] = title,
["Title"] = title,
["Rows"] = tiu.jagged_array_to_header_data_rows(entries)
["Rows"] = entries
}
}