m (Syncing content across wikis) |
m (Syncing content across wikis, if something seems broken as a result let me know) |
||
Line 10: | Line 10: | ||
local sep = '%s*,%s*' |
local sep = '%s*,%s*' |
||
+ | local h = {} |
||
⚫ | |||
+ | |||
⚫ | |||
local layout = { |
local layout = { |
||
tabs = args.tabs, |
tabs = args.tabs, |
||
Line 30: | Line 32: | ||
-- build infobox |
-- build infobox |
||
− | function addHeading(tbl, content, class) |
+ | function h.addHeading(tbl, content, class) |
tbl:tag('tr') |
tbl:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 41: | Line 43: | ||
end |
end |
||
− | function addNormalRow(tbl, label, content) |
+ | function h.addNormalRow(tbl, label, content) |
tbl:tag('tr') |
tbl:tag('tr') |
||
:tag('td') |
:tag('td') |
||
Line 54: | Line 56: | ||
end |
end |
||
− | function addWideRow(tbl, content) |
+ | function h.addWideRow(tbl, content) |
tbl:tag('tr') |
tbl:tag('tr') |
||
:tag('td') |
:tag('td') |
||
Line 65: | Line 67: | ||
end |
end |
||
− | function printFinalInfobox(infoboxType, layout, display) |
+ | function h.printFinalInfobox(infoboxType, layout, display) |
local tbl = mw.html.create('table') |
local tbl = mw.html.create('table') |
||
:addClass('infobox') |
:addClass('infobox') |
||
Line 73: | Line 75: | ||
end |
end |
||
if display.notice then |
if display.notice then |
||
− | addHeading(tbl, display.notice,'notice') |
+ | h.addHeading(tbl, display.notice,'notice') |
end |
end |
||
− | addHeading(tbl,layout.lc and lang:lcfirst(display.title) or display.title, 'title') |
+ | h.addHeading(tbl,layout.lc and lang:lcfirst(display.title) or display.title, 'title') |
if display.image then |
if display.image then |
||
− | addWideRow(tbl, string.format('[[File:%s|center|%s]]', display.image, display.imagesize or '220px')) |
+ | h.addWideRow(tbl, string.format('[[File:%s|center|%s]]', display.image, display.imagesize or '220px')) |
end |
end |
||
if not display.names then display.names = {} end |
if not display.names then display.names = {} end |
||
Line 85: | Line 87: | ||
if display[v2] then |
if display[v2] then |
||
if not heading_already then |
if not heading_already then |
||
− | addHeading(tbl, v) |
+ | h.addHeading(tbl, v) |
heading_already = true |
heading_already = true |
||
end |
end |
||
if layout.contents[k][v2] == 'wide' then |
if layout.contents[k][v2] == 'wide' then |
||
− | addWideRow(tbl, display[v2]) |
+ | h.addWideRow(tbl, display[v2]) |
else |
else |
||
− | addNormalRow(tbl, display.names[v2] or layout.i18n[v2] or v2, display[v2]) |
+ | h.addNormalRow(tbl, display.names[v2] or layout.i18n[v2] or v2, display[v2]) |
end |
end |
||
end |
end |
||
Line 99: | Line 101: | ||
end |
end |
||
− | function storeCargo(frame, nocargo, data) |
+ | function h.storeCargo(frame, nocargo, data) |
if nocargo then return end |
if nocargo then return end |
||
for _, tbl in ipairs(data) do |
for _, tbl in ipairs(data) do |
||
Line 107: | Line 109: | ||
end |
end |
||
− | function setLC(frame, lc) |
+ | function h.setLC(frame, lc) |
if not lc then |
if not lc then |
||
return |
return |
||
Line 116: | Line 118: | ||
end |
end |
||
− | function setVariables(frame, data) |
+ | function h.setVariables(frame, data) |
util_table.removeFalseEntries(data) |
util_table.removeFalseEntries(data) |
||
for k, v in pairs(data) do |
for k, v in pairs(data) do |
||
Line 130: | Line 132: | ||
end |
end |
||
− | function setCategories(data, nocat) |
+ | function h.setCategories(data, nocat) |
if nocat then |
if nocat then |
||
return '' |
return '' |
||
Line 162: | Line 164: | ||
end |
end |
||
local data = { |
local data = { |
||
− | layout = layoutFromArgs(args), |
+ | layout = h.layoutFromArgs(args), |
display = args, |
display = args, |
||
cargo = {}, |
cargo = {}, |
||
Line 173: | Line 175: | ||
function p.main(frame, data, infoboxType) |
function p.main(frame, data, infoboxType) |
||
− | setLC(frame, data.settings.lc) |
+ | h.setLC(frame, data.settings.lc) |
− | setVariables(frame, data.variables or {}) |
+ | h.setVariables(frame, data.variables or {}) |
− | storeCargo(frame, data.settings.nocargo, data.cargo or {}) |
+ | h.storeCargo(frame, data.settings.nocargo, data.cargo or {}) |
local output = { |
local output = { |
||
data.layout.tabs and frame:expandTemplate{title = data.layout.tabs} or '', |
data.layout.tabs and frame:expandTemplate{title = data.layout.tabs} or '', |
||
− | tostring(printFinalInfobox(infoboxType, data.layout, data.display)), |
+ | tostring(h.printFinalInfobox(infoboxType, data.layout, data.display)), |
− | setCategories(data.categories or {}, data.settings.nocat) |
+ | h.setCategories(data.categories or {}, data.settings.nocat) |
} |
} |
||
return table.concat(output,'') |
return table.concat(output,'') |
Revision as of 19:30, 7 November 2018
To edit the documentation or categories for this module, click here.
To make a new infobox module, subclass this.
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('[[File:%s|center|%s]]', 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] = ("[[Category:%s]]"):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