Module:Infobox
From koreapedia
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)
-- Defensive: ensure we always have parent args even if getParent() is nil
local parent = nil
if type(frame.getParent) == "function" then
parent = frame:getParent()
end
local args = parent and parent.args or {}
local map = frame.args or {}
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