Module:Navbox: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
implement navbox nesting; some cleanup
Ganaram inukshuk (talk | contribs)
No edit summary
Line 1: Line 1:
-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
-- Loosely modeled off of Runescape Wiki's navbox, not Wikipedia's
-- Loosely modeled off of Runescape Wiki's navbox, not Wikipedia's
local getArgs = require('Module:Arguments').getArgs
local getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:Yesno")
local p = {}
local p = {}


-- Navbox row
-- Navbox row
-- The <td> cell has a div that sets padding for row content; this is omitted if
function p.navbox_row(row_content)
-- the row content is a child navbox.
local row_header    = row_content["Header"]
function p.navbox_row(row_content, is_row_child_navbox)
local row_data      = row_content["Data"]
local row_content = row_content or { "Header", "Content" }
local is_row_navbox = row_content["Is Row Navbox"]
local is_row_child_navbox = is_row_child_navbox or false -- Not implemented


local row = '<tr>\n'
local row = '<tr>\n'
if #row_content == 1 then
if row_header == nil then
-- Headerless row
-- Headerless row
if is_row_child_navbox then
if is_row_navbox then
-- Row data is a child navbox
-- Row data is a child navbox
row = row
row = row
Line 27: Line 27:
    .. '</td>\n'
    .. '</td>\n'
     end
     end
elseif #row_content == 2 then
else
-- Simple row with header and data
-- Simple row with header and data
if is_row_child_navbox then
if is_row_navbox then
row = row
row = row
.. '<th style="width:5%; text-align:right; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_content[1] .. '</th>\n'
.. '<th style="width:5%; text-align:right; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_content[1] .. '</th>\n'
Line 75: Line 75:
local is_child_navbox = args["Is Child Navbox"]
local is_child_navbox = args["Is Child Navbox"]
local rows            = args["Rows"]
local rows            = args["Rows"]
local row_is_subtable = args["Is Row Child Navbox"] -- Not implemented
local is_collapsible  = args["Is Collapsible"] -- Not implemented
local is_collapsible  = args["Is Collapsible"] -- Not implemented
local is_collapsed    = args["Is Collapsible"] -- Not working?
local is_collapsed    = args["Is Collapsible"] -- Not working?
Line 97: Line 96:
function p.navbox(frame)
function p.navbox(frame)
local args = getArgs(frame)
local args = getArgs(frame)
-- Prepropress boolean args
args["Is Collapsible" ] = yesno(args["Is Collapsible" ])
args["Is Collapsible" ] = yesno(args["Is Collapsible" ])
args["Is Child Navbox"] = yesno(args["Is Child Navbox"])
-- Preprocess individual entries for, headers, data, and is-row-child into
-- Preprocess individual entries for, headers, data, and is-row-child into
-- two separate tables.
-- one single table.
-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that
-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that.
local rows = {}
local rows = {}
local is_row_child_navbox = {}
for i = 1, 20 do
for i = 1, 20 do
local row = {}
local header = args["Header " .. i]
local header = args["Header " .. i]
local data  = args["Data "  .. i]
local data  = args["Data "  .. i]
local is_child_navbox = args["Is Row Child Navbox" .. i]
local is_navbox = yesno(args["Is Data " .. i .. " Navbox"])
if header ~= nil and data ~= nil then
local row = nil
-- If there's both a header data, add both
if (header ~= nil or data ~= nil or is_navbox ~= nil) then
table.insert(row, header)
row = {
table.insert(row, data)
["Header"] = header,
elseif header == nil and data ~= nil then
["Data"] = data,
-- Headerless row
["Is Navbox"] = is_navbox
table.insert(row, data)
}
elseif header ~= nil and data == nil then
-- Dataless row with header; this is allowed
table.insert(row, header)
table.insert(row, "")
end
end
if row ~= nil then
if #row >= 1 then
table.insert(rows, row)
table.insert(rows, row)
end
end
if is_child_navbox ~= nil then
-- Remove original entries as cleanup
table.insert(is_row_child_navbox, is_child_navbox)
end
args["Header " .. i] = nil
args["Header " .. i] = nil
args["Data "  .. i] = nil
args["Data "  .. i] = nil
args["Is Row Child Navbox" .. i] = nil
args["Is Data " .. i .. " Navbox"] = nil
end
end
args["Rows"] = rows
args["Rows"] = rows
args["Is Row Child Navbox"] = is_row_child_navbox
return p._navbox(args)
return p._navbox(args)

Revision as of 10:56, 23 November 2024

Module documentation[view] [edit] [history] [purge]
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.

On modules, you can include local navbox = require("Module:Navbox")._navbox to create a navbox.


