Difference between revisions of "Module:Infobox"

From Foundation - Wiki
Jump to navigation Jump to search
Line 19: Line 19:
if image or defaultImage then
if image or defaultImage then
local file = '%[%[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')

Revision as of 19:39, 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')
		: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)
			:node(data)
		table.insert(result, rowData)
	end
	
	return result
end

local function infoboxData(data, label, default)
	mw.log(data)
	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 = require('Module:Buildings')[string.lower(building)]

	if buildingData or args['image'] then
		local result = mainBox(building)
		mw.log(tostring(result))
		if args['image'] then
			result:node(infoboxImage(args['image'], args['caption']))
		end
		mw.log(tostring(result))
		--Production group
		local workers = buildingData.workers
		local productData = buildingData.products
		local products = ''
		if productData then
			for k, v in ipairs(productData) do
				products = products .. i.icon(v, nil, nil, nil, 'en')
				if k ~= #productData then
					products = products .. '<br />'
				end
			end
		end
		
		if workers or products then
			local productionSet = {}
			table.insert(productionSet, infoboxData(workers, 'Workers'))
			table.insert(productionSet, infoboxData(products, 'Produces'))
			local production = infoboxGroup(productionSet, nil, 'Production')
			for k, v in ipairs(production) do
				result:node(v)
				mw.log(tostring(result))
			end
		end
	end
	
	return result
end
	
return p