Module:Navbox: Difference between revisions
Jump to navigation
Jump to search
Undo revision 212910 by Ganaram inukshuk (talk) rollback for now Tag: Undo |
add back new code, commented out for later fixing |
||
| Line 42: | Line 42: | ||
table.insert(row, data_style .. row_content[2]) | table.insert(row, data_style .. row_content[2]) | ||
end | end | ||
-- New code, commented out for later bugfixing | |||
--[[ | |||
table.insert(row, "|-") | |||
local header = row_content["Header"] | |||
local data = row_content["Data"] | |||
if is_data_navbox then | |||
-- Row content contains a nested navbox... | |||
if header and data then | |||
-- ...and it's a header-data pair. | |||
table.insert(row, string.format('! style="white-space: nowrap; font-size: 0.9em; width: 5%%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" | %s', header)) | |||
table.insert(row, string.format('| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ', data)) | |||
elseif data and not header then | |||
-- ...and it's a headerless data row. | |||
table.insert(row, string.format('| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | %s', data)) | |||
elseif header and not data then | |||
-- ...and it's a dataless header row; whole row becomes a header | |||
-- that can be used as a separator. | |||
end | |||
else | |||
-- Row content is normal content (reglar text and/or links)... | |||
if header and data then | |||
-- ...and it's a header-data pair | |||
table.insert(row, string.format('! style="white-space: nowrap; font-size: 0.9em; width: 5%%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" | %s', header)) | |||
table.insert(row, string.format('| style="padding: 0;" | ', data)) | |||
elseif data and not header then | |||
-- ...and it's a headerless data row | |||
table.insert(row, '| style="padding: 0;" colspan="2" |' .. data) | |||
elseif header and not data then | |||
-- ...and it's a dataless header row | |||
end | |||
end | |||
]]-- | |||
return table.concat(row, "\n") | return table.concat(row, "\n") | ||
Revision as of 20:57, 12 October 2025
- This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Navbox, or used directly from other modules.
Module:Navbox is a module that implements the {{Navbox}} template. Navbox templates can be made by using the template or by calling the _navbox function from another module.
On templates, you can create a navbox by using {{Navbox}}, which calls this module's wrapper function.
local navbox = require("Module:Navbox")._navbox to create a navbox.
| Introspection summary for Module:Navbox | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local getArgs = require("Module:Arguments").getArgs
local navbar = require("Module:Navbar")._navbar
local yesno = require("Module:Yesno")
local p = {}
-- TODO (low priority): add an option for a dataless header row, to mirror the
-- option of having a headerless data row. Requires changing row content from
-- a regular array to an associative array.
-- TODO (medium priority): nest and refactor helper functions
-- Navbox row helper function
-- Function was refactored to use table.concat(), like with the "main" function,
-- since it's being called multiple times. The other helper functions don't need
-- to be refactored (currently) because they only need to be called a fixed
-- number of times.
function p.navbox_row(row_content, is_navbox)
local is_navbox = (is_navbox ~= nil and is_navbox or false)
local row = {}
table.insert(row, "|-")
if #row_content == 1 then
-- Row is a headerless row...
if is_navbox then
-- ...and row content is a nested navbox
table.insert(row, '| style="padding: 0;" colspan="2" |')
table.insert(row, row_content[1])
else
-- ... and row content is normal content (text and links)
table.insert(row, '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1])
end
else
-- Row is a header+data row
local header_style = '! style="white-space: nowrap; font-size: 0.9em; width: 5%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" |'
local data_style = is_navbox
and '| style="padding: 0;" |' -- For nested navboxes
or '| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ' -- For normal content
table.insert(row, header_style .. row_content[1])
table.insert(row, data_style .. row_content[2])
end
-- New code, commented out for later bugfixing
--[[
table.insert(row, "|-")
local header = row_content["Header"]
local data = row_content["Data"]
if is_data_navbox then
-- Row content contains a nested navbox...
if header and data then
-- ...and it's a header-data pair.
table.insert(row, string.format('! style="white-space: nowrap; font-size: 0.9em; width: 5%%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" | %s', header))
table.insert(row, string.format('| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ', data))
elseif data and not header then
-- ...and it's a headerless data row.
table.insert(row, string.format('| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | %s', data))
elseif header and not data then
-- ...and it's a dataless header row; whole row becomes a header
-- that can be used as a separator.
end
else
-- Row content is normal content (reglar text and/or links)...
if header and data then
-- ...and it's a header-data pair
table.insert(row, string.format('! style="white-space: nowrap; font-size: 0.9em; width: 5%%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" | %s', header))
table.insert(row, string.format('| style="padding: 0;" | ', data))
elseif data and not header then
-- ...and it's a headerless data row
table.insert(row, '| style="padding: 0;" colspan="2" |' .. data)
elseif header and not data then
-- ...and it's a dataless header row
end
end
]]--
return table.concat(row, "\n")
end
-- Header/footer row helper function
-- Called up to twice on a table.
function p.navbox_header_footer(row_content)
local row = "|-\n"
.. '| colspan="2" style="font-size: 0.8em; text-align: center; background-color: #eaecf0; padding: 0em; border: 1px solid white;" | '
.. row_content --.. "\n"
return row
end
-- Navbox title helper function
-- Up to one title helper is called per table; this is expected to be called in
-- most cases, however.
function p.navbox_title(title, is_collapsible, name)
local is_root_navbox = (is_root_navbox == nil and is_root_navbox or true) -- If not specified, default to TRUE
local has_navbar = name ~= nil
local navbox_title = ''
if title ~= nil then
navbox_title = "|-\n"
.. '! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;'
.. 'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | '
.. '<span style="display: inline-block; float: left; text-align: left; font-weight: normal; font-style: normal; min-width: 4em; padding: 0px; margin: 0px;">'
.. (has_navbar and navbar(name, "mini", "") or "") .. '</span>'
.. '<span style="font-size: 1.05em;">' .. title .. "</span>"
.. (is_collapsible and '' or '<span style="display: inline-block; float: right; font-size: 0.8em; width: 5em;"> </span>')
--.. '\n'
end
return navbox_title
end
-- Navbox title for nested navboxes or subheader (subcategory) navboxes
-- Up to one title helper is called per table; not always necessray if the table
-- is used for subcategories.
function p.nested_navbox_title(title, is_collapsible)
local navbox_title = ''
if title ~= nil then
navbox_title = "|-\n"
.. '! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;'
.. 'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | '
.. (is_collapsible and '<span style="display: inline-block; float: left; min-width: 4em; padding: 0px; margin: 0px;"> </span>' or '')
.. '<span style="font-size: 0.9em;">' .. title .. "</span>"
--.. '\n'
end
return navbox_title
end
-- "Main" function
-- Navbox to be called by other modules; also called by wrapper function
function p._navbox(args)
local title = args["Title"]
local name = args["name"]
local rows = args["Rows"]
local is_data_navbox = args["Is Data Navbox"]
local is_collapsible = yesno(args["Is Collapsible"], true )
local is_collapsed = yesno(args["Is Collapsed" ], false)
local navbox_type = ((args["Navbox Type"] == nil) and "Normal" or args["Navbox Type"])
local header_row = args["Header Row"]
local footer_row = args["Footer Row"]
-- Table that holds individual lines
local navbox = {}
if navbox_type == "Nested" then
-- Build a navbox that lies within another navbox
table.insert(navbox, '<div class="wikitable" style="overflow-x: auto; padding: 0; margin: 2px;">')
table.insert(navbox,
'{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"'
.. (is_collapsible and (' class="mw-collapsible' .. (is_collapsed and ' mw-collapsed"' or '"')) or '')
)
-- Title row
table.insert(navbox, p.nested_navbox_title(title, is_collapsible))
-- Header row
if header_row then
table.insert(navbox, p.navbox_header_footer(header_row))
end
-- Data rows
for i = 1, #rows do
table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
end
-- Footer rows
if footer_row then
table.insert(navbox, p.navbox_header_footer(footer_row))
end
-- End of table
table.insert(navbox, '|}')
table.insert(navbox, '</div>')
elseif navbox_type == "Subheader" then
-- Build a navbox that lies within another navbox
-- This one serves as subcategories for one row
table.insert(navbox, '{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"')
-- Title (optional)
table.insert(navbox, p.nested_navbox_title(title, false))
-- Rows
for i = 1, #rows do
table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
end
-- End of table
table.insert(navbox, '|}')
else -- Normal
table.insert(navbox, '<div class="wikitable" style="overflow-x: auto; padding: 0">')
table.insert(navbox,
'{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"'
.. (is_collapsible and (' class="mw-collapsible' .. (is_collapsed and ' mw-collapsed"' or '"')) or '')
)
-- Title
table.insert(navbox, p.navbox_title(title, is_collapsible, name))
-- Header
if header_row then
table.insert(navbox, p.navbox_header_footer(header_row))
end
-- Rows
for i = 1, #rows do
table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
end
-- Footer
if footer_row then
table.insert(navbox, p.navbox_header_footer(footer_row))
end
-- End of table
table.insert(navbox, '|}')
table.insert(navbox, '</div>')
end
return table.concat(navbox, '\n')
end
-- Wrapper function for template-based navboxes
function p.navbox(frame)
local args = getArgs(frame)
-- Preprocess individual entries for, headers, data, and is-row-child into
-- one single table.
local rows = {}
local is_data_navbox = {}
for i = 1, 30 do
local header = args["Header " .. i]
local data = args["Data " .. i]
local is_navbox = args["Is Data " .. i .. " Navbox"] ~= nil and args["Is Data " .. i .. " Navbox"] or false
local row = nil
if (header == nil and data ~= nil) then
-- Headerless row
row = {}
table.insert(row, data)
elseif (header ~= nil and data == nil) then
-- Dataless row; permitted for placeholder purposes
row = {}
table.insert(row, header)
table.insert(row, "")
elseif (header ~= nil and data ~= nil) then
-- Row with header and data
row = {}
table.insert(row, header)
table.insert(row, data)
end
if row ~= nil then
table.insert(rows, row)
table.insert(is_data_navbox, is_navbox)
end
-- Remove original entries as cleanup
args["Header " .. i] = nil
args["Data " .. i] = nil
args["Is Data " .. i .. " Navbox"] = nil
end
args["Rows"] = rows
args["Is Data Navbox"] = is_data_navbox
local result = p._navbox(args)
-- Debugger option
local debugg = yesno(args["debug"])
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return result
end
return p