Module:Infobox

From koreapedia
Revision as of 17:31, 8 October 2025 by Wikiuser (talk | contribs) (Created page with "-- Module:Infobox -- Minimal, dependency-free infobox builder that outputs HTML. -- All styling comes from your site CSS classes (e.g., .infobox, .infobox-label). local p = {} local function isNonEmpty(s) if type(s) ~= "string" then return s ~= nil end return mw.text.trim(s) ~= "" end local function cellText(s) if not isNonEmpty(s) then return nil end return mw.text.trim(s) end -- Render an File:... wikitext for the image; callers pass only the filename. local...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- Module:Infobox
-- Minimal, dependency-free infobox builder that outputs HTML.
-- All styling comes from your site CSS classes (e.g., .infobox, .infobox-label).
local p = {}

local function isNonEmpty(s)
	if type(s) ~= "string" then return s ~= nil end
	return mw.text.trim(s) ~= ""
end

local function cellText(s)
	if not isNonEmpty(s) then return nil end
	return mw.text.trim(s)
end

-- Render an [[File:...]] wikitext for the image; callers pass only the filename.
local function renderImage(filename, size)
	if not isNonEmpty(filename) then return nil end
	size = size or "280px"
	return string.format("[[File:%s|%s|center]]", mw.text.trim(filename), size)
end

-- Builds the infobox table using mw.html for safe HTML output.
local function buildTable(args)
	local root = mw.html.create("table"):addClass("infobox")

	-- Title row
	local title = args.title or args.name or mw.title.getCurrentTitle().text
	root:tag("tr")
		:tag("th")
			:attr("colspan", "2")
			:addClass("infobox-title")
			:wikitext(title)

	-- Optional image
	local image = renderImage(args.image, args.image_size)
	if image then
		root:tag("tr")
			:tag("td")
				:attr("colspan", "2")
				:addClass("infobox-image")
				:wikitext(image)
	end

	-- Optional caption
	if isNonEmpty(args.caption) then
		root:tag("tr")
			:tag("td")
				:attr("colspan", "2")
				:addClass("infobox-caption")
				:wikitext(args.caption)
	end

	-- Data rows: look for labelN/dataN pairs
	local maxRows = tonumber(args.maxrows) or 40
	for i = 1, maxRows do
		local label = cellText(args["label" .. i])
		local data  = cellText(args["data"  .. i])
		if label or data then
			-- show the row only if at least one side has content
			local tr = root:tag("tr")
			tr:tag("th"):addClass("infobox-label"):wikitext(label or "")
			tr:tag("td"):addClass("infobox-data") :wikitext(data  or "")
		end
	end

	return tostring(root)
end

function p.build(frame)
	-- Merge parent and current frame args (wrapper templates + direct calls)
	local args = {}
	for k, v in pairs(frame:getParent() and frame:getParent().args or {}) do
		args[k] = v
	end
	for k, v in pairs(frame.args or {}) do
		args[k] = v
	end

	return buildTable(args)
end

return p