Module:Infobox: Difference between revisions

Ganaram inukshuk (talk | contribs)
m Corrected a variable name
Sintel (talk | contribs)
Add infobox class
 
(89 intermediate revisions by 4 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 = {}


function p.build(title, entries, prev_link, next_link)
-- TODO (medium priority):
local s = '<div style="\n'
-- - Use templatestyles
.. "border: 1px solid #999;\n"
 
.. "margin: 0;\n"
--------------------------------------------------------------------------------
.. "margin-left: 1em;\n"
-------------------------------- MAIN FUNCTIONS --------------------------------
.. "margin-bottom: 0.5em;\n"
--------------------------------------------------------------------------------
.. "padding: 0.5em;\n"
 
.. "background-color: #f0f0f0;\n"
-- Function to be called by other modules; also called by wrapper function
.. "min-width: 15em;\n"
function p._infobox(args)
.. "float: right;\n"
local title         = args["Title"] or "Infobox Title"
.. "max-width: 100%;\n"
local adjacent_links = args["Adjacent Links"]
.. "overflow: auto;\n"
local header_row = args["Header Row"]
.. '">\n'
local rows      = args["Rows"]
.. '{| width="100%" style="border-collapse: collapse;"\n'
local footer_row = args["Footer Row"]
.. '|+ style="font-weight: bold; text-align: center;" | '
local name      = args["name"]
local has_adjacent = (prev_link and #prev_link > 0) or (next_link and #next_link > 0)
if has_adjacent then
-- Helper function; preprocess rows
s = s
function preprocess_rows()
.. '<table style="width: 100%; margin: 0"><tr>'
-- Preproces rows
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
local is_jagged = true
.. (prev_link or "")
for i = 1, #rows do
.. "</td>"
local row = rows[i]
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">'
is_jagged = is_jagged and (#row == 1 or #row == 2)
.. title
end
.. "</td>"
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
if is_jagged then return tiu.jagged_array_to_header_data_pairs(rows)
.. (next_link or "")
else return rows end
.. "</td>"
end
.. "</tr></table>"
else
-- Preprocess rows
s = s .. title
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 _, 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
local data  = row_content["Data"  ]
.. "|-\n"
.. '| style="text-align:right; padding-right: 0.25em" | '
if header and data then
.. caption
local lines = {}
.. "\n"
table.insert(lines, '|-')
.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
table.insert(lines, '| style="text-align: right; padding-right: 0.25em;" | ' .. header)
.. text
table.insert(lines, '| style="background-color: white; padding-left: 0.25em; font-weight: bold;" | ' .. data)
.. "\n"
elseif #entry == 1 then
return table.concat(lines, "\n")
local text = entry[1]
elseif header and not data then
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
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
end
-- Produces a header or footer row
 
function header_footer_row(row_content)
-- Multisection multilink infobox
local lines = {}
-- The following must be passed in:
table.insert(lines, '|-')
-- - Title (self-explanatory)
table.insert(lines, '| colspan="2" style="text-align: center; font-size: 0.8em;" | ' .. row_content)
-- - Sections (see comments for details)
-- - Adjacent links; supports 0, 2, or 8 adjacent links
return table.concat(lines, "\n")
function p.build_multisection(title, sections, adjacent_links)
end
-- Boilerplate stuff
local s = '<div style="\n'
-- Nested helper function
.. "border: 1px solid #999;\n"
-- Produces the navbar, if specified
.. "margin: 0;\n"
function navbar_row()
.. "margin-left: 1em;\n"
local lines = {}
.. "margin-bottom: 0.5em;\n"
table.insert(lines, '|-')
.. "padding: 0.5em;\n"
table.insert(lines, '| colspan="2" style="text-align: center;" | ' .. navbar(name, "mini", ""))
.. "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;" | '
-- Adjacent links
return table.concat(lines, "\n")
-- 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]
-- - Link 1 is previous, link 2 is next
-- If there are eight adjacent links (such as with mosses), then
-- links surround the title, forming a 3x3 grid as such:
-- [Link 1] [Link 2] [Link 3]
-- [Link 4]  Title  [Link 5]
-- [Link 6] [Link 7] [Link 8]
-- - 1: prev_A, prev_B
-- - 2: prev_B
-- - 3: next_A, prev_B
-- - 4: prev_A, 
-- - 5: next_A, 
-- - 6: prev_A, next_B
-- - 7: next_B, 
-- - 8: next_A, next_B
-- Leaving this field nil defaults to only having the title; individual
-- links, as with "border" cases (eg, nothing comes before 0edo), can be
-- left blank.
if adjacent_links == nil then
s = s .. title
s = s .. '\n'
elseif #adjacent_links == 2 then
local prev_link = adjacent_links[1]
local next_link = adjacent_links[2]
s = s
.. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (prev_link or "")
.. "</td>"
.. '<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_link or "")
.. "</td>"
.. "</tr></table>"
s = s .. '\n'
elseif #adjacent_links == 8 then
s = s .. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[1] or "")
.. '</td>'
.. '<td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[2] or "")
.. '\n</td><td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[3] or "")
.. '</td>'
.. '\n<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[4] or "")
.. '</td>'
.. '<td style="width: 50%; padding-left: 1em; padding-right: 1em; text-align: center">'
.. title
.. '</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[5] or "")
.. '</td>'
.. '</tr>'
.. '<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[6] or "")
.. '</td><td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[7] or "")
.. '\n</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[8] or "")
.. '</td>'
.. '</tr>'
..  '</table>'
s = s .. '\n'
else
s = s .. title
s = s .. '\n'
end
end
-- Build up sections
-- Start of infobox; outer div and start of table
-- In code, sections are built up as an array of pairs, coded as such:
local lines = {}
-- {{"Header", entries}, {"Header", entries}}
-- Each set of entries is also built up as an array of pairs, as such:
-- Infobox boilerplate
-- {{"Caption 1", "Text 1"}, {"Caption 2", "Text 2"}}
table.insert(lines,  
-- Sections are written in order in which they appear, as are their
[[<div class="infobox" style="
-- respective entries.
border: 1px solid #999;
-- Section headers may be blank, written as ""
margin: 0;
for _, section in ipairs(sections) do
margin-left: 1em;
-- Get the header and entries
margin-bottom: 0.5em;
local header = section[1]
padding: 0.5em;
local entries = section[2]
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
-- Section header
-- Rows
s = s .. '|-\n'
for i = 1, #rows do
    .. '|colspan="2" style="text-align:center;"| <b>' .. (header or "") .. '</b>\n'
table.insert(lines, infobox_row(rows[i]))
   
end
    -- Section entries
    for _, entry in ipairs(entries) do
-- Footer
if #entry > 1 then
if footer_row then
local caption = entry[1]
table.insert(lines, header_footer_row(footer_row))
local text = entry[2]
end
s = s
.. "|-\n"
-- Template navbar
.. '| style="text-align:right; padding-right: 0.25em" | '
if name then
.. caption
table.insert(lines, navbar_row())
.. "\n"
.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
.. text
.. "\n"
elseif #entry == 1 then
local text = entry[1]
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
end
end
end
end
-- End of infobox
-- End of infobox
s = s .. "|}</div>"
table.insert(lines, "|}")
return s
table.insert(lines, "</div>")
 
return table.concat(lines, "\n")
end
end


-- Multilink infobox
-- Function to be #invoke'd
-- The following must be passed in:
-- Wrapper function for template-based infoboxes
-- - Title (self-explanatory)
-- Modeled off of navbox
-- - Sections (see comments for details)
function p.infobox(frame)
-- - Adjacent links; supports 0, 2, or 8 adjacent links
local args = getArgs(frame)
function p.build_multilink(title, entries, adjacent_links)
-- Boilerplate stuff
-- Preprocess adjacent links
local s = '<div style="\n'
-- If there are two adjacent links (such as with edos), then links will be
.. "border: 1px solid #999;\n"
-- placed on the left and right of the title:
.. "margin: 0;\n"
.. "margin-left: 1em;\n"
.. "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;" | '
-- 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]
-- [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
-- surround the title in a 3x3 grid:
-- links surround the title, forming a 3x3 grid as such:
-- [Link 1] [Link 2] [Link 3]
-- [Link 1] [Link 2] [Link 3]
-- [Link 4]  Title  [Link 5]
-- [Link 4] Title   [Link 5]
-- [Link 6] [Link 7] [Link 8]
-- [Link 6] [Link 7] [Link 8]
-- - 1: prev_A, prev_B
local function any_keys_present(tbl, keys)
-- - 2: prev_B
for _, key in ipairs(keys) do
-- - 3: next_A, prev_B
if tbl[key] ~= nil then
-- - 4: prev_A, 
return true
-- - 5: next_A, 
end
-- - 6: prev_A, next_B
end
-- - 7: next_B, 
return false
-- - 8: next_A, next_B
-- Leaving this field nil defaults to only having the title; individual
-- links, as with "border" cases (eg, nothing comes before 0edo), can be
-- left blank.
if adjacent_links == nil then
s = s .. title
s = s .. '\n'
elseif #adjacent_links == 2 then
local prev_link = adjacent_links[1]
local next_link = adjacent_links[2]
s = s
.. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (prev_link or "")
.. "</td>"
.. '<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_link or "")
.. "</td>"
.. "</tr></table>"
s = s .. '\n'
elseif #adjacent_links == 8 then
s = s .. '<table style="width: 100%; margin: 0"><tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[1] or "")
.. '</td>'
.. '<td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[2] or "")
.. '\n</td><td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[3] or "")
.. '</td>'
.. '\n<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[4] or "")
.. '</td>'
.. '<td style="width: 50%; padding-left: 1em; padding-right: 1em; text-align: center">'
.. title
.. '</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[5] or "")
.. '</td>'
.. '</tr>'
.. '<tr>'
.. '<td style="width: 15%; text-align: left; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[6] or "")
.. '</td><td style="\n'
.. 'width: 50%; padding-left: 1em; padding-right: 1em; text-align: center; font-size: smaller">'
.. (adjacent_links[7] or "")
.. '\n</td>'
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">'
.. (adjacent_links[8] or "")
.. '</td>'
.. '</tr>'
..  '</table>'
s = s .. '\n'
else
s = s .. title
s = s .. '\n'
end
end
-- Add infobox entries
-- Keys to links
-- Entries are entered as a jagged array (array of arrays) where each
-- Left and right are added if at least one is present -> 2-element table.
-- subarray has either one or two entries.
-- All three rows are added if at least one link from the upper or lower
-- These entries form the rows of a two-column table.
-- rows is present, regardless of side links -> 8-element table.
-- One-entry arrays are used for entries that must span both columns,
-- No links are added if none of them are present -> 0-element table.
-- such as a section header. Two-entry arrays are used for the main content.
local upper_links = { "Upper Left Link", "Upper Link", "Upper Right Link" }
for _, entry in ipairs(entries) do
local side_links  = { "Left Link", "Right Link" }
if #entry > 1 then
local lower_links = { "Lower Left Link", "Lower Link", "Lower Right Link" }
local caption = entry[1]
local text = entry[2]
-- Check which links are present
s = s
local is_upper_lower_links_present =
.. "|-\n"
any_keys_present(args, upper_links) or any_keys_present(args, lower_links)
.. '| style="text-align:right; padding-right: 0.25em" | '
local is_side_links_present = any_keys_present(args, side_links)
.. caption
.. "\n"
-- Build adjacent_links
.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold" | '
-- Links from args are removed, as they're stored in a separate table
.. text
local adjacent_links = {}
.. "\n"
if is_upper_lower_links_present then
elseif #entry == 1 then
-- Upper row of links
local text = entry[1]
for _, key in ipairs(upper_links) do
s = s .. "|-\n" .. '| colspan="2" style="text-align: center;" | ' .. text .. "\n"
table.insert(adjacent_links, args[key] or "")
args[key] = nil
end
-- 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
-- Preprocess rows
-- 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.
args["Rows"] = tiu.numbered_header_data_args_to_table(args, 30)
local result = p._infobox(args)
local debugg = yesno(args["debug"])
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
end
-- End of infobox
return result
s = s .. "|}</div>"
end
return s
 
--------------------------------------------------------------------------------
------------------------------- 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