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
Line 1: Line 1:
-- Module:InfoboxMinimal
-- Module:InfoboxMinimal (patched)
local p = {}
local p = {}


local function isBlank(s)
local function isBlank(s) return not s or mw.text.trim(tostring(s)) == '' end
if s == nil then return true end
 
s = tostring(s)
local function get(args1, args2, key)
return mw.text.trim(s) == ''
-- prefer wrapper (#invoke) args, then parent (template) args
return (args1 and args1[key]) or (args2 and args2[key])
end
end


Line 11: Line 12:
if isBlank(data) then return end
if isBlank(data) then return end
local tr = tbl:tag('tr')
local tr = tbl:tag('tr')
tr:tag('th')
tr:tag('th'):attr('scope','row'):addClass('infobox-label'):wikitext(label or '')
:attr('scope', 'row')
tr:tag('td'):addClass('infobox-data'):wikitext(data)
:addClass('infobox-label')
:wikitext(label or '')
tr:tag('td')
:addClass('infobox-data')
:wikitext(data)
end
end


function p.infobox(frame)
function p.infobox(frame)
-- Prefer parent args so wrapper templates work naturally
local invokeArgs  = frame.args                      -- from {{#invoke:...| key = value }}
local args = frame:getParent() and frame:getParent().args or frame.args
local parentArgs  = frame:getParent() and frame:getParent().args or {} -- from {{Infobox_Startup|...}}


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


-- Title
local title = get(invokeArgs, parentArgs, 'title') or
local title = args.title or args.name or mw.title.getCurrentTitle().baseText
              get(invokeArgs, parentArgs, 'name') or
root:tag('caption')
              mw.title.getCurrentTitle().baseText
:addClass('infobox-title')
root:tag('caption'):addClass('infobox-title'):wikitext(title)
:wikitext(title)


-- Rows: scan a reasonable range; stops naturally when dataN is blank
for i = 1, 100 do
for i = 1, 100 do
addRow(root, args['label' .. i], args['data' .. i])
local label = get(invokeArgs, parentArgs, 'label'..i)
local data  = get(invokeArgs, parentArgs, 'data' ..i)
addRow(root, label, data)
end
end