Module:InfoboxBasic: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
local title = args.name or args.Name or args.title or args.Title or 'Infobox' | local title = args.name or args.Name or args.title or args.Title or 'Infobox' | ||
local image = args.image or args.Image | local image = args.image or args.Image | ||
local color = args.color or args.Color or args.colour or args.Colour -- | local color = args.color or args.Color or args.colour or args.Colour -- title BG (light mode only) | ||
local tbl = mw.html.create('table') | local tbl = mw.html.create('table') | ||
Line 19: | Line 19: | ||
:css('margin','0 0 1em 1em') | :css('margin','0 0 1em 1em') | ||
-- Title (centered, 125% | -- Title (centered, 125%); only add background if |color= was set | ||
local th = tbl:tag('tr') | 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 | if not isBlank(color) then | ||
th:css('--ib-title-bg', color) | th:addClass('has-title-color') -- CSS uses this to apply background | ||
th:css('--ib-title-bg', color) -- value only used in light mode | |||
end | end | ||
-- Optional image | -- Optional image row; give TD a class so CSS can zero the padding and make it 100% wide | ||
if not isBlank(image) then | if not isBlank(image) then | ||
tbl:tag('tr') | tbl:tag('tr') | ||
:tag('td') | :tag('td') | ||
:attr('colspan','2') | :attr('colspan','2') | ||
: | :addClass('infobox-image-cell') | ||
:wikitext('[[File:' .. image .. '|frameless | :wikitext('[[File:' .. image .. '|frameless]]') | ||
end | end | ||
-- | -- Render all remaining named parameters in the order supplied | ||
local names = {} | local names = {} | ||
if parent and parent.getArgumentNames then | if parent and parent.getArgumentNames then | ||
Line 47: | Line 47: | ||
else | else | ||
for k,_ in pairs(args) do table.insert(names,k) end | for k,_ in pairs(args) do table.insert(names,k) end | ||
table.sort(names,function(a,b) return tostring(a)<tostring(b) end) | table.sort(names, function(a,b) return tostring(a) < tostring(b) end) | ||
end | end | ||
for _,key in ipairs(names) do | 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 | |||
} | |||
for _, key in ipairs(names) do | |||
if not skip[key] and not tonumber(key) then | if not skip[key] and not tonumber(key) then | ||
local v = args[key] | local v = args[key] |
Revision as of 22:34, 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()
local args = parent and parent.args or frame.args
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 -- title BG (light mode only)
local tbl = mw.html.create('table')
:addClass('infobox')
:css('float','right')
:css('clear','right')
:css('margin','0 0 1em 1em')
-- Title (centered, 125%); only add background if |color= was set
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') -- CSS uses this to apply background
th:css('--ib-title-bg', color) -- value only used in light mode
end
-- Optional image row; give TD a class so CSS can zero the padding and make it 100% wide
if not isBlank(image) then
tbl:tag('tr')
:tag('td')
:attr('colspan','2')
:addClass('infobox-image-cell')
:wikitext('[[File:' .. image .. '|frameless]]')
end
-- Render all remaining named parameters in the order supplied
local names = {}
if parent and parent.getArgumentNames then
names = parent:getArgumentNames()
else
for k,_ in pairs(args) do table.insert(names,k) end
table.sort(names, function(a,b) return tostring(a) < tostring(b) end)
end
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
}
for _, key in ipairs(names) do
if not skip[key] and not tonumber(key) then
local v = args[key]
if not isBlank(v) then
local row = tbl:tag('tr')
row:tag('th'):attr('scope','row'):wikitext(labelFromKey(key))
row:tag('td'):wikitext(v)
end
end
end
return tostring(tbl)
end
return p