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 | ||
-- | -- Central Lua infobox builder: hides empty rows automatically and supports wrapper templates. | ||
local p = {} | local p = {} | ||
local function isNonEmpty( | local function isNonEmpty(v) | ||
if type( | if v == nil then return false end | ||
return mw.text.trim( | if type(v) ~= "string" then return true end | ||
return mw.text.trim(v) ~= "" | |||
end | end | ||
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 | -- 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") | local root = mw.html.create("table"):addClass("infobox") | ||
-- Title | -- 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 | ||
-- | -- Auto-detect label/data pairs dynamically | ||
for i = 1, 50 do | |||
for i = 1, | local label = args["label" .. i] | ||
local label = | local data = args["data" .. i] | ||
local data = | if isNonEmpty(label) or isNonEmpty(data) then | ||
if label or data then | |||
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 | tr:tag("td"):addClass("infobox-data") :wikitext(data or "") | ||
end | end | ||
end | end | ||
Line 68: | Line 69: | ||
end | end | ||
function p. | -- Helper: given a mapping table, automatically hides empty values | ||
function p.smart(frame) | |||
local args = | local args = frame:getParent() and frame:getParent().args or {} | ||
local map = frame.args -- wrapper-defined label-to-field mapping | |||
local newArgs = {} | |||
for | local i = 0 | ||
args[ | |||
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 | 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