Module:InfoboxMinimal: Difference between revisions

Created page with "-- Module:InfoboxMinimal local p = {} local function isBlank(s) if s == nil then return true end s = tostring(s) return mw.text.trim(s) == '' end local function addRow(tbl, label, data) if isBlank(data) then return end local tr = tbl: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) -- Prefer parent args so wrapper templat..."
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
local p = {}
local p = {}


local function isBlank(s)
local function blank(s)
if s == nil then return true end
if s == nil then return true end
s = tostring(s)
s = tostring(s)
Line 8: Line 8:
end
end


local function addRow(tbl, label, data)
-- Prefer wrapper (#invoke) args first, then parent (template/page) args
if isBlank(data) then return end
local function get(inv, par, key)
local tr = tbl:tag('tr')
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')
tr:tag('th')
:attr('scope', 'row')
:attr('scope','row')
:addClass('infobox-label')
:addClass('infobox-label')
:wikitext(label or '')
:wikitext(label or '')
Line 21: Line 26:


function p.infobox(frame)
function p.infobox(frame)
-- Prefer parent args so wrapper templates work naturally
local inv = frame.args                                    -- args set in {{#invoke:...}}
local args = frame:getParent() and frame:getParent().args or frame.args
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


-- Table wrapper
local classes = 'infobox'
if not isBlank(args.bodyclass) then
classes = classes .. ' ' .. tostring(args.bodyclass)
end
local root = mw.html.create('table')
local root = mw.html.create('table')
:addClass(classes)
:addClass(classes)
:attr('role', 'presentation')
:attr('role', 'presentation')


-- Title
-- Title / caption element
local title = args.title or args.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')
root:tag('caption'):addClass('infobox-title'):wikitext(title)
: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


-- Rows: scan a reasonable range; stops naturally when dataN is blank
-- Data rows: label1/data1 ... label100/data100 (skips blanks)
for i = 1, 100 do
for i = 1, 100 do
addRow(root, args['label' .. i], args['data' .. i])
local label = get(inv, par, 'label' .. i)
local data  = get(inv, par, 'data' .. i)
addRow(root, label, data)
end
end