Module:Basic infobox

From Pandorabox
Revision as of 06:16, 23 June 2021 by Ywang (talk | contribs) (Use consistent styling with existing infobox templates)
Jump to navigation Jump to search

This is a simple module that generates infoboxes.

Usage

The above field is the content of the header column at the top of the infobox.

Each row is indexed by a number and can have the following elements:

  • headern: used for header rows
  • labeln: optional label for data rows
  • datan: data for the row (ignored for header rows; hides row when absent or empty otherwise)

Row indices do not have to be continuous.

Styles can be defined with e.g. abovestyle and labelstylen.

Example

{{#invoke:basic infobox|main
| above = [[Module:Basic infobox]]<br/>''Simple infobox generator''
| data1 = [[File:Placeholder.png|frameless|upright=1.0]]
| header2 =  
| headerstyle2 = font-size:0.5em
| label3 = Type
| data3 = Module
| label4 = Status
| data4 = Testing
| datastyle4 = background-color:orange
| header10 = Discussion page
| data11 = [[Module talk:Basic infobox]]
}}

Generates:

 
TypeModule
StatusTesting
Discussion page

--[[
This module implements a basic infobox template.
This is designed to be at least partially compatitable with the one seen on Wikipedia, but is not a replacement of it.
]]

local function main(frame)
	local args = frame.args or error("No argument supplied")
	local style = args.style or ""
	style = "float:right;" .. style
	local tclass = args.class or ""
	tclass = "wikitable "..tclass
	local node = mw.html.create("table"):attr{class = tclass, style = style}
	local i = 1
	local oargs = {} -- organized argument list
	for k, v in pairs(args) do
		local attr, idx = string.match(k, "^(%D+)(%d+)$")
		idx = tonumber(idx)
		local body = v
		if body == "" then body = nil end
		if idx and body then
			if not oargs[idx] then oargs[idx] = {} end
			oargs[idx][attr] = body
		end
	end
	for i = 1, table.maxn(oargs) do
		local rowargs = oargs[i]
		if rowargs then
			local row = node:tag("tr"):cssText("background:green")
			if rowargs.header then
				row:tag("th"):attr("colspan", "2"):cssText(rowargs.headerstyle):wikitext(rowargs.header)
			elseif rowargs.label and rowargs.data then
				row:tag("th"):cssText(rowargs.labelstyle):wikitext(rowargs.label)
				row:tag("td"):cssText(rowargs.datastyle):wikitext(rowargs.data)
			end
		end
	end
	return tostring(node)
end

return {main = main}

--[[ Testing code:
mw.log(p.main(mw.getCurrentFrame():newChild{args = {
	style="width:fit-content",
	class="sample-class",
	header1="Header",
	headerstyle1="background-color:green",
	header2="", -- should not be shown
	label10="Field",
	labelstyle10="color:blue",
	data10="No",
	datastyle10="color:red",
	data11="data with no label", -- should not be shown
	label12="label with no data", -- should not be shown
}}))
]]