Module:Infobox: Difference between revisions

From koreapedia
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..."
 
No edit summary
Line 1: Line 1:
-- Module:Infobox
-- Module:Infobox
-- Minimal, dependency-free infobox builder that outputs HTML.
-- Central Lua infobox builder: hides empty rows automatically and supports wrapper templates.
-- All styling comes from your site CSS classes (e.g., .infobox, .infobox-label).
 
local p = {}
local p = {}


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


-- Render an [[File:...]] wikitext for the image; callers pass only the filename.
local function renderImage(filename, size)
local function renderImage(filename, size)
if not isNonEmpty(filename) then return nil end
if not isNonEmpty(filename) then return nil end
Line 21: Line 16:
end
end


-- Builds the infobox table using mw.html for safe HTML output.
-- Builds the table with automatic row hiding
local function buildTable(args)
function p.build(frame)
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
 
local root = mw.html.create("table"):addClass("infobox")
local root = mw.html.create("table"):addClass("infobox")


-- Title row
-- Title
local title = args.title or args.name or mw.title.getCurrentTitle().text
local title = args.title or args.name or mw.title.getCurrentTitle().text
root:tag("tr")
root:tag("tr")
Line 52: Line 55:
end
end


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


function p.build(frame)
-- Helper: given a mapping table, automatically hides empty values
-- Merge parent and current frame args (wrapper templates + direct calls)
function p.smart(frame)
local args = {}
local args = frame:getParent() and frame:getParent().args or {}
for k, v in pairs(frame:getParent() and frame:getParent().args or {}) do
local map  = frame.args -- wrapper-defined label-to-field mapping
args[k] = v
 
end
local newArgs = {}
for k, v in pairs(frame.args or {}) do
local i = 0
args[k] = v
 
for label, key in pairs(map) do
local value = args[key]
if isNonEmpty(value) then
i = i + 1
newArgs["label" .. i] = label
newArgs["data"  .. i] = value
end
end
end


return buildTable(args)
newArgs.title      = args.title or args.name or mw.title.getCurrentTitle().text
newArgs.image      = args.image
newArgs.image_size  = args.image_size
newArgs.caption    = args.caption
 
local fakeFrame = { args = newArgs }
return p.build(fakeFrame)
end
end


return p
return p

Revision as of 17:44, 8 October 2025

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

-- Module:Infobox
-- Central Lua infobox builder: hides empty rows automatically and supports wrapper templates.

local p = {}

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

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 table with automatic row hiding
function p.build(frame)
	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

	local root = mw.html.create("table"):addClass("infobox")

	-- Title
	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

	-- Auto-detect label/data pairs dynamically
	for i = 1, 50 do
		local label = args["label" .. i]
		local data  = args["data"  .. i]
		if isNonEmpty(label) or isNonEmpty(data) then
			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

-- Helper: given a mapping table, automatically hides empty values
function p.smart(frame)
	local args = frame:getParent() and frame:getParent().args or {}
	local map  = frame.args -- wrapper-defined label-to-field mapping

	local newArgs = {}
	local i = 0

	for label, key in pairs(map) do
		local value = args[key]
		if isNonEmpty(value) then
			i = i + 1
			newArgs["label" .. i] = label
			newArgs["data"  .. i] = value
		end
	end

	newArgs.title       = args.title or args.name or mw.title.getCurrentTitle().text
	newArgs.image       = args.image
	newArgs.image_size  = args.image_size
	newArgs.caption     = args.caption

	local fakeFrame = { args = newArgs }
	return p.build(fakeFrame)
end

return p