Module:Infobox: Difference between revisions

Ganaram inukshuk (talk | contribs)
m todo
Ganaram inukshuk (talk | contribs)
add support for header and footer
Line 11: Line 11:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------- HELPER FUNCTIONS -------------------------------
-------------------------------- MAIN FUNCTIONS --------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Infobox header with adjacent links
-- Function to be called by other modules; also called by wrapper function
-- Adjacent links are placed on the sides of the title, with options for 0, 2,
function p._infobox(args)
-- or 8 links.
local title          = args["Title"] or "Infobox Title"
function p.infobox_header(title, adjacent_links)
local adjacent_links = args["Adjacent Links"]
local header = ''
local header_row = args["Header"]
if adjacent_links == nil then
local rows      = args["Rows"  ]
header = title
local footer_row = args["Footer"]
elseif #adjacent_links == 2 then
lines = {}
-- Nested helper function
--table.insert(lines, "\n")
-- Produces the title and, if present, prev/next links
table.insert(lines, '{| style="width: 100%;"')
function infobox_title()
table.insert(lines, '|-')
if adjacent_links == nil then
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1])
return title
table.insert(lines, '| style="width: 50%;" | ' .. title)
elseif #adjacent_links == 2 then
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[2])
lines = {}
table.insert(lines, '|}')
--table.insert(lines, "\n")
table.insert(lines, '{| style="width: 100%;"')
header = table.concat(lines, "\n")
table.insert(lines, '|-')
elseif #adjacent_links == 8 then
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1])
lines = {}
table.insert(lines, '| style="width: 50%;" | ' .. title)
--table.insert(lines, "\n")
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[2])
table.insert(lines, '{| style="width: 100%;"')
table.insert(lines, '|}')
table.insert(lines, '|-')
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1])
return table.concat(lines, "\n")
table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[2])
elseif #adjacent_links == 8 then
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[3])
lines = {}
table.insert(lines, '|-')
--table.insert(lines, "\n")
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[4])
table.insert(lines, '{| style="width: 100%;"')
table.insert(lines, '| style="width: 50%;"  | ' .. title)
table.insert(lines, '|-')
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[5])
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[1])
table.insert(lines, '|-')
table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[2])
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[6])
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[3])
table.insert(lines, '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[7])
table.insert(lines, '|-')
table.insert(lines, '| style="font-size: 0.75em;" | ' .. adjacent_links[8])
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])
header = table.concat(lines, "\n")
return table.concat(lines, "\n")
else
end
header = title
end
end
return header
-- Nested helper function
end
-- Produces a header or footer row
 
function header_footer_row(row_content)
-- Row of an infobox
-- Infobox is set up as a Mediawiki table with two cols.
function p.infobox_row(row_content)
local row = ""
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])
row = table.concat(lines, "\n")
elseif #row_content == 1 then
local lines = {}
local lines = {}
table.insert(lines, '|-')
table.insert(lines, '|-')
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. row_content[1])
table.insert(lines, '| colspan="2" style="text-align: center; font_size: 0.8em;" | ' .. row_content)
row = table.concat(lines, "\n")
return table.concat(lines, "\n")
end
end
return row
end
--------------------------------------------------------------------------------
-------------------------------- MAIN FUNCTIONS --------------------------------
--------------------------------------------------------------------------------
-- Function to be called by other modules; also called by wrapper function
function p._infobox(args)
local title = args["Title"] or "Infobox Title"
local adjacent_links = args["Adjacent Links"]
local rows = args["Rows"]
-- 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, p.infobox_header(title, adjacent_links))
-- Title
table.insert(lines, infobox_title())
-- Header
if header_row then
table.insert(lines, header_footer_row(header_row))
end
-- For loop for populating entries
-- Rows
for i = 1, #rows do
for i = 1, #rows do
table.insert(lines, p.infobox_row(rows[i]))
table.insert(lines, infobox_row(rows[i]))
end
-- Footer
if footer_row then
table.insert(lines, header_footer_row(footer_row))
end
end