Module:Infobox: Difference between revisions
m bugfix |
Add infobox class |
||
| (22 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | |||
local getArgs = require("Module:Arguments").getArgs | local getArgs = require("Module:Arguments").getArgs | ||
local navbar = require("Module:Navbar")._navbar | local navbar = require("Module:Navbar")._navbar | ||
local tiu = require("Module:Template input utils") | |||
local yesno = require("Module:Yesno") | local yesno = require("Module:Yesno") | ||
local p = {} | local p = {} | ||
-- TODO ( | -- TODO (medium priority): | ||
-- - | -- - Use templatestyles | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 18: | Line 18: | ||
local title = args["Title"] or "Infobox Title" | local title = args["Title"] or "Infobox Title" | ||
local adjacent_links = args["Adjacent Links"] | local adjacent_links = args["Adjacent Links"] | ||
local header_row = args["Header"] | local header_row = args["Header Row"] | ||
local rows = args["Rows" | local rows = args["Rows"] | ||
local footer_row = args["Footer"] | 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 | |||
rows = preprocess_rows() | |||
-- Nested helper function | -- Nested helper function | ||
| Line 66: | Line 82: | ||
-- Produces a row in the infobox | -- Produces a row in the infobox | ||
function infobox_row(row_content) | function infobox_row(row_content) | ||
if | local header = row_content["Header"] | ||
local data = row_content["Data" ] | |||
if header and data then | |||
local lines = {} | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="text-align: right; padding-right: 0.25em;" | ' .. header) | |||
table.insert(lines, '| style="background-color: white; padding-left: 0.25em; font-weight: bold;" | ' .. data) | |||
return table.concat(lines, "\n") | |||
elseif header and not data then | |||
local lines = {} | local lines = {} | ||
table.insert(lines, '|-') | table.insert(lines, '|-') | ||
table.insert(lines, '| | table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. header) | ||
return table.concat(lines, "\n") | return table.concat(lines, "\n") | ||
elseif | elseif data and not header then | ||
local lines = {} | local lines = {} | ||
table.insert(lines, '|-') | table.insert(lines, '|-') | ||
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. | table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. data) | ||
return table.concat(lines, "\n") | return table.concat(lines, "\n") | ||
| Line 97: | Line 122: | ||
local lines = {} | local lines = {} | ||
table.insert(lines, '|-') | table.insert(lines, '|-') | ||
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. | table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. navbar(name, "mini", "")) | ||
return table.concat(lines, "\n") | return table.concat(lines, "\n") | ||
| Line 106: | Line 131: | ||
-- Infobox boilerplate | -- Infobox boilerplate | ||
table.insert(lines, [[<div style=" | table.insert(lines, | ||
[[<div class="infobox" style=" | |||
border: 1px solid #999; | border: 1px solid #999; | ||
margin: 0; | margin: 0; | ||
| Line 145: | Line 171: | ||
-- End of infobox | -- End of infobox | ||
table.insert(lines, "|}</div>") | table.insert(lines, "|}") | ||
table.insert(lines, "</div>") | |||
return table.concat(lines, "\n") | return table.concat(lines, "\n") | ||
| Line 160: | 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] | ||
-- 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 166: | Line 192: | ||
-- [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) | |||
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 row of links | ||
table.insert(adjacent_links, args[ | for _, key in ipairs(upper_links) do | ||
table.insert(adjacent_links, args[key] or "") | |||
args[key] = nil | |||
end | |||
table.insert(adjacent_links, args[ | -- Middle row of links | ||
for _, key in ipairs(side_links) do | |||
table.insert(adjacent_links, args[key] or "") | |||
args[key] = nil | |||
end | |||
-- Bottom row of links | |||
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 | ||
-- 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.numbered_header_data_args_to_table(args, 30) | |||
local result = p._infobox(args) | local result = p._infobox(args) | ||
| Line 249: | Line 269: | ||
["Adjacent Links"] = { (prev_link or ""), (next_link or "") }, | ["Adjacent Links"] = { (prev_link or ""), (next_link or "") }, | ||
["Title"] = title, | ["Title"] = title, | ||
["Rows"] = entries | ["Rows"] = entries | ||
} | } | ||
| Line 260: | Line 280: | ||
["Adjacent Links"] = adjacent_links, | ["Adjacent Links"] = adjacent_links, | ||
["Title"] = title, | ["Title"] = title, | ||
["Rows"] = entries | ["Rows"] = entries | ||
} | } | ||