Module:Infobox: Difference between revisions
m todo |
add support for header and footer |
||
| Line 11: | Line 11: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
------------------------------- | -------------------------------- MAIN FUNCTIONS -------------------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Infobox | -- Function to be called by other modules; also called by wrapper function | ||
-- | function p._infobox(args) | ||
local title = args["Title"] or "Infobox Title" | |||
function | local adjacent_links = args["Adjacent Links"] | ||
local header_row = args["Header"] | |||
local rows = args["Rows" ] | |||
local footer_row = args["Footer"] | |||
-- Nested helper function | |||
-- Produces the title and, if present, prev/next links | |||
function infobox_title() | |||
if adjacent_links == nil then | |||
return title | |||
elseif #adjacent_links == 2 then | |||
lines = {} | |||
--table.insert(lines, "\n") | |||
table.insert(lines, '{| style="width: 100%;"') | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1]) | |||
table.insert(lines, '| style="width: 50%;" | ' .. title) | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[2]) | |||
table.insert(lines, '|}') | |||
return table.concat(lines, "\n") | |||
elseif #adjacent_links == 8 then | |||
lines = {} | |||
--table.insert(lines, "\n") | |||
table.insert(lines, '{| style="width: 100%;"') | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1]) | |||
table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[2]) | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[3]) | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[4]) | |||
table.insert(lines, '|}') | table.insert(lines, '| style="width: 50%;" | ' .. title) | ||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[5]) | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[6]) | |||
table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[7]) | |||
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[8]) | |||
table.insert(lines, '|}') | |||
return table.concat(lines, "\n") | |||
else | |||
return title | |||
end | |||
end | |||
-- Nested helper function | |||
-- Produces a row in the infobox | |||
function infobox_row(row_content) | |||
if #row_content > 1 then | |||
local lines = {} | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| style="text-align: right; padding-right: 0.25em;" | ' .. row_content[1]) | |||
table.insert(lines, '| style="background-color: white; padding-left: 0.25em; font-weight: bold;" | ' .. row_content[2]) | |||
return table.concat(lines, "\n") | |||
elseif #row_content == 1 then | |||
local lines = {} | |||
table.insert(lines, '|-') | |||
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. row_content[1]) | |||
return table.concat(lines, "\n") | |||
end | |||
end | end | ||
-- Nested helper function | |||
-- Produces a header or footer row | |||
function header_footer_row(row_content) | |||
-- | |||
-- | |||
local lines = {} | local lines = {} | ||
table.insert(lines, '|-') | table.insert(lines, '|-') | ||
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. row_content | table.insert(lines, '| colspan="2" style="text-align: center; font_size: 0.8em;" | ' .. row_content) | ||
return table.concat(lines, "\n") | |||
end | end | ||
-- Start of infobox; outer div and start of table | -- Start of infobox; outer div and start of table | ||
local lines = {} | local lines = {} | ||
-- Infobox boilerplate | |||
table.insert(lines, [[<div style=" | table.insert(lines, [[<div style=" | ||
border: 1px solid #999; | border: 1px solid #999; | ||
| Line 104: | Line 107: | ||
overflow: auto;">]] | overflow: auto;">]] | ||
) | ) | ||
table.insert(lines, '{| style="border-collapse: collapse; width: 100%;"') | table.insert(lines, '{| style="border-collapse: collapse; width: 100%;"') | ||
table.insert(lines, '|+ style="font-size: 105%; font-weight: bold; text-align: center;" | ') | table.insert(lines, '|+ style="font-size: 105%; font-weight: bold; text-align: center;" | ') | ||
table.insert(lines, | |||
-- Title | |||
table.insert(lines, infobox_title()) | |||
-- Header | |||
if header_row then | |||
table.insert(lines, header_footer_row(header_row)) | |||
end | |||
-- | -- Rows | ||
for i = 1, #rows do | for i = 1, #rows do | ||
table.insert(lines, | table.insert(lines, infobox_row(rows[i])) | ||
end | |||
-- Footer | |||
if footer_row then | |||
table.insert(lines, header_footer_row(footer_row)) | |||
end | end | ||