Module:Infobox: Difference between revisions

Inthar (talk | contribs)
No edit summary
Sintel (talk | contribs)
Add infobox class
 
(103 intermediate revisions by 6 users not shown)
Line 1: Line 1:
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local getArgs = require("Module:Arguments").getArgs
local navbar  = require("Module:Navbar")._navbar
local tiu    = require("Module:Template input utils")
local yesno  = require("Module:Yesno")
local p = {}
local p = {}


local function put_adjacent_x_links(title, s, prev_x, next_x)
-- TODO (medium priority):
return s .. '<table style="width: 100%; margin: 0"><tr>'
-- - Use templatestyles
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
 
.. (prev_x or '')
--------------------------------------------------------------------------------
.. '</td>'
-------------------------------- MAIN FUNCTIONS --------------------------------
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
--------------------------------------------------------------------------------
.. title
.. '</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (next_x or '')
.. '</td>'
.. '</tr></table>'
end


function p.build(title, entries, prev_link, next_link)
-- Function to be called by other modules; also called by wrapper function
local s = '<div style="\n' ..
function p._infobox(args)
'border: 1px solid #999;\n' ..
local title         = args["Title"] or "Infobox Title"
'margin: 0;\n' ..
local adjacent_links = args["Adjacent Links"]
'margin-left: 1em;\n' ..
local header_row = args["Header Row"]
'margin-bottom: 0.5em;\n' ..
local rows      = args["Rows"]
'padding: 0.5em;\n' ..
local footer_row = args["Footer Row"]
'background-color: #f0f0f0;\n' ..
local name      = args["name"]
'min-width: 15em;\n' ..
'float: right;\n' ..
-- Helper function; preprocess rows
'max-width: 100%;\n' ..
function preprocess_rows()
'overflow: auto;\n' ..
-- Preproces rows
'">\n' ..
local is_jagged = true
'{| width="100%" style="border-collapse: collapse;"\n' ..
for i = 1, #rows do
'|+ style="font-weight: bold; text-align: center;" | '
local row = rows[i]
local has_adjacent = (prev_link and #prev_link > 0) or (next_link and #next_link > 0)
is_jagged = is_jagged and (#row == 1 or #row == 2)
if has_adjacent then
end
s = put_adjacent_x_links(title, s, prev_link, next_link)
else
if is_jagged then return tiu.jagged_array_to_header_data_pairs(rows)
s = s .. title
else return rows end
end
-- Preprocess rows
rows = preprocess_rows()
-- 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, '| 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
end
s = s .. '\n'
for i, entry in ipairs(entries) do
-- Nested helper function
if #entry > 1 then
-- Produces a row in the infobox
local caption = entry[1]
function infobox_row(row_content)
local text = entry[2]
local header = row_content["Header"]
s = s .. '|-\n' ..
local data  = row_content["Data"  ]
'| style="text-align:right; padding-right: 0.25em" | ' .. caption .. '\n' ..
'| style="background-color: white; padding-left: 0.25em; font-weight: bold" | ' .. text .. '\n'
if header and data then
elseif #entry == 1 then
local lines = {}
local text = entry[1]
table.insert(lines, '|-')
s = s .. '|-\n'
table.insert(lines, '| style="text-align: right; padding-right: 0.25em;" | ' .. header)
.. '| colspan="2" style="text-align: center;" | ' .. text .. '\n'
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 = {}
table.insert(lines, '|-')
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. header)
return table.concat(lines, "\n")
elseif data and not header then
local lines = {}
table.insert(lines, '|-')
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. data)
return table.concat(lines, "\n")
end
end
end
end
s = s .. '|}</div>'
return s
-- Nested helper function
-- Produces a header or footer row
function header_footer_row(row_content)
local lines = {}
table.insert(lines, '|-')
table.insert(lines, '| colspan="2" style="text-align: center; font-size: 0.8em;" | ' .. row_content)
return table.concat(lines, "\n")
end
-- Nested helper function
-- Produces the navbar, if specified
function navbar_row()
local lines = {}
table.insert(lines, '|-')
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. navbar(name, "mini", ""))
return table.concat(lines, "\n")
end
-- Start of infobox; outer div and start of table
local lines = {}
-- Infobox boilerplate
table.insert(lines,
[[<div class="infobox" style="
border: 1px solid #999;
margin: 0;
margin-left: 1em;
margin-bottom: 0.5em;
padding: 0.5em;
background-color: #f0f0f0;
min-width: 15em;
float: right;
max-width: 100%;
overflow: auto;">]]
)
table.insert(lines, '{| style="border-collapse: collapse; width: 100%;"')
table.insert(lines, '|+ style="font-size: 105%; font-weight: bold; text-align: center;" | ')
-- 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
table.insert(lines, infobox_row(rows[i]))
end
-- Footer
if footer_row then
table.insert(lines, header_footer_row(footer_row))
end
-- Template navbar
if name then
table.insert(lines, navbar_row())
end
-- End of infobox
table.insert(lines, "|}")
table.insert(lines, "</div>")
 
return table.concat(lines, "\n")
end
end


function p.build(title, entries, prev_x, next_x, prev_y, next_y)
-- Function to be #invoke'd
-- Wrapper function for template-based infoboxes
-- Modeled off of navbox
function p.infobox(frame)
local args = getArgs(frame)
-- Preprocess adjacent links
-- If there are two adjacent links (such as with edos), then links will be
-- placed on the left and right of the title:
-- [Link 1] Title [Link 2]
-- If there are eight adjacent links (such as with mosses), then links
-- surround the title in a 3x3 grid:
-- [Link 1] [Link 2] [Link 3]
-- [Link 4]  Title  [Link 5]
-- [Link 6] [Link 7] [Link 8]
local function any_keys_present(tbl, keys)
for _, key in ipairs(keys) do
if tbl[key] ~= nil then
return true
end
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" }
local s = '<div style="\n' ..
-- Check which links are present
'border: 1px solid #999;\n' ..
local is_upper_lower_links_present =  
'margin: 0;\n' ..
any_keys_present(args, upper_links) or any_keys_present(args, lower_links)
'margin-left: 1em;\n' ..
local is_side_links_present = any_keys_present(args, side_links)
'margin-bottom: 0.5em;\n' ..
'padding: 0.5em;\n' ..
'background-color: #f0f0f0;\n' ..
'min-width: 15em;\n' ..
'float: right;\n' ..
'max-width: 100%;\n' ..
'overflow: auto;\n' ..
'">\n' ..
'{| width="100%" style="border-collapse: collapse;"\n' ..
'|+ style="font-weight: bold; text-align: center;" | '
local has_adjacent_x = (prev_x and #prev_x > 0) or (next_x and #next_x > 0)
local has_adjacent_y = (prev_y and #prev_y > 0) or (next_y and #next_y > 0)
if prev_y then -- prev y
-- Build adjacent_links
s = s .. '<table style="width: 100%; margin: 0"><tr>'
-- Links from args are removed, as they're stored in a separate table
.. '<td></td><td style="\n'
local adjacent_links = {}
.. 'width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
if is_upper_lower_links_present then
.. prev_y
-- Upper row of links
.. '\n</td><td></td>'
for _, key in ipairs(upper_links) do
.. '\n<tr>'
table.insert(adjacent_links, args[key] or "")
.. '<table style="width: 100%; margin: 0"><tr>'
args[key] = nil
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
end
.. (prev_x or '')
-- Middle row of links
.. '</td>'
for _, key in ipairs(side_links) do
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
table.insert(adjacent_links, args[key] or "")
.. title
args[key] = nil
.. '</td>'
end
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
-- Bottom row of links
.. (next_x or '')
for _, key in ipairs(lower_links) do
.. '</td>'
table.insert(adjacent_links, args[key] or "")
.. '</tr>'
args[key] = nil
elseif has_adjacent_x then -- no prev y, has adjacent x
end
s = s .. '<table style="width: 100%; margin: 0"><tr>'
elseif is_side_links_present then
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
-- Left and right links only
.. (prev_x or '')
for _, key in ipairs(side_links) do
.. '</td>'
table.insert(adjacent_links, args[key] or "")
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
args[key] = nil
.. title
.. '</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (next_x or '')
.. '</td>'
.. '</tr>'
if next_y then
s = s .. '<tr><td></td>'
.. '<td style="\n'
.. 'width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. next_y
.. '\n</td><td></td>'
.. '</tr>'
end
end
s = s ..  '</table>'
elseif next_y then -- no prev y, no adjacent x, so first row should be title in center
s = s .. '<table style="width: 100%; margin: 0"><tr>'
.. '<td>'
.. '</td>'
.. '<td>'
.. title
.. '</td>'
.. '<td>'
.. '</td>'
.. '</tr>'
.. '<tr><td></td>'
.. '<td style="\n'
.. 'width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. next_y
.. '\n</td><td></td>'
.. '</tr>'
.. '</table>'
else
s = s .. title
end
end
s = s .. '\n'
args["Adjacent Links"] = adjacent_links
for i, entry in ipairs(entries) do
if #entry > 1 then
-- Preprocess rows
local caption = entry[1]
-- Set row count to 30, under the reasoning that an infobox may need more
local text = entry[2]
-- rows and/or headers. This may be increased to 40 if needed.
s = s .. '|-\n' ..
args["Rows"] = tiu.numbered_header_data_args_to_table(args, 30)
'| style="text-align:right; padding-right: 0.25em" | ' .. caption .. '\n' ..
'| style="background-color: white; padding-left: 0.25em; font-weight: bold" | ' .. text .. '\n'
local result = p._infobox(args)
elseif #entry == 1 then
local debugg = yesno(args["debug"])
local text = entry[1]
if debugg == true then
s = s .. '|-\n'
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
.. '| colspan="2" style="text-align: center;" | ' .. text .. '\n'
end
end
end
s = s .. '|}</div>'
return s
return result
end
 
--------------------------------------------------------------------------------
------------------------------- LEGACY FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
 
-- These functions are kept in the meantime to support older infoboxes and WILL
-- be entirely deleted later! Please switch to the new functions!
 
-- Original function signature, kept for legacy support (for now).
function p.build(title, entries, prev_link, next_link)
local args = {
["Adjacent Links"] = { (prev_link or ""), (next_link or "") },
["Title"] = title,
["Rows"] = entries
}
 
return p._infobox(args)
end
 
-- Original 8-link function signature, pulled from infobox mos.
function p.build_multilink(title, entries, adjacent_links)
local args = {
["Adjacent Links"] = adjacent_links,
["Title"] = title,
["Rows"] = entries
}
 
return p._infobox(args)
end
end


return p
return p