Module:Infobox

local util_html = require('Module:HTMLUtil') local util_text = require('Module:Text') 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 = {}, }	for k, v in ipairs(layout.sections) do		layout.contents[k] = util_text.split(args[v],sep) local section = layout.contents[k] 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] 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 '') :wikitext(content) :done :done return end

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

function h.printFinalInfobox(infoboxType, layout, display) local tbl = mw.html.create('table') :addClass('infobox') 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')) end if not display.names then display.names = {} 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)					heading_already = true end if layout.contents[k][v2] == 'wide' then h.addWideRow(tbl, display[v2]) else h.addNormalRow(tbl, display.names[v2] or layout.i18n[v2] or v2, display[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) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').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) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').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