Introspection summary for Module:Navbox 
Functions provided (4)
Line Function Params
8 navbox_row (row_content)
53 navbox_title (title, is_collapsible, is_collapsed)
73 _navbox (main) (args)
96 navbox (invokable) (frame)
Lua modules required (2)
Variable Module Functions used
getArgs Module:Arguments getArgs
yesno Module:Yesno yesno

No function descriptions were provided. The Lua code may have further information.


-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
-- Loosely modeled off of Runescape Wiki's navbox, not Wikipedia's
local getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:Yesno")
local p = {}

-- Navbox row
function p.navbox_row(row_content)
	local row_header    = row_content["Header"]
	local row_data      = row_content["Data"]
	local is_row_navbox = row_content["Is Row Navbox"]

	local row = '<tr>\n'
	if row_header == nil then
		-- Headerless row
		if is_row_navbox then
			-- Row data is a child navbox
			row = row
				.. '<td style="width:5%; text-align:center; background-color:#eaecf0; white-space:nowrap; padding:0em; border:1px solid white" colspan="2">\n'
	        	.. row_content[1]
	    		.. '</td>\n'
		else
			-- Row data is normal data
			row = row
				.. '<td style="width:5%; text-align:center; background-color:#eaecf0; white-space:nowrap; padding:0em; border:1px solid white" colspan="2">\n'
	        	.. '<div style="padding:0.25em 0.5em">' .. row_content[1] .. '</div>\n'
	    		.. '</td>\n'
    	end
	else
		-- Simple row with header and data
		if is_row_navbox then
			row = row
			.. '<th style="width:5%; text-align:right; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_content[1] .. '</th>\n'
			.. '<td style="padding:0em">\n'
			.. row_content[2]
			.. '</td>\n'
		else
			row = row
				.. '<th style="width:5%; text-align:right; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_content[1] .. '</th>\n'
				.. '<td style="padding:0em">\n'
				.. '<div style="padding:0.25em 0.5em">' .. row_content[2] .. '</div>\n'
				.. '</td>\n'
		end
	end
	local row = row .. '</tr>\n'
	
	return row
end

-- Navbox title
-- A title is added if it's provided.
-- Child navboxes used for subcategories don't need a title or collapse options.
function p.navbox_title(title, is_collapsible, is_collapsed)
	local is_collapsible = is_collapsible or true		-- Not implemented
	local is_collapsed = is_collapsed or true			-- Not working?
	
	local navbox_title = ''
	if title == nil then
		navbox_title = '<table style="width:100%; border-spacing:0px">\n'
	else
		navbox_title = navbox_title
			.. '<div class="wikitable">'
			.. '<table class="mw-collapsible' .. (is_collapsed and ' mw-collapsed ' or '') .. 'nowraplinks" style="width: 100%; border-spacing:0px">\n'
			.. '<tr>\n'
			.. '<th style="width:5%; text-align:center; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white" colspan="2"><b>' .. title .. '</b></th>\n'
			.. '</tr>\n'
	end
	
	return navbox_title
end

-- Navbox to be called by other modules; also called by wrapper function
function p._navbox(args)
	local title           = args["Title"]
	local is_child_navbox = args["Is Child Navbox"]
	local rows            = args["Rows"]
	local is_collapsible  = args["Is Collapsible"]			-- Not implemented
	local is_collapsed    = args["Is Collapsible"]			-- Not working?
	
	-- Start of table
	local navbox = p.navbox_title(title, is_collapsible, is_collapsed, is_subtable)
	
	for i = 1, #rows do
		navbox = navbox .. p.navbox_row(rows[i])
	end
	
	navbox = navbox
		.. '</table>\n'
		.. '</div>'
	
	return navbox
end

-- Navbox to be #invoke'd
-- Wrapper function for template-based navboxes
function p.navbox(frame)
	local args = getArgs(frame)
	
	-- Prepropress boolean args
	args["Is Collapsible" ] = yesno(args["Is Collapsible" ])
	args["Is Collapsible" ] = yesno(args["Is Collapsible" ])
	args["Is Child Navbox"] = yesno(args["Is Child Navbox"])
	
	-- Preprocess individual entries for, headers, data, and is-row-child into
	-- one single table.
	-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that.
	local rows = {}
	for i = 1, 20 do
		local header = args["Header " .. i]
		local data   = args["Data "   .. i]
		local is_navbox = yesno(args["Is Data " .. i .. " Navbox"])
		
		local row = nil
		if (header ~= nil or data ~= nil or is_navbox ~= nil) then
			row = {
				["Header"] = header,
				["Data"] = data,
				["Is Navbox"] = is_navbox
			}
		end
		if row ~= nil then
			table.insert(rows, row)
		end
		
		-- Remove original entries as cleanup
		args["Header " .. i] = nil
		args["Data "   .. i] = nil
		args["Is Data " .. i .. " Navbox"] = nil
	end
	args["Rows"] = rows
	
	return p._navbox(args)
	
end

return p