Module:InfoboxMinimal: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
local p = {} | local p = {} | ||
local function blank(s) return | local function blank(s) | ||
local function get(inv, par, key) return (inv and inv[key]) or (par and par[key]) end | if s == nil then return true end | ||
s = tostring(s) | |||
return mw.text.trim(s) == '' | |||
end | |||
-- Prefer wrapper (#invoke) args first, then parent (template/page) args | |||
local function get(inv, par, key) | |||
return (inv and inv[key]) or (par and par[key]) | |||
end | |||
local function addRow(root, label, data) | local function addRow(root, label, data) | ||
if blank(data) then return end | if blank(data) then return end | ||
local tr = root:tag('tr') | local tr = root:tag('tr') | ||
tr:tag('th'):attr('scope','row'):addClass('infobox-label'):wikitext(label or '') | tr:tag('th') | ||
tr:tag('td'):addClass('infobox-data'):wikitext(data) | :attr('scope','row') | ||
:addClass('infobox-label') | |||
:wikitext(label or '') | |||
tr:tag('td') | |||
:addClass('infobox-data') | |||
:wikitext(data) | |||
end | end | ||
function p.infobox(frame) | function p.infobox(frame) | ||
local inv = frame.args | local inv = frame.args -- args set in {{#invoke:...}} | ||
local par = frame:getParent() and frame:getParent().args or {} -- args from | local par = (frame:getParent() and frame:getParent().args) or {} -- args from calling template/page | ||
local classes = 'infobox' | -- Wrapper table | ||
local classes = 'infobox' | |||
local bodyclass = get(inv, par, 'bodyclass') | local bodyclass = get(inv, par, 'bodyclass') | ||
if not blank(bodyclass) then classes = classes .. ' ' .. bodyclass end | if not blank(bodyclass) then classes = classes .. ' ' .. tostring(bodyclass) end | ||
local root = mw.html.create('table'):addClass(classes):attr('role','presentation') | local root = mw.html.create('table') | ||
:addClass(classes) | |||
:attr('role', 'presentation') | |||
-- Title / caption element | |||
local title = get(inv, par, 'title') or get(inv, par, 'name') or mw.title.getCurrentTitle().baseText | local title = get(inv, par, 'title') or get(inv, par, 'name') or mw.title.getCurrentTitle().baseText | ||
root:tag('caption'):addClass('infobox-title'):wikitext(title) | root:tag('caption'):addClass('infobox-title'):wikitext(title) | ||
-- | -- Optional top image (filename only; no "File:" or "[[]]") | ||
local image = get(inv, par, 'image') | |||
local caption = get(inv, par, 'caption') | |||
local alttext = get(inv, par, 'alt') | |||
local upright = get(inv, par, 'upright') -- e.g. "1.1" | |||
local size = get(inv, par, 'imagesize') -- e.g. "200px" | |||
if not blank(image) then | |||
local opts = { 'frameless', 'center' } | |||
if not blank(upright) then table.insert(opts, 'upright=' .. tostring(upright)) end | |||
if not blank(size) then table.insert(opts, tostring(size)) end | |||
if not blank(alttext) then table.insert(opts, 'alt=' .. tostring(alttext)) end | |||
local imgRow = root:tag('tr') | |||
imgRow:tag('td') | |||
:attr('colspan', 2) | |||
:addClass('infobox-image') | |||
:wikitext(string.format('[[File:%s|%s|%s]]', | |||
tostring(image), | |||
table.concat(opts, '|'), | |||
caption or '' | |||
)) | |||
end | |||
-- Data rows: label1/data1 ... label100/data100 (skips blanks) | |||
for i = 1, 100 do | for i = 1, 100 do | ||
local label = get(inv, par, 'label' .. i) | |||
local data = get(inv, par, 'data' .. i) | |||
addRow(root, label, data) | |||
end | end | ||
Latest revision as of 06:50, 9 November 2025
Documentation for this module may be created at Module:InfoboxMinimal/doc
-- Module:InfoboxMinimal
local p = {}
local function blank(s)
if s == nil then return true end
s = tostring(s)
return mw.text.trim(s) == ''
end
-- Prefer wrapper (#invoke) args first, then parent (template/page) args
local function get(inv, par, key)
return (inv and inv[key]) or (par and par[key])
end
local function addRow(root, label, data)
if blank(data) then return end
local tr = root:tag('tr')
tr:tag('th')
:attr('scope','row')
:addClass('infobox-label')
:wikitext(label or '')
tr:tag('td')
:addClass('infobox-data')
:wikitext(data)
end
function p.infobox(frame)
local inv = frame.args -- args set in {{#invoke:...}}
local par = (frame:getParent() and frame:getParent().args) or {} -- args from calling template/page
-- Wrapper table
local classes = 'infobox'
local bodyclass = get(inv, par, 'bodyclass')
if not blank(bodyclass) then classes = classes .. ' ' .. tostring(bodyclass) end
local root = mw.html.create('table')
:addClass(classes)
:attr('role', 'presentation')
-- Title / caption element
local title = get(inv, par, 'title') or get(inv, par, 'name') or mw.title.getCurrentTitle().baseText
root:tag('caption'):addClass('infobox-title'):wikitext(title)
-- Optional top image (filename only; no "File:" or "[[]]")
local image = get(inv, par, 'image')
local caption = get(inv, par, 'caption')
local alttext = get(inv, par, 'alt')
local upright = get(inv, par, 'upright') -- e.g. "1.1"
local size = get(inv, par, 'imagesize') -- e.g. "200px"
if not blank(image) then
local opts = { 'frameless', 'center' }
if not blank(upright) then table.insert(opts, 'upright=' .. tostring(upright)) end
if not blank(size) then table.insert(opts, tostring(size)) end
if not blank(alttext) then table.insert(opts, 'alt=' .. tostring(alttext)) end
local imgRow = root:tag('tr')
imgRow:tag('td')
:attr('colspan', 2)
:addClass('infobox-image')
:wikitext(string.format('[[File:%s|%s|%s]]',
tostring(image),
table.concat(opts, '|'),
caption or ''
))
end
-- Data rows: label1/data1 ... label100/data100 (skips blanks)
for i = 1, 100 do
local label = get(inv, par, 'label' .. i)
local data = get(inv, par, 'data' .. i)
addRow(root, label, data)
end
return tostring(root)
end
return p