Jump to content

Module:Infobox military conflict

From Simple English Wikipedia, the free encyclopedia

Documentation for this module may be created at Module:Infobox military conflict/doc

require('strict')

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

-- NOTE (Simple Wikipedia):
-- * Do NOT require Module:Infobox mapframe unless you actually have it on SimpleWiki.
-- * SimpleWiki's {{Location map}} can be picky; {{Location map many}} + width without "px" is safest.

local IMC = {}
IMC.__index = IMC

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	local colspans = {}

	-- This may result in colspans[1] getting set twice, but this is no big deal.
	local lastCombatant = 1

	for i = 1, self.combatants do
		if self.args[prefix .. i .. suffix] then
			colspans[lastCombatant] = i - lastCombatant
			lastCombatant = i
		end
	end

	local jointText = self.args[prefix .. (self.combatants + 1) .. suffix]

	if headerText and (colspans[1] or jointText) then
		builder:tag('tr')
			:tag('th')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.header_raw)
				:wikitext(headerText)
	end

	-- The only time colspans[1] wouldn't be set is if no combatant has a field with the given prefix/suffix.
	if colspans[1] then
		-- Set the final colspan which wasn't set above.
		colspans[lastCombatant] = self.combatants - lastCombatant + 1
		builder = builder:tag('tr')
		for i = 1, self.combatants do
			if colspans[i] then
				builder:tag('td')
					:attr('colspan', colspans[i] ~= 1 and colspans[i] or nil)
					:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
					:css('border-right', i ~= lastCombatant and infoboxStyle.internal_border or nil)
					:css('padding-left', i ~= 1 and '0.25em' or nil)
					:css('border-top', not headerText and infoboxStyle.internal_border or nil)
					:newline()
					:wikitext(self.args[prefix .. i .. suffix])
			end
		end
	end

	if jointText then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('text-align', 'center')
				:css('border-top', (not headerText or colspans[1]) and infoboxStyle.internal_border or nil)
				:newline()
				:wikitext(jointText)
	end
end

function IMC:renderHeaderTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)
		:css('display', 'inline-table')

	if self.args.date then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Date')
			:done()
			:tag('td')
				:wikitext(self.args.date)
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext('Location')
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				:wikitext(self.args.place or '{{{place}}}')
			:done()

	if self.args.coordinates then
		builder:wikitext(self.args.coordinates)
	end

	builder = builder:done():done()

	-- only for "Putsch"
	if self.args.action then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Action')
			:done()
			:tag('td')
				:wikitext(self.args.action)
	end

	if self.args.status or self.args.result then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.status and 'Status' or 'Result')
			:done()
			:tag('td')
				:addClass('status')
				:newline()
				:wikitext(self.args.status or self.args.result)
	end

	if self.args.territory then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Territorial<br />changes')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.territory)
	end
end

function IMC:render()
	local builder = mw.html.create()

	if self.args.campaignbox then
		-- same idea as {{stack|clear=right|...}}
		builder:wikitext(self.frame:expandTemplate{ title = 'stack begin', args = { clear = 'true' } })
	end

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

		:css('width', self.args.width or nil)

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants)
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args.conflict or mw.title.getCurrentTitle().text)

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

	if self.args.image then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('Module:InfoboxImage').InfoboxImage{ args = {
						image = self.args.image,
						size = self.args.image_size,
						sizedefault = 'frameless',
						upright = self.args.image_upright or 1.2,
						alt = self.args.alt
					}},
					self.args.caption and '<br />' or '',
					self.args.caption or ''
				))
	end

	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))

	self:renderPerCombatant(builder, self.args.combatants_header or 'Belligerents', 'combatant')
	for _, v in ipairs{ 'a', 'b', 'c', 'd', 'e' } do
		self:renderPerCombatant(builder, nil, 'combatant', v)
	end

	self:renderPerCombatant(builder, self.args.commanders_header or 'Commanders and leaders', 'commander')
	for _, v in ipairs{ 'a', 'b', 'c', 'd' } do
		self:renderPerCombatant(builder, nil, 'commander', v)
	end

	self:renderPerCombatant(builder, self.args.units_header or 'Units involved', 'units')
	self:renderPerCombatant(builder, self.args.strengths_header or 'Strength', 'strength')
	self:renderPerCombatant(builder, self.args.polstrengths_header or 'Political support', 'polstrength')
	self:renderPerCombatant(builder, self.args.milstrengths_header or 'Military support', 'milstrength')
	self:renderPerCombatant(builder, self.args.casualties_header or 'Casualties and losses', 'casualties')

	if self.args.notes then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.section_border)
				:newline()
				:wikitext(self.args.notes)
	end

	-- =========================
	-- SIMPLEWIKI MAP PARAMETERS
	-- =========================
	local function stripPx(v)
		if not v then return nil end
		v = tostring(v)
		v = mw.ustring.gsub(v, '%s', '')
		v = mw.ustring.gsub(v, 'px$', '')
		return v ~= '' and v or nil
	end

	-- Only draw a map if map_type is set.
	-- coordinates are optional; if you want the marker to appear, pass coordinates in the infobox.
	if self.args.map_type then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.internal_border)
				:css('text-align', 'center')
				:wikitext(self.frame:expandTemplate{
					title = 'Location map many',
					args = {
						[1] = self.args.map_type, -- e.g. "Afghanistan#South Asia"
						relief = self.args.map_relief,
						width = stripPx(self.args.map_size) or 220,
						float = 'center',
						border = 'none',
						mark = self.args.map_mark,
						marksize = stripPx(self.args.map_marksize) or 8,
						label = self.args.map_label,
						alt = self.args.map_alt,
						caption = self.args.map_caption,
						coordinates = self.args.coordinates
					}
				})
	end

	builder = builder:done()

	if self.args.campaignbox then
		builder = builder:done()
		builder:wikitext(self.args.campaignbox .. self.frame:expandTemplate{ title = 'stack end' })
	end

	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, { wrappers = 'Template:Infobox military conflict' })
	end

	local obj = {
		frame = frame,
		args = args
	}

	obj.combatants = 2
	for _, v in ipairs{ '', 'a', 'b', 'c', 'd' } do
		for i = 1, 5 do
			if args['combatant' .. i .. v] then
				obj.combatants = math.max(obj.combatants, i)
			end
		end
	end

	return setmetatable(obj, IMC)
end

local p = {}

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

return p