Difference between revisions of "Module:Infobox"
Jump to navigation
Jump to search
| (38 intermediate revisions by the same user not shown) | |||
| Line 19: | Line 19: | ||
if image or defaultImage then | if image or defaultImage then | ||
local file = ' | local file = '[[File:' | ||
if image then | if image then | ||
file = file .. image | file = file .. image | ||
| Line 26: | Line 26: | ||
end | end | ||
file = file .. '|260px|frameless | file = file .. '|260px|frameless]]' | ||
local fileCaption = mw.html.create('span') | local fileCaption = mw.html.create('span') | ||
| Line 48: | Line 48: | ||
if header then | if header then | ||
local headerData = mw.html.create('div') | local headerData = mw.html.create('div') | ||
headerData:addClass('infobox-header') | |||
:wikitext(header) | :wikitext(header) | ||
table.insert(result, headerData) | |||
end | end | ||
local rowClass = 'infobow-row' | |||
if rows then | if rows then | ||
rowClass = rowClass .. ' data-rows-' .. rows | |||
end | end | ||
local rowData = mw.html.create('div') | local rowData = mw.html.create('div') | ||
rowData:addClass( | rowData:addClass(rowClass) | ||
:node( | for k,v in ipairs(data) do | ||
rowData:node(v) | |||
end | |||
table.insert(result, rowData) | |||
end | end | ||
| Line 70: | Line 76: | ||
:addClass('infobox-data-section') | :addClass('infobox-data-section') | ||
if label then | if label then | ||
result:tag(' | result:tag('span') | ||
:addClass('infobox-label') | :addClass('infobox-label') | ||
:wikitext(label) | :wikitext(label) | ||
:done() | :done() | ||
:tag('span') | :tag('span') | ||
:addClass('infobox-data') | |||
:wikitext(data or default) | |||
else | |||
result:tag('span') | |||
:addClass('infobox-data') | :addClass('infobox-data') | ||
:wikitext(data or default) | :wikitext(data or default) | ||
| Line 85: | Line 95: | ||
local args = a.getArgs(frame) | local args = a.getArgs(frame) | ||
local building = com.modulePagename(args['title']) | local building = com.modulePagename(args['title']) | ||
local buildingData = require('Module:Buildings')[building] | local buildingData = require('Module:Buildings')[string.lower(building)] | ||
local result | |||
if buildingData or args['image'] then | if buildingData or args['image'] then | ||
result = mainBox(building) | |||
if args['image'] then | if args['image'] then | ||
result:node(infoboxImage(args['image'], args['caption'])) | result:node(infoboxImage(args['image'], args['caption'])) | ||
end | |||
--Prerequisites group | |||
local prereq = {} | |||
table.insert(prereq, infoboxData(buildingData.building, 'Building')) | |||
table.insert(prereq, infoboxData(buildingData.population, 'Population')) | |||
if buildingData.influence then | |||
for k,v in pairs(buildingData.influence) do | |||
table.insert(prereq, infoboxData(v, k .. 'Influence')) | |||
end | |||
end | |||
if buildingData.splendor then | |||
for k, v in pairs(buildingData.splendor) do | |||
table.insert(prereq, infoboxData(v, k .. 'Splendor')) | |||
end | |||
end | |||
if #prereq > 0 then | |||
local prereqSet = infoboxGroup(prereq, nil, 'Prerequisites') | |||
for k, v in ipairs(prereqSet) do | |||
result:node(v) | |||
end | |||
end | |||
if buildingData.prosperity then | |||
local prospRows = {} | |||
for k, v in pairs(buildingData.prosperity) do | |||
table.insert(prospRows, infoboxData(v, | |||
com.firstUpper({k}))) | |||
end | |||
local prospSet = infoboxGroup(prospRows, 2, 'Prosperity') | |||
mw.logObject(prospSet) | |||
for k, v in ipairs(prospSet) do | |||
result:node(v) | |||
end | |||
end | end | ||
--Production group | --Production group | ||
local workers = buildingData.workers | local workers = buildingData.workers | ||
if buildingData.rank then | |||
workers = workers .. ' ' .. buildingData.rank | |||
end | |||
local requirementData = buildingData.requirements | |||
local requirements = '' | |||
if requirementData then | |||
for k, v in ipairs(requirementData) do | |||
requirements = requirements .. i.icon(v, nil, nil, | |||
com.firstUpper({v}), 'en') | |||
if k ~= #requirementData then | |||
requirements = requirements .. '<br />' | |||
end | |||
end | |||
end | |||
if not com.exists(requirements) then | |||
requirements = nil | |||
end | |||
local productData = buildingData.products | local productData = buildingData.products | ||
local products = '' | local products = '' | ||
if productData then | if productData then | ||
for k, v in ipairs(productData) do | for k, v in ipairs(productData) do | ||
products = products .. i.icon(v, nil, nil, | products = products .. i.icon(v, nil, nil, com.firstUpper({v}), | ||
'en') | |||
if k ~= #productData then | if k ~= #productData then | ||
products = products .. '<br />' | products = products .. '<br />' | ||
| Line 106: | Line 172: | ||
end | end | ||
if workers or products then | if not com.exists(products) then | ||
products = nil | |||
end | |||
if workers or products or requirements then | |||
local productionSet = {} | local productionSet = {} | ||
productionSet | table.insert(productionSet, infoboxData(workers, 'Workers')) | ||
productionSet:insert(infoboxData(' | table.insert(productionSet, infoboxData(requirements, 'Requires')) | ||
table.insert(productionSet, infoboxData(products, 'Produces')) | |||
local production = infoboxGroup(productionSet, nil, 'Production') | |||
for k, v in ipairs(production) do | |||
result:node(v) | |||
end | |||
end | |||
--Construction group | |||
local construction = buildingData.construction | |||
if construction then | |||
local conList = {} | |||
for k, v in pairs(construction) do | |||
local conLabel = i.icon(k, nil, nil, com.firstUpper({k}), | |||
'en') | |||
table.insert(conList, infoboxData(v, conLabel)) | |||
end | |||
local conGroup = infoboxGroup(conList, '2', 'Construction Requirements') | |||
for k, v in ipairs(conGroup) do | |||
result:node(v) | |||
end | |||
end | |||
local maintenance = buildingData.maintenance | |||
local tax = buildingData.tax | |||
if maintenance or tax then | |||
local costSet = {} | |||
local maintenanceLabel = i.icon('maintenance cost', nil , nil, | |||
'Maintenance Cost', 'en') | |||
local taxLabel = i.icon('coin', nil, nil, 'Tax', 'en') | |||
table.insert(costSet, infoboxData(maintenance, maintenanceLabel)) | |||
table.insert(costSet, infoboxData(tax, taxLabel)) | |||
local costGroup = infoboxGroup(costSet, nil, 'Running Costs') | |||
for k, v in ipairs(costGroup) do | |||
result:node(v) | |||
end | |||
end | end | ||
end | end | ||
Latest revision as of 05:27, 25 December 2023
Documentation for this module may be created at Module:Infobox/doc
local com = require('Module:Common')
local a = require('Module:Arguments')
local i = require('Module:Icon')
local p = {}
local function mainBox(title)
local result = mw.html.create('div')
result:addClass('infobox')
:tag('div')
:addClass('infobox-title')
:wikitext(com.modulePagename(title))
:done()
return result
end
local function infoboxImage(image, caption, defaultImage, defaultCaption)
local result = mw.html.create('div')
result:addClass('infobox-image')
if image or defaultImage then
local file = '[[File:'
if image then
file = file .. image
else
file = file .. defaultImage
end
file = file .. '|260px|frameless]]'
local fileCaption = mw.html.create('span')
if caption or defaultCaption then
fileCaption:addClass('infoboxCaption')
:wikitext(caption or defaultCaption)
end
result:wikitext(file)
:node(fileCaption)
else
result = '[[Category:Image Needed]]'
end
return result
end
local function infoboxGroup(data, rows, header)
local result = {}
if data then
if header then
local headerData = mw.html.create('div')
headerData:addClass('infobox-header')
:wikitext(header)
table.insert(result, headerData)
end
local rowClass = 'infobow-row'
if rows then
rowClass = rowClass .. ' data-rows-' .. rows
end
local rowData = mw.html.create('div')
rowData:addClass(rowClass)
for k,v in ipairs(data) do
rowData:node(v)
end
table.insert(result, rowData)
end
return result
end
local function infoboxData(data, label, default)
local result
if data or default then
result = mw.html.create('div')
:addClass('infobox-data-section')
if label then
result:tag('span')
:addClass('infobox-label')
:wikitext(label)
:done()
:tag('span')
:addClass('infobox-data')
:wikitext(data or default)
else
result:tag('span')
:addClass('infobox-data')
:wikitext(data or default)
end
end
return result
end
function p.building(frame)
local args = a.getArgs(frame)
local building = com.modulePagename(args['title'])
local buildingData = require('Module:Buildings')[string.lower(building)]
local result
if buildingData or args['image'] then
result = mainBox(building)
if args['image'] then
result:node(infoboxImage(args['image'], args['caption']))
end
--Prerequisites group
local prereq = {}
table.insert(prereq, infoboxData(buildingData.building, 'Building'))
table.insert(prereq, infoboxData(buildingData.population, 'Population'))
if buildingData.influence then
for k,v in pairs(buildingData.influence) do
table.insert(prereq, infoboxData(v, k .. 'Influence'))
end
end
if buildingData.splendor then
for k, v in pairs(buildingData.splendor) do
table.insert(prereq, infoboxData(v, k .. 'Splendor'))
end
end
if #prereq > 0 then
local prereqSet = infoboxGroup(prereq, nil, 'Prerequisites')
for k, v in ipairs(prereqSet) do
result:node(v)
end
end
if buildingData.prosperity then
local prospRows = {}
for k, v in pairs(buildingData.prosperity) do
table.insert(prospRows, infoboxData(v,
com.firstUpper({k})))
end
local prospSet = infoboxGroup(prospRows, 2, 'Prosperity')
mw.logObject(prospSet)
for k, v in ipairs(prospSet) do
result:node(v)
end
end
--Production group
local workers = buildingData.workers
if buildingData.rank then
workers = workers .. ' ' .. buildingData.rank
end
local requirementData = buildingData.requirements
local requirements = ''
if requirementData then
for k, v in ipairs(requirementData) do
requirements = requirements .. i.icon(v, nil, nil,
com.firstUpper({v}), 'en')
if k ~= #requirementData then
requirements = requirements .. '<br />'
end
end
end
if not com.exists(requirements) then
requirements = nil
end
local productData = buildingData.products
local products = ''
if productData then
for k, v in ipairs(productData) do
products = products .. i.icon(v, nil, nil, com.firstUpper({v}),
'en')
if k ~= #productData then
products = products .. '<br />'
end
end
end
if not com.exists(products) then
products = nil
end
if workers or products or requirements then
local productionSet = {}
table.insert(productionSet, infoboxData(workers, 'Workers'))
table.insert(productionSet, infoboxData(requirements, 'Requires'))
table.insert(productionSet, infoboxData(products, 'Produces'))
local production = infoboxGroup(productionSet, nil, 'Production')
for k, v in ipairs(production) do
result:node(v)
end
end
--Construction group
local construction = buildingData.construction
if construction then
local conList = {}
for k, v in pairs(construction) do
local conLabel = i.icon(k, nil, nil, com.firstUpper({k}),
'en')
table.insert(conList, infoboxData(v, conLabel))
end
local conGroup = infoboxGroup(conList, '2', 'Construction Requirements')
for k, v in ipairs(conGroup) do
result:node(v)
end
end
local maintenance = buildingData.maintenance
local tax = buildingData.tax
if maintenance or tax then
local costSet = {}
local maintenanceLabel = i.icon('maintenance cost', nil , nil,
'Maintenance Cost', 'en')
local taxLabel = i.icon('coin', nil, nil, 'Tax', 'en')
table.insert(costSet, infoboxData(maintenance, maintenanceLabel))
table.insert(costSet, infoboxData(tax, taxLabel))
local costGroup = infoboxGroup(costSet, nil, 'Running Costs')
for k, v in ipairs(costGroup) do
result:node(v)
end
end
end
return result
end
return p