Module:InfoboxBasic: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
-- | -- Module:InfoboxBasic | ||
local | local p = {} | ||
local trim = mw.text.trim | |||
local function | local function isBlank(v) return v == nil or trim(tostring(v)) == '' end | ||
local | local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end | ||
function p.render(frame) | |||
end | local parent = frame:getParent() or frame | ||
local args = parent.args or {} -- never nil | |||
local title = args.name or args.Name or args.title or args.Title or 'Infobox' | |||
local image = args.image or args.Image | |||
local color = args.color or args.Color or args.colour or args.Colour | |||
-- Table | |||
local tbl = mw.html.create('table') | |||
:addClass('infobox') | |||
:css('float','right') | |||
:css('clear','right') | |||
:css('margin','0 0 1em 1em') | |||
-- Title (centered, 125%). Background only if |color= was set (CSS reads --ib-title-bg) | |||
local th = tbl:tag('tr'):tag('th') | |||
:attr('colspan','2') | |||
:addClass('infobox-title') | |||
:css('text-align','center') | |||
:css('font-size','125%') | |||
:wikitext(title) | |||
if not isBlank(color) then | |||
th:addClass('has-title-color') | |||
th:css('--ib-title-bg', color) | |||
end | |||
-- | -- Optional image row (full width via CSS) | ||
if | if not isBlank(image) then | ||
tbl:tag('tr') | |||
:tag('td') | |||
:attr('colspan','2') | |||
:addClass('infobox-image-cell') | |||
:wikitext('[[File:' .. image .. '|frameless]]') | |||
end | end | ||
-- Keys to skip (already handled above) | |||
addRow( | local skip = { | ||
name=true, Name=true, title=true, Title=true, | |||
image=true, Image=true, | |||
color=true, Color=true, colour=true, Colour=true, | |||
class=true, style=true | |||
} | |||
local function addRow(label, value) | |||
local row = tbl:tag('tr') | |||
row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label)) | |||
row:tag('td'):wikitext(value) | |||
end | end | ||
-- Preserve editor's order: prefer getArgumentNames() (source order on modern MW) | |||
if parent.getArgumentNames then | |||
for _, key in ipairs(parent:getArgumentNames()) do | |||
if not skip[key] and not tonumber(key) then | |||
local v = args[key] | |||
if not isBlank(v) then addRow(key, v) end | |||
end | |||
end | |||
elseif parent.argumentPairs then | |||
-- Fallback: collect then reverse (argumentPairs often yields last→first) | |||
local seq = {} | |||
for key, value in parent:argumentPairs() do | |||
if not skip[key] and not tonumber(key) and not isBlank(value) then | |||
seq[#seq+1] = { key = key, value = value } | |||
end | |||
end | |||
for i = #seq, 1, -1 do | |||
addRow(seq[i].key, seq[i].value) | |||
end | |||
else | |||
-- Very old fallback: best effort (order not guaranteed) | |||
for k, v in pairs(args) do | |||
if not skip[k] and not tonumber(k) and not isBlank(v) then | |||
addRow(k, v) | |||
end | |||
end | |||
end | end | ||
return tostring(tbl) | |||
end | end | ||
return p |
Revision as of 22:54, 1 October 2025
Documentation for this module may be created at Module:InfoboxBasic/doc
-- Module:InfoboxBasic
local p = {}
local trim = mw.text.trim
local function isBlank(v) return v == nil or trim(tostring(v)) == '' end
local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end
function p.render(frame)
local parent = frame:getParent() or frame
local args = parent.args or {} -- never nil
local title = args.name or args.Name or args.title or args.Title or 'Infobox'
local image = args.image or args.Image
local color = args.color or args.Color or args.colour or args.Colour
-- Table
local tbl = mw.html.create('table')
:addClass('infobox')
:css('float','right')
:css('clear','right')
:css('margin','0 0 1em 1em')
-- Title (centered, 125%). Background only if |color= was set (CSS reads --ib-title-bg)
local th = tbl:tag('tr'):tag('th')
:attr('colspan','2')
:addClass('infobox-title')
:css('text-align','center')
:css('font-size','125%')
:wikitext(title)
if not isBlank(color) then
th:addClass('has-title-color')
th:css('--ib-title-bg', color)
end
-- Optional image row (full width via CSS)
if not isBlank(image) then
tbl:tag('tr')
:tag('td')
:attr('colspan','2')
:addClass('infobox-image-cell')
:wikitext('[[File:' .. image .. '|frameless]]')
end
-- Keys to skip (already handled above)
local skip = {
name=true, Name=true, title=true, Title=true,
image=true, Image=true,
color=true, Color=true, colour=true, Colour=true,
class=true, style=true
}
local function addRow(label, value)
local row = tbl:tag('tr')
row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label))
row:tag('td'):wikitext(value)
end
-- Preserve editor's order: prefer getArgumentNames() (source order on modern MW)
if parent.getArgumentNames then
for _, key in ipairs(parent:getArgumentNames()) do
if not skip[key] and not tonumber(key) then
local v = args[key]
if not isBlank(v) then addRow(key, v) end
end
end
elseif parent.argumentPairs then
-- Fallback: collect then reverse (argumentPairs often yields last→first)
local seq = {}
for key, value in parent:argumentPairs() do
if not skip[key] and not tonumber(key) and not isBlank(value) then
seq[#seq+1] = { key = key, value = value }
end
end
for i = #seq, 1, -1 do
addRow(seq[i].key, seq[i].value)
end
else
-- Very old fallback: best effort (order not guaranteed)
for k, v in pairs(args) do
if not skip[k] and not tonumber(k) and not isBlank(v) then
addRow(k, v)
end
end
end
return tostring(tbl)
end
return p