Module:Infobox

Revision as of 06:50, 22 November 2026 by Nota (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Infobox/doc

local p = {}

local function isEmpty(val)
	return val == nil or mw.text.trim(tostring(val)) == ''
end

local function mergeArgs(frame)
	local args = {}
	local parent = frame:getParent()
	if parent and parent.args then
		for k, v in pairs(parent.args) do
			if not isEmpty(v) then
				args[k] = mw.text.trim(tostring(v))
			end
		end
	end
	if frame.args then
		for k, v in pairs(frame.args) do
			if not isEmpty(v) then
				args[k] = mw.text.trim(tostring(v))
			end
		end
	end
	return args
end

local function collectIndices(args, prefixes)
	local seen = {}
	local indices = {}
	for k, _ in pairs(args) do
		if type(k) == 'string' then
			for _, prefix in ipairs(prefixes) do
				local numStr = k:match('^' .. prefix .. '(%d+)$')
				if numStr then
					local num = tonumber(numStr)
					if num and not seen[num] then
						seen[num] = true
						table.insert(indices, num)
					end
				end
			end
		end
	end
	table.sort(indices)
	return indices
end

function p.infobox(frame)
	local args = mergeArgs(frame)

	local defaultBodyStyle = 'width: 22em; float: right; clear: right; margin: 0.5em 0 1em 1em; border: 1px solid #a2a9b1; background-color: #f8f9fa; color: black; font-size: 88%; line-height: 1.5em; border-collapse: collapse;'
	local tbl = mw.html.create('table')
		:addClass('infobox')
		:cssText(defaultBodyStyle)

	if args.bodyclass then
		tbl:addClass(args.bodyclass)
	end
	if args.bodystyle then
		tbl:cssText(args.bodystyle)
	end

	-- Title (rendered as caption or top header row)
	if args.title then
		tbl:tag('tr')
			:tag('th')
				:attr('colspan', 2)
				:addClass(args.titleclass)
				:cssText('text-align: center; font-size: 125%; font-weight: bold; padding: 0.4em;')
				:cssText(args.titlestyle)
				:wikitext(args.title)
	end

	-- Above
	if args.above then
		tbl:tag('tr')
			:tag('th')
				:attr('colspan', 2)
				:addClass(args.aboveclass)
				:cssText('text-align: center; font-size: 125%; font-weight: bold; padding: 0.4em;')
				:cssText(args.abovestyle)
				:wikitext(args.above)
	end

	-- Subheaders (unnumbered + numbered)
	local subIndices = collectIndices(args, { 'subheader' })
	if args.subheader then
		table.insert(subIndices, 1, 0)
	end
	for _, idx in ipairs(subIndices) do
		local key = idx == 0 and 'subheader' or ('subheader' .. idx)
		if args[key] then
			tbl:tag('tr')
				:tag('th')
					:attr('colspan', 2)
					:addClass(args.subheaderclass)
					:cssText('text-align: center; font-weight: normal; padding: 0.2em 0.4em;')
					:cssText(args.subheaderstyle)
					:cssText(args['subheaderstyle' .. idx])
					:wikitext(args[key])
		end
	end

	-- Images (unnumbered + numbered)
	local imgIndices = collectIndices(args, { 'image' })
	if args.image then
		table.insert(imgIndices, 1, 0)
	end
	for _, idx in ipairs(imgIndices) do
		local imgKey = idx == 0 and 'image' or ('image' .. idx)
		local capKey = idx == 0 and 'caption' or ('caption' .. idx)
		if args[imgKey] then
			local cell = tbl:tag('tr')
				:tag('td')
					:attr('colspan', 2)
					:addClass(args.imageclass)
					:cssText('text-align: center; padding: 0.5em;')
					:cssText(args.imagestyle)
					:cssText(args['imagestyle' .. idx])

			local content = args[imgKey]
			if args[capKey] then
				content = content .. '<br/><small style="' .. (args.captionstyle or '') .. '">' .. args[capKey] .. '</small>'
			end
			cell:wikitext(content)
		end
	end

	-- Data & Header rows
	local rowIndices = collectIndices(args, { 'header', 'label', 'data' })
	for _, idx in ipairs(rowIndices) do
		local header = args['header' .. idx]
		local label = args['label' .. idx]
		local data = args['data' .. idx]
		local rowStyle = args['rowstyle' .. idx]
		local rowClass = args['rowclass' .. idx]

		if header then
			local tr = tbl:tag('tr'):addClass(rowClass)
			tr:tag('th')
				:attr('colspan', 2)
				:addClass(args.headerclass)
				:cssText('text-align: center; padding: 0.4em;')
				:cssText(args.headerstyle)
				:cssText(rowStyle)
				:cssText(args['headerstyle' .. idx])
				:wikitext(header)
		elseif data then
			local tr = tbl:tag('tr'):addClass(rowClass)
			if label then
				tr:tag('th')
					:addClass(args.labelclass)
					:cssText('text-align: left; vertical-align: top; padding: 0.4em; width: 40%;')
					:cssText(args.labelstyle)
					:cssText(rowStyle)
					:cssText(args['labelstyle' .. idx])
					:wikitext(label)
				tr:tag('td')
					:addClass(args.dataclass)
					:addClass(args['class' .. idx])
					:cssText('vertical-align: top; padding: 0.4em;')
					:cssText(args.datastyle)
					:cssText(rowStyle)
					:cssText(args['datastyle' .. idx])
					:wikitext(data)
			else
				tr:tag('td')
					:attr('colspan', 2)
					:addClass(args.dataclass)
					:addClass(args['class' .. idx])
					:cssText('text-align: center; vertical-align: top; padding: 0.4em;')
					:cssText(args.datastyle)
					:cssText(rowStyle)
					:cssText(args['datastyle' .. idx])
					:wikitext(data)
			end
		end
	end

	-- Below
	if args.below then
		tbl:tag('tr')
			:tag('td')
				:attr('colspan', 2)
				:addClass(args.belowclass)
				:cssText('text-align: center; padding: 0.4em;')
				:cssText(args.belowstyle)
				:wikitext(args.below)
	end

	return tostring(tbl)
end

p.main = p.infobox

return p