Difference between revisions of "Module:Basic infobox"

From Pandorabox
Jump to navigation Jump to search
m (allow data-only rows (e.g. for pictures or screenshots))
m (fix text alignment)
 
(2 intermediate revisions by the same user not shown)
Line 22: Line 22:
 
oargs[idx][attr] = body
 
oargs[idx][attr] = body
 
end
 
end
 +
end
 +
if args.above then
 +
node:tag("tr"):tag("th"):attr("colspan", "2"):cssText("background:green"):cssText(args.abovestyle):wikitext(args.above)
 
end
 
end
 
for i = 1, table.maxn(oargs) do
 
for i = 1, table.maxn(oargs) do
Line 30: Line 33:
 
row:tag("th"):attr("colspan", "2"):cssText("background:green"):cssText(rowargs.headerstyle):wikitext(rowargs.header)
 
row:tag("th"):attr("colspan", "2"):cssText("background:green"):cssText(rowargs.headerstyle):wikitext(rowargs.header)
 
elseif rowargs.label and rowargs.data then
 
elseif rowargs.label and rowargs.data then
row:tag("th"):cssText(rowargs.labelstyle):wikitext(rowargs.label)
+
row:tag("td"):cssText("font-weight:bold"):cssText(rowargs.labelstyle):wikitext(rowargs.label)
 
row:tag("td"):cssText(rowargs.datastyle):wikitext(rowargs.data)
 
row:tag("td"):cssText(rowargs.datastyle):wikitext(rowargs.data)
 
elseif rowargs.data then
 
elseif rowargs.data then
row:tag("td"):attr("colspan", "2"):cssText(rowargs.datastyle):wikitext(rowargs.data)
+
row:tag("td"):attr("colspan", "2"):cssText("text-align:center"):cssText(rowargs.datastyle):wikitext(rowargs.data)
 
end
 
end
 
end
 
end
Line 46: Line 49:
 
style="width:fit-content",
 
style="width:fit-content",
 
class="sample-class",
 
class="sample-class",
 +
above="Title",
 
header1="Header",
 
header1="Header",
 
headerstyle1="background-color:green",
 
headerstyle1="background-color:green",

Latest revision as of 18:14, 23 June 2021

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:

Module:Basic infobox
Simple infobox generator
Placeholder.png
 
TypeModule
StatusTesting
Discussion page
Module talk:Basic infobox

--[[
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
	if args.above then
		node:tag("tr"):tag("th"):attr("colspan", "2"):cssText("background:green"):cssText(args.abovestyle):wikitext(args.above)
	end
	for i = 1, table.maxn(oargs) do
		local rowargs = oargs[i]
		if rowargs then
			local row = node:tag("tr")
			if rowargs.header then
				row:tag("th"):attr("colspan", "2"):cssText("background:green"):cssText(rowargs.headerstyle):wikitext(rowargs.header)
			elseif rowargs.label and rowargs.data then
				row:tag("td"):cssText("font-weight:bold"):cssText(rowargs.labelstyle):wikitext(rowargs.label)
				row:tag("td"):cssText(rowargs.datastyle):wikitext(rowargs.data)
			elseif rowargs.data then
				row:tag("td"):attr("colspan", "2"):cssText("text-align:center"):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",
	above="Title",
	header1="Header",
	headerstyle1="background-color:green",
	header2="", -- should not be shown
	label10="Field",
	labelstyle10="color:blue",
	data10="No",
	datastyle10="color:red",
	data11="content-only",
	label12="label with no data", -- should not be shown
}}))
]]