Module:Massacre infobox

From Revolupedia
Jump to navigation Jump to search

Documentation for this module may be created at Module:Massacre infobox/doc

require('Module:No globals')

local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style')
local templateStyles = 'Module:Infobox military conflict/styles.css'

local MI = {}
MI.__index = MI

function MI:render()
	local args = self.args
	local builder = mw.html.create()

	builder = builder:tag('table')
		:addClass('infobox vevent')
		:cssText(infoboxStyle.main_box_raw)

	-- title
	builder:tag('tr')
		:tag('th')
			:attr('colspan', 2)
			:cssText(infoboxStyle.header_raw)
			:wikitext(args.massacre or mw.title.getCurrentTitle().text)

	-- subtitle (Part of ...)
	if args.partof then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', 2)
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext('Part of ' .. args.partof)
	end

	-- image block
	if args.image then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', 2)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(require('Module:InfoboxImage').InfoboxImage{
					args = {
						image      = args.image,
						size       = args.image_size,
						sizedefault= 'frameless',
						upright    = 1,
						alt        = args.alt
					}
				} .. (args.caption and ('<br/>' .. args.caption) or ''))
	end

	-- data rows
	local fields = {
		{ label = 'Perpetrators',    key = 'perpetrators' },
		{ label = 'Defending side',  key = 'defending_side' },
		{ label = 'Date',            key = 'date' },
		{ label = 'Location',        key = 'location' },
		{ label = 'Victims',         key = 'victims' },
		{ label = 'Deaths',          key = 'deaths' }
	}

	for _, f in ipairs(fields) do
		if args[f.key] then
			builder:tag('tr')
				:tag('th')
					:css('padding-right', '1em')
					:wikitext(f.label)
				:done()
				:tag('td')
					:wikitext(args[f.key])
		end
	end

	return tostring(builder)
end

function MI.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame,
		       {wrappers = 'Template:Massacre infobox'})
	end
	return setmetatable({ args = args }, MI)
end

local p = {}
function p.main(frame)
	return frame:extensionTag{name = 'templatestyles',
	                          args = {src = templateStyles}} ..
	       tostring(MI.new(frame):render())
end

return p