Module:Infobox: Difference between revisions

ArrowHead294 (talk | contribs)
mNo edit summary
Sintel (talk | contribs)
Add infobox class
 
(38 intermediate revisions by 2 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 getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:Yesno")
local navbar  = require("Module:Navbar")._navbar
local tiu    = require("Module:Template input utils")
local yesno   = require("Module:Yesno")
 
local p = {}
local p = {}


--------------------------------------------------------------------------------
-- TODO (medium priority):
------------------------------- HELPER FUNCTIONS -------------------------------
-- - Use templatestyles
--------------------------------------------------------------------------------
 
-- Infobox header with adjacent links
-- Adjacent links are placed on the sides of the title, with options for 0, 2,
-- or 8 links.
function p.infobox_header(title, adjacent_links)
local header = ''
if adjacent_links == nil then
header = title
elseif #adjacent_links == 2 then
header = header
.. "\n"
.. '{| style="width: 100%;"\n'
.. '|-\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[1] .. '\n'
.. '| style="width: 50%;" | ' .. title .. '\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[2] .. '\n'
.. '|}\n'
elseif #adjacent_links == 8 then
header = header
.. "\n"
.. '{| style="width: 100%;"\n'
.. '|-\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[1] .. '\n'
.. '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[2] .. '\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[3] .. '\n'
.. '|-\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[4] .. '\n'
.. '| style="width: 50%;"  | ' .. title .. '\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[5] .. '\n'
.. '|-\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[6] .. '\n'
.. '| style="font-size: 0.75em; width: 50%;" | ' .. adjacent_links[7] .. '\n'
.. '| style="font-size: 0.75em;" | ' .. adjacent_links[8] .. '\n'
.. '|}\n'
else
header = title
end
return header
end
 
-- 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 caption = row_content[1]
local text    = row_content[2]
row = row
.. '|-\n'
.. '| style="text-align: right; padding-right: 0.25em;" | ' .. caption .. '\n'
.. '| style="background-color: white; padding-left: 0.25em; font-weight: bold;" | ' .. text .. '\n'
elseif #row_content == 1 then
local text = row_content[1]
row = row
.. '|-\n'
.. '| colspan="2" style="text-align: center;" | ' .. text .. '\n'
end
return row
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 73: Line 16:
-- Function to be called by other modules; also called by wrapper function
-- Function to be called by other modules; also called by wrapper function
function p._infobox(args)
function p._infobox(args)
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 rows = args["Rows"]
local header_row = args["Header Row"]
local debugg = yesno(args["debug"])
local rows       = args["Rows"]
local footer_row = args["Footer Row"]
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
-- 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
-- Nested helper function
-- Produces a row in the infobox
function infobox_row(row_content)
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 = {}
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
-- 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
-- Start of infobox; outer div and start of table
local infobox = '<div style="'
local lines = {}
.. 'border: 1px solid #999; '
.. 'margin: 0; '
-- Infobox boilerplate
.. 'margin-left: 1em; '
table.insert(lines,
.. 'margin-bottom: 0.5em; '
[[<div class="infobox" style="
.. 'padding: 0.5em; '
border: 1px solid #999;  
.. 'background-color: #f0f0f0; '
margin: 0;  
.. 'min-width: 15em; '
margin-left: 1em;  
.. 'float: right; '
margin-bottom: 0.5em;  
.. 'max-width: 100%; '
padding: 0.5em;  
.. 'overflow: auto;">\n'
background-color: #f0f0f0;  
min-width: 15em;  
-- Infobox table, starting with table
float: right;  
.. '{| style="border-collapse: collapse; width: 100%;"\n'
max-width: 100%;  
.. '|+ style="font-size: 105%; font-weight: bold; text-align: center;" | ' .. p.infobox_header(title, adjacent_links) .. '\n'
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
-- For loop for populating entries
-- Rows
for i = 1, #rows do
for i = 1, #rows do
infobox = infobox .. 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
infobox = infobox .. "|}</div>"
-- Template navbar
if debugg == true then
if name then
infobox = "<syntaxhighlight lang=\"wikitext\">" .. infobox .. "</syntaxhighlight>"
table.insert(lines, navbar_row())
end
end
return infobox
-- End of infobox
table.insert(lines, "|}")
table.insert(lines, "</div>")
 
return table.concat(lines, "\n")
end
end


Line 118: 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]
-- - 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 links
-- surround the title in a 3x3 grid:
-- surround the title in a 3x3 grid:
Line 124: Line 192:
-- [Link 4]  Title  [Link 5]
-- [Link 4]  Title  [Link 5]
-- [Link 6] [Link 7] [Link 8]
-- [Link 6] [Link 7] [Link 8]
-- Individual links can be omitted. If at least one upper or lower link is
local function any_keys_present(tbl, keys)
-- present, then rows for those two sets of links are added. Having no links
for _, key in ipairs(keys) do
-- defaults to having only the title.
if tbl[key] ~= nil then
local is_upper_lower_links_present = args["Upper Left Link"] ~= nil or args["Upper Link"] ~= nil or args["Upper Right Link"] ~= nil
return true
or args["Lower Left Link"] ~= nil or args["Lower Link"] ~= nil or args["Lower Right Link"] ~= nil
end
local is_side_links_present  = args["Left Link"] ~= nil or args["Right Link"] ~= nil
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 Left Link" ] or "")
-- Upper row of links
table.insert(adjacent_links, args["Upper Link"      ] or "")
for _, key in ipairs(upper_links) do
table.insert(adjacent_links, args["Upper Right Link"] or "")
table.insert(adjacent_links, args[key] or "")
end
args[key] = nil
if is_side_links_present then
end
table.insert(adjacent_links, args["Left Link" ] or "")
-- Middle row of links
table.insert(adjacent_links, args["Right Link"] or "")
for _, key in ipairs(side_links) do
end
table.insert(adjacent_links, args[key] or "")
if is_upper_lower_links_present then
args[key] = nil
table.insert(adjacent_links, args["Lower Left Link" ] or "")
end
table.insert(adjacent_links, args["Lower Link"      ] or "")
-- Bottom row of links
table.insert(adjacent_links, args["Lower Right Link"] or "")
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
-- Cleanup individual links, as they've been combined into one table
args["Upper Left Link" ] = nil
args["Upper Link"      ] = nil
args["Upper Right Link"] = nil
args["Left Link"      ] = nil
args["Right Link"      ] = nil
args["Lower Left Link" ] = nil
args["Lower Link"      ] = nil
args["Lower Right Link"] = nil
-- 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.
local rows = {}
args["Rows"] = tiu.numbered_header_data_args_to_table(args, 30)
for i = 1, 30 do
local header = args["Header " .. i]
local result = p._infobox(args)
local data  = args["Data "  .. i]
local debugg = yesno(args["debug"])
if debugg == true then
local row = nil
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
if (header == nil and data ~= nil) then
row = {}
table.insert(row, data)
elseif (header ~= nil and data ~= nil) then
row = {}
table.insert(row, header)
table.insert(row, data)
end
if row ~= nil then
table.insert(rows, row)
end
-- Remove original entries as cleanup
args["Header " .. i] = nil
args["Data "   .. i] = nil
end
end
args["Rows"] = rows
return p._infobox(args)
return result
end
end


Line 200: 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 211: Line 280:
["Adjacent Links"] = adjacent_links,
["Adjacent Links"] = adjacent_links,
["Title"] = title,
["Title"] = title,
["Rows"] = entries,
["Rows"] = entries
}
}