(Translated by https://www.hiragana.jp/)
Module:Infobox military conflict: Difference between revisions - Wikipedia Jump to content

Module:Infobox military conflict: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Jackmcbarn (talk | contribs)
add a hack so that people who don't know Lua know that the place parameter is required
Jackmcbarn (talk | contribs)
rework combatants counting, and round width
Line 22: Line 22:
builder:tag('tr')
builder:tag('tr')
:wikitext(string.format('<th colspan="%d" %s>%s</th>',
:wikitext(string.format('<th colspan="%d" %s>%s</th>',
self.combatantsColspan,
self.combatants,
self.header_bar,
self.header_bar,
headerText
headerText
Line 108: Line 108:
builder:tag('tr')
builder:tag('tr')
:wikitext(string.format('<th class="summary" colspan="%d" %s>%s</th>',
:wikitext(string.format('<th class="summary" colspan="%d" %s>%s</th>',
self.combatantsColspan,
self.combatants,
self.header_bar,
self.header_bar,
self.args.conflict or mw.title.getCurrentTitle().text
self.args.conflict or mw.title.getCurrentTitle().text
Line 115: Line 115:
builder:tag('tr')
builder:tag('tr')
:wikitext(string.format('<td colspan="%d" %s>Part of %s</td>',
:wikitext(string.format('<td colspan="%d" %s>Part of %s</td>',
self.combatantsColspan,
self.combatants,
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'sub_header_bar'}},
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'sub_header_bar'}},
self.args.partof
self.args.partof
Line 123: Line 123:
builder:tag('tr')
builder:tag('tr')
:wikitext(string.format('<td colspan="%d" %s>%s%s%s</td>',
:wikitext(string.format('<td colspan="%d" %s>%s%s%s</td>',
self.combatantsColspan,
self.combatants,
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'image_box'}},
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'image_box'}},
require('Module:InfoboxImage').InfoboxImage{args = {
require('Module:InfoboxImage').InfoboxImage{args = {
Line 136: Line 136:
))
))
end
end
self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatantsColspan))
self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
self:renderPerCombatant(builder, self.args.combatants_header or 'Belligerents', 'combatant')
self:renderPerCombatant(builder, self.args.combatants_header or 'Belligerents', 'combatant')
-- can be un-hardcoded once gerrit:165108 is merged
-- can be un-hardcoded once gerrit:165108 is merged
Line 150: Line 150:
builder:tag('tr')
builder:tag('tr')
:tag('td')
:tag('td')
:attr('colspan', self.combatantsColspan)
:attr('colspan', self.combatants)
:css('font-size', '90%')
:css('font-size', '90%')
:css('border-top', self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'section_border'}})
:css('border-top', self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'section_border'}})
Line 158: Line 158:
builder:tag('tr')
builder:tag('tr')
:tag('td')
:tag('td')
:attr('colspan', self.combatantsColspan)
:attr('colspan', self.combatants)
:css('border-top', self.internal_border)
:css('border-top', self.internal_border)
:tag('center')
:tag('center')
Line 197: Line 197:
-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
-- also, don't try to make this more efficient, or references could be in the wrong order
-- also, don't try to make this more efficient, or references could be in the wrong order
obj.combatants = args.combatant1 and 1
obj.combatants = 2
for _,v in ipairs{
obj.combatants = args.combatant2 and 2 or obj.combatants
{'combatant'},
obj.combatants = args.combatant3 and 3 or obj.combatants
{'combatant', 'a'},
obj.combatants = args.combatant4 and 4 or obj.combatants
{'combatant', 'b'},
{'combatant', 'c'},
{'combatant', 'd'},
{'commander'},
{'strength'},
{'units'},
{'casualties'},
} do
for i = 1,4 do
if args[v[1] .. i .. (v[2] or '')] then
obj.combatants = math.max(obj.combatants, i)
end
end
end


obj.combatantsColspan = obj.combatants or 2
obj.combatantWidth = math.floor(100/obj.combatants + 0.5) .. '%'
obj.combatantWidth = (100/obj.combatantsColspan) .. '%'


return setmetatable(obj, IMC)
return setmetatable(obj, IMC)

Revision as of 03:29, 7 October 2014

require('Module:No globals')

local IMC = {}
IMC.__index = IMC

function IMC:hasAtLeastOne(prefix, suffix)
	if self.combatants then
		for i = 1,self.combatants do
			if self.args[prefix .. i .. suffix] then
				return true
			end
		end
	end
	return false
end

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	if self:hasAtLeastOne(prefix, suffix) then
		if headerText then
			builder:tag('tr')
				:wikitext(string.format('<th colspan="%d" %s>%s</th>',
					self.combatants,
					self.header_bar,
					headerText
				))
		end
		builder = builder:tag('tr')
		for i = 1,self.combatants do
			builder:tag('td')
				:css('width', self.combatantWidth)
				-- no border on the right of the rightmost column
				:css('border-right', i ~= self.combatants and self.internal_border or nil)
				-- no padding on the left of the leftmost column
				:css('padding-left', i ~= 1 and '0.25em' or nil)
				:css('border-top', suffix ~= '' and self.internal_border or nil)
				:wikitext(self.args[prefix .. i .. suffix])
		end
	end
end

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

	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('span')
				:addClass('location')
				:wikitext(self.args.place or '{&#123;{place}&#125;}') -- hack so that people who don't know Lua know that this parameter is required
			:done()
	if self.args.coordinates then
		builder:wikitext('<br />' .. self.args.coordinates)
	end
	builder = builder:done():done()

	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')
				: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')
				:wikitext(self.args.territory)
	end
end

function IMC:render()
	local builder = mw.html.create()
	if self.args.campaignbox then
		builder = builder:tag('table')
			:css('float', 'right')
			:css('clear', 'right')
			:css('background', 'transparent')
			:css('margin', 0)
			:css('padding', 0)
			:tag('tr'):tag('td')
	end
	builder:wikitext('<table ' .. self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'main_box', 'vevent'}} .. '>')
	builder:tag('tr')
		:wikitext(string.format('<th class="summary" colspan="%d" %s>%s</th>',
			self.combatants,
			self.header_bar,
			self.args.conflict or mw.title.getCurrentTitle().text
		))
	if self.args.partof then
		builder:tag('tr')
			:wikitext(string.format('<td colspan="%d" %s>Part of %s</td>',
				self.combatants,
				self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'sub_header_bar'}},
				self.args.partof
			))
	end
	if self.args.image then
		builder:tag('tr')
			:wikitext(string.format('<td colspan="%d" %s>%s%s%s</td>',
				self.combatants,
				self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'image_box'}},
				require('Module:InfoboxImage').InfoboxImage{args = {
					image = self.args.image,
					size = self.args.image_size,
					sizedefault = 'frameless',
					upright = 1,
					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')
	-- can be un-hardcoded once gerrit:165108 is merged
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'combatant', v)
	end
	self:renderPerCombatant(builder, 'Commanders and leaders', 'commander')
	self:renderPerCombatant(builder, 'Units involved', 'units')
	self:renderPerCombatant(builder, 'Strength', 'strength')
	self:renderPerCombatant(builder, 'Casualties and losses', 'casualties')

	if self.args.notes then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('font-size', '90%')
				:css('border-top', self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'section_border'}})
				:wikitext(self.args.notes)
	end
	if self.args.map_type then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', self.internal_border)
				:tag('center')
					:node(require('Module:Location map').main(self.frame, {
						self.args.map_type,
						relief = self.args.map_relief,
						lat = self.args.latitude,
						long = self.args.longitude,
						width = self.args.map_size or 220,
						float = 'center',
						border = 'none',
						marksize = self.args.map_marksize or 8,
						label = self.args.map_label,
						alt = self.args.map_alt,
						caption = self.args.map_caption or ('Location within ' .. self.args.map_type)
					}))
	end
	builder:wikitext('</table>')
	if self.args.campaignbox then
		builder:done():done():tag('tr')
			:tag('td')
				:wikitext(self.args.campaignbox)
	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,
		header_bar = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'header_bar'}},
		internal_border = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'internal_border'}}
	}

	-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
	-- also, don't try to make this more efficient, or references could be in the wrong order
	obj.combatants = 2
	for _,v in ipairs{
		{'combatant'},
		{'combatant', 'a'},
		{'combatant', 'b'},
		{'combatant', 'c'},
		{'combatant', 'd'},
		{'commander'},
		{'strength'},
		{'units'},
		{'casualties'},
	} do
		for i = 1,4 do
			if args[v[1] .. i .. (v[2] or '')] then
				obj.combatants = math.max(obj.combatants, i)
			end
		end
	end

	obj.combatantWidth = math.floor(100/obj.combatants + 0.5) .. '%'

	return setmetatable(obj, IMC)
end

local p = {}

function p.main(frame)
	return IMC.new(frame):render()
end

return p