Module:Infobox

local util_args = require('Module:ArgsUtil') local util_html = require('Module:HTMLUtil') local util_text = require('Module:TextUtil') local util_cargo = require('Module:CargoUtil') local util_table = require('Module:TableUtil') local lang = mw.getLanguage('en') local CLASSES = { title = 'infobox-title', notice = 'infobox-notice' } local sep = '%s*,%s*'

local h = {}

function h.layoutFromArgs(args) local layout = { tabs = args.tabs, sections = util_text.split(args.sections,sep), contents = {}, i18n = {}, classes = {}, }	for k, v in ipairs(layout.sections) do		layout.contents[k] = util_text.split(args[v],sep) local section = layout.contents[k] layout.classes[v] = args[v .. '_class'] local names = args[v .. '_names'] and util_text.split(args[v .. '_names'],sep) for i, field in ipairs(section) do layout.i18n[field] = args[field .. '_name'] or names and names[i] layout.classes[field] = args[field .. '_class'] if args[field .. '_style'] then section[field] = args[field .. '_style'] end end end return layout end

-- build infobox

function h.addHeading(tbl, content, class) tbl:tag('tr') :tag('th') :attr('colspan','2') :addClass(class and (CLASSES[class] or class) or '') :wikitext(content) :done :done return end

function h.addNormalRow(tbl, label, content, class) tbl:tag('tr') :addClass(class or '') :tag('td') :addClass('infobox-label') :wikitext(label) :done :tag('td') :wikitext(content) :done :done return end function h.addWideRow(tbl, content, class) tbl:tag('tr') :addClass(class or '') :tag('td') :attr('colspan','2') :addClass('infobox-wide') :wikitext(content) :done :done return end

function h.printFinalInfobox(infoboxType, layout, display) local classes = util_table.merge(layout.classes, display.classes) local names = util_table.merge(layout.i18n, display.names) local tbl = mw.html.create('table') :addClass('infobox') :addClass(display.class) if infoboxType then tbl:addClass('Infobox' .. infoboxType) :attr('id','infobox' .. infoboxType) end if display.notice then h.addHeading(tbl, display.notice,'notice') end h.addHeading(tbl,layout.lc and lang:lcfirst(display.title) or display.title, 'title') if display.image then h.addWideRow(tbl, string.format('', display.image, display.imagesize or '220px')) if display.imagecaption then h.addWideRow(tbl, display.imagecaption, 'infobox-image-caption') end end for k, v in ipairs(layout.sections) do		heading_already = false for _, v2 in ipairs(layout.contents[k]) do			if display[v2] then if not heading_already then h.addHeading(tbl, v, classes[v2]) heading_already = true end if layout.contents[k][v2] == 'wide' then h.addWideRow(tbl, display[v2], classes[v2]) else h.addNormalRow(tbl, names[v2] or v2, display[v2], classes[v2]) end end end end return tbl end

function h.storeCargo(frame, nocargo, data) if nocargo then return end for _, tbl in ipairs(data) do		util_cargo.store(tbl, frame) end return end

function h.setLC(frame, lc) if not lc then return end local title = mw.title.getCurrentTitle.text frame:callParserFunction{ name = 'DISPLAYTITLE', args = lang:lcfirst(title) } return end

function h.setVariables(frame, data) util_table.removeFalseEntries(data) for k, v in pairs(data) do		-- table may have entries that are false if v then frame:callParserFunction{ name = '#vardefine:' .. k,				args = { v } }		end end return end

function h.setCategories(data, nocat) if nocat then return '' end util_table.removeFalseEntries(data) local tbl = {} for _, v in ipairs(data) do		tbl[#tbl+1] = (""):format(v) end return table.concat(tbl, '') end

local p = {} function p.fromPreload(frame) local args = frame if frame == mw.getCurrentFrame then args = util_args.merge(true) else frame = mw.getCurrentFrame end local infoboxType = args.infoboxType local preload = require('Module:Infobox/' .. infoboxType) local data = preload.main(args) return p.main(frame, data, infoboxType) end

function p.fromArgs(frame) local args = frame if frame == mw.getCurrentFrame then args = util_args.overwrite(true) else frame = mw.getCurrentFrame end local data = { layout = h.layoutFromArgs(args), display = args, cargo = {}, categories = {}, settings = {}, variables = {}, }	return p.main(frame, data) end

function p.main(frame, data, infoboxType) h.setLC(frame, data.settings.lc) h.setVariables(frame, data.variables or {}) h.storeCargo(frame, data.settings.nocargo, data.cargo or {}) local output = { data.layout.tabs and frame:expandTemplate{title = data.layout.tabs} or '', tostring(h.printFinalInfobox(infoboxType, data.layout, data.display)), h.setCategories(data.categories or {}, data.settings.nocat) }	return table.concat(output,'') end return p