Difference between revisions of "Module:Infobox"
Jump to navigation
Jump to search
Line 14: | Line 14: | ||
end | end | ||
local function infoboxImage(image, defaultImage | local function infoboxImage(image, caption, defaultImage, defaultCaption) | ||
local result = mw.html.create('div') | local result = mw.html.create('div') | ||
result:addClass('infobox-image') | result:addClass('infobox-image') | ||
Line 43: | Line 43: | ||
end | end | ||
local function infoboxGroup(data, rows) | local function infoboxGroup(data, rows, header) | ||
local result | local result = {} | ||
if data then | if data then | ||
if header then | |||
local headerData = mw.html.create('div') | |||
headData:addClass('infobox-header') | |||
:wikitext(header) | |||
result:insert(headerData) | |||
end | |||
if rows then | if rows then | ||
rows = 'data-rows-' .. rows | rows = 'data-rows-' .. rows | ||
end | end | ||
local | local rowData = mw.html.create('div') | ||
rowData:addClass('infobox-row ' .. rows) | |||
:node(data) | |||
result:insert(rowData) | |||
end | end | ||
Line 77: | Line 82: | ||
end | end | ||
function p.building(frame) | |||
local args = a.getArgs(frame) | |||
local building = com.modulePagename(args['title']) | |||
local buildingData = result('Module:Buildings').building | |||
if buildingData or args['image'] then | |||
local result = mainbox(building) | |||
if args['image'] then | |||
result:node(infoboxImage(args['image'], args['caption'])) | |||
end | |||
--Production group | |||
local workers = buildingData.workers | |||
local productData = buildingData.products | |||
local products = '' | |||
if productData then | |||
for k, v in ipairs(productData) do | |||
products = products .. p.icon(v) | |||
if k ~= #productData then | |||
products = products .. '<br />' | |||
end | |||
end | |||
end | |||
if workers or products then | |||
local productionSet = {} | |||
productionSet:insert(infoboxData('workers', workers)) | |||
productionSet:insert(infoboxData('Produces', products)) | |||
result:node(infoboxGroup(productionSet, nil, 'Production')) | |||
end | |||
end | |||
return result | |||
end | |||
return p | return p |
Revision as of 18:01, 11 August 2022
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') :class('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') headData:addClass('infobox-header') :wikitext(header) result:insert(headerData) end if rows then rows = 'data-rows-' .. rows end local rowData = mw.html.create('div') rowData:addClass('infobox-row ' .. rows) :node(data) result:insert(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('label') :addClass('infobox-label') :wikitext(label) :done() :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 = result('Module:Buildings').building if buildingData or args['image'] then local result = mainbox(building) if args['image'] then result:node(infoboxImage(args['image'], args['caption'])) end --Production group local workers = buildingData.workers local productData = buildingData.products local products = '' if productData then for k, v in ipairs(productData) do products = products .. p.icon(v) if k ~= #productData then products = products .. '<br />' end end end if workers or products then local productionSet = {} productionSet:insert(infoboxData('workers', workers)) productionSet:insert(infoboxData('Produces', products)) result:node(infoboxGroup(productionSet, nil, 'Production')) end end return result end return p