local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local i18n = require('Module:I18nUtil')
local lang = mw.getLanguage('en')
local CLASSES = {
title = 'infobox-title',
notice = 'infobox-notice'
}
local h = {}
local p = {}
function p.fromPreload(frame)
local args = util_args.merge(true)
local infoboxType = args.infoboxType
local preload = require('Module:Infobox/' .. infoboxType)
local data = preload.main(args)
return p._main(data, infoboxType)
end
function p.fromArgs(frame)
local args = util_args.merge(true)
local data = {
layout = h.layoutFromArgs(args),
display = args,
cargo = {},
categories = {},
settings = {},
variables = {},
}
return p._main(data)
end
function p._main(data, infoboxType)
if data.i18nFile then
i18n.initGlobalFromFile(i18nFile)
data.i18n = {}
end
h.setLC(data.settings.lc)
h.setVariables(data.variables)
h.storeCargo(data.settings.nocargo, data.cargo)
h.mergeDynamicDisplayValuesIntoStatic(data, data.display)
local output = {
h.getTabs(data.layout.tabs),
tostring(h.printFinalInfobox(infoboxType, data.layout, data.display)),
h.setCategories(data.categories or {}, data.settings.nocat)
}
return table.concat(output,'')
end
function h.layoutFromArgs(args)
local layout = {
tabs = args.tabs,
sections = util_text.split(args.sections),
contents = {},
i18n = {},
classes = {},
}
for k, v in ipairs(layout.sections) do
layout.contents[k] = util_text.split(args[v] or '')
local section = layout.contents[k]
layout.classes[v] = args[v .. '_class']
local names = args[v .. '_names'] and util_text.split(args[v .. '_names'])
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.printHeading(tbl, content, class)
if not content then return end
tbl:tag('tr')
:tag('th')
:attr('colspan','2')
:addClass(class and (CLASSES[class] or class) or '')
:wikitext(content)
:done()
:done()
return
end
function h.printNormalRow(tbl, label, content, class)
if not content then return end
tbl:tag('tr')
:addClass(class or '')
:tag('td')
:addClass('infobox-label')
:wikitext(label)
:done()
:tag('td')
:wikitext(content)
:done()
:done()
return
end
function h.printWideRow(tbl, content, class)
if not content then return end
tbl:tag('tr')
:addClass(class or '')
:tag('td')
:attr('colspan','2')
:addClass('infobox-wide')
:wikitext(content)
return
end
function h.mergeDynamicDisplayValuesIntoStatic(data, display)
if not data.layout.classes then data.layout.classes = {} end
if not data.layout.i18n then data.layout.i18n = {} end
util_table.merge(data.layout.classes, display.classes)
util_table.merge(data.layout.i18n, display.names)
end
function h.printFinalInfobox(infoboxType, layout, display)
local tbl = mw.html.create('table')
:addClass('infobox')
:addClass(display.class)
if infoboxType then
tbl:addClass('Infobox' .. infoboxType)
:attr('id','infobox' .. infoboxType)
end
h.printHeading(tbl, display.notice,'notice')
h.printHeading(tbl,layout.lc and lang:lcfirst(display.title) or display.title, 'title')
h.printImage(tbl, display.image, display.imagesize, display.imagecaption)
for k, v in ipairs(layout.sections) do
local headingKey = h.getKeyForHeading(layout.contents[k], display)
if headingKey then
h.printHeading(tbl, v, layout.classes[headingKey])
end
for _, v2 in ipairs(layout.contents[k]) do
if layout.contents[k][v2] == 'wide' then
h.printWideRow(tbl, display[v2], layout.classes[v2])
else
h.printNormalRow(tbl, layout.i18n[v2] or v2, display[v2], layout.classes[v2])
end
end
end
return tbl
end
function h.printImage(tbl, image, imagesize, imagecaption)
if not image then return end
local imageText = ('[[File:%s|center|%s]]'):format(image, imagesize or '220px')
h.printWideRow(tbl, imageText, 'infobox-image')
if imagecaption then
h.printWideRow(tbl, imagecaption, 'infobox-image-caption')
end
end
function h.getKeyForHeading(contents, display)
for k, v in ipairs(contents) do
if display[v] then
return k
end
end
end
function h.storeCargo(nocargo, data)
if nocargo or not data then return end
for _, tbl in ipairs(data) do
util_cargo.store(tbl)
end
return
end
function h.getTabs(tabsTemplateTitle)
if tabsTemplateTitle then
local frame = mw.getCurrentFrame()
return frame:expandTemplate{title = tabsTemplateTitle }
end
return ''
end
function h.setLC(lc)
if not lc then return end
local title = mw.title.getCurrentTitle().text
local frame = mw.getCurrentFrame()
frame:callParserFunction{ name = 'DISPLAYTITLE', args = lang:lcfirst(title) }
return
end
function h.setVariables(data)
if not data then return end
util_table.removeFalseEntries(data)
for k, v in pairs(data) do
mw.getCurrentFrame():callParserFunction{
name = '#vardefine:' .. k,
args = { v }
}
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] = ("[[Category:%s]]"):format(v)
end
return table.concat(tbl, '')
end
return p
Advertisement
Module:Infobox
Advertisement