Module:InfoboxBuilder
Appearance
Documentation for this module may be created at Module:InfoboxBuilder/doc
-- Helper module for creating infoboxes, taken from Meta Weird Gloop wiki
-- @source https://meta.weirdgloop.org/w/Module:InfoboxBuilder?oldid=1271
local InfoboxBuilder = {}
local htmlmt = getmetatable(mw.html.create())
local htmlindex = htmlmt.__index
local html_build_original = htmlindex._build
setmetatable(InfoboxBuilder, {
-- Constructor for a new InfoboxBuilder object.
__call = function(self)
mw.ext.TemplateStyles.link("Module:InfoboxBuilder/styles.css")
return mw.html.create('div')
:addClass("infobox-root")
end,
__index = htmlindex,
__tostring = htmlmt.__tostring
})
-- Override the default html builder to ensure groups are not empty before outputting them
htmlindex._build = function(t, ret)
if t._skip then
return
end
html_build_original(t, ret)
end
htmlindex.title = function(self, value)
return self:tag('div'):addClass("infobox-title"):wikitext(value):done()
end
htmlindex.header = function(self, value)
return self:tag('div'):addClass("infobox-header"):wikitext(value):done()
end
htmlindex.group = function(self, args)
local extraClasses = nil
if args and args.layout then
extraClasses = args.layout
end
local group = self:tag('table'):addClass("infobox-group"):addClass(extraClasses)
group._skip = true
return group
end
--Called by htmlindex.image
local function _image(self, image, caption)
if image and not image:match('gallery') and not image:match('tabber') then
image = image:gsub('%[%[', ''):gsub('%]%]', ''):gsub('^File:', ''):gsub('|thumb', ''):gsub('|$', '')
image = string.format('[[File:%s]]', image)
end
local container = self:tag('div'):addClass('infobox-image-container')
:tag('div'):addClass('infobox-image'):wikitext(image):done()
if caption ~= nil then
container:tag('div'):addClass('infobox-image-caption'):wikitext(caption):done()
end
return container:done()
end
htmlindex.image = function(self, args)
local image = args.image
local caption = args.caption
if (image == nil or image == '') and (caption == nil or caption == '') then
return self
end
return _image(self, image, caption)
end
htmlindex.row = function(self, args)
local label = args.label
local value = args.value
if value == nil or value == '' then
return self
end
-- Don't skip any ancestors of this node
local t = self
t._skip = nil
while t.parent do
t.parent._skip = nil
t = t.parent
end
local row = self:tag('tr'):addClass('infobox-row-container')
if label ~= nil then
row:tag('th'):addClass('infobox-row-label'):wikitext(label):done()
end
return row
:wikitext('\n'):tag('td'):addClass('infobox-row-value'):wikitext('\n'..tostring(value)):done()
:done()
end
local Tabber = {}
Tabber.__index = Tabber
setmetatable(Tabber, {__index = htmlindex})
htmlindex.tabber = function(self)
local obj = mw.html.create()
obj.parent = self
setmetatable(obj, Tabber)
self:node(obj)
return obj
end
function Tabber:_build(ret)
local tabs = {}
for _, tab in ipairs(self.nodes) do
table.insert(tabs, tab.name .. '=' .. tostring(tab))
end
local tabsString = table.concat(tabs, '|-|')
local tabber = mw.getCurrentFrame():extensionTag('tabber', tabsString)
table.insert(ret, tabber)
end
function Tabber:tab(name)
local obj = self:tag('div'):addClass("infobox-tab")
obj.name = name
return obj
end
return InfoboxBuilder