Module:Sidebar

From Xenharmonic Wiki
Revision as of 12:09, 11 October 2025 by Ganaram inukshuk (talk | contribs)
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]
This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Sidebar, or used directly from other modules.

Module:Sidebar is a module that implements the {{Sidebar}} template. Sidebar templates can be made by using the template or by calling the _sidebar function from another module.

On templates, you can create a sidebar by using {{Sidebar}}, which calls this module's wrapper function.

On modules, you can include local sidebar = require("Module:Sidebar")._sidebar to create a sidebar.
Introspection summary for Module:Sidebar 
Functions provided (2)
Line Function Params
13 _sidebar (main) (args)
69 sidebar (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
getArgs Module:Arguments getArgs

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


local getArgs = require("Module:Arguments").getArgs

local p = {}

-- Not to be confused with the infobox; the sidebar only has one column and is a
-- different color. Some infobox-ish templates use that style

-- Not to be confused with the sidebox either.

-- TODO: add float option?

-- "Main" function
function p._sidebar(args)
	local title  = args["Title" ]
	local header = args["Header"]
	local rows   = args["Rows"  ]
	local footer = args["Footer"]
	
	-- Helper function; title
	function sidebar_title()
		local lines = {}
		table.insert(lines, "|-")
		table.insert(lines, string.format("! %s", title))
		return table.concat(lines, "\n")
	end
	
	-- Helper function; row
	function sidebar_row(row_content)
		local lines = {}
		table.insert(lines, "|-")
		table.insert(lines, string.format("| %s", row_content))
		return table.concat(lines, "\n")
	end
	
	-- Helper function; header/footer
	function header_footer_row(row_content)
		local lines = {}
		table.insert(lines, "|-")
		table.insert(lines, string.format('| style="font-size: 80%%;" | %s', row_content))
		return table.concat(lines, "\n")
	end
	
	-- Boilerplate and start of table
	local lines = {}
	table.insert(lines, '<div class="toccolours" style="float: right; margin: 0px 0px 4px 4px; text-align: center">')
	table.insert(lines, '{| class="none"')
	table.insert(lines, "|-")
	
	-- Title
	if title then table.insert(lines, sidebar_title()) end
	
	-- Header
	if header then table.insert(lines, header_footer_row(header)) end
	
	-- Rows
	for i = 1, #rows do table.insert(lines, sidebar_row(rows[i])) end
	
	-- Footer
	if footer then table.insert(lines, header_footer_row(footer)) end
	
	-- End of table
	table.insert(lines, "|}")
	table.insert(lines, "</div>")
	
	return table.concat(lines, "\n")
end

-- Wrapper function
function p.sidebar(frame)
	local args = getArgs(frame)
	
	-- Preprocess rows
	local rows = {}
	for i = 1, 20 do
		local key = string.format("Row %d", i)
		local row = args[key]
		if row then table.insert(rows, row) end
		args[key] = nil
	end
	args["Rows"] = rows
	
	return frame:preprocess(p._sidebar(args))
end

return p