Basculer le menu
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

Module:Infobox conflit militaire

De ProleWiki
Version datée du 14 avril 2026 à 14:20 par CriticalResist (discussion | contributions) (v2)
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)
require('Module:No globals')

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

local IMC = {}
IMC.__index = IMC

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	local colspans = {}
	
	-- Ceci peut entraîner que colspans[1] soit défini deux fois, mais
	-- ce n'est pas grave. La deuxième définition sera la bonne.
	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

	-- Le seul cas où colspans[1] ne serait pas défini est si aucun
	-- combattant n'a de champ avec le préfixe et le suffixe donnés.
	if colspans[1] then
		-- Puisque chaque argument trouvé a défini le colspan du précédent,
		-- le dernier n'a pas été défini ci-dessus, on le fait maintenant.
		colspans[lastCombatant] = self.combatants - lastCombatant + 1
		builder = builder:tag('tr')
		for i = 1,self.combatants do
			-- À ce stade, colspans[i] est défini pour i=1 inconditionnellement, et pour
			-- toute autre valeur de i où self.args[prefix .. i .. suffix] est défini.
			if colspans[i] then
				builder:tag('td')
					-- ne pas émettre colspan="1" inutilement
					:attr('colspan', colspans[i] ~= 1 and colspans[i] or nil)
					:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
					-- pas de bordure à droite de la colonne la plus à droite
					:css('border-right', i ~= lastCombatant and infoboxStyle.internal_border or nil)
					-- pas de padding à gauche de la colonne la plus à gauche
					:css('padding-left', i ~= 1 and '0.25em' or nil)
					-- ne pas afficher la bordure si nous sommes directement sous un en-tête
					: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')
				-- ne pas afficher la bordure si nous sommes directement sous un en-tête
				: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('Lieu')
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				:wikitext(self.args.lieu) -- hack pour que ceux qui ne connaissent pas Lua sachent que ce paramètre est requis
			:done()
	if self.args.coordonnees then
		builder:wikitext(self.args.coordonnees)
	end
	builder = builder:done():done()

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

	if self.args.etat or self.args.issue then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.etat and 'État' or 'Issue')
			:done()
			:tag('td')
				:addClass('status')
				:newline()
				:wikitext(self.args.etat or self.args.issue)
	end

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

function IMC:render()
	local builder = mw.html.create()
	if self.args.campagne then
		-- ceci devrait être identique à l'utilisation de {{stack|clear=right|...}}
		builder:wikitext(self.frame:expandTemplate{ title = 'stack begin', args = { clear='true'} })
	end
	builder = builder:tag('table')
		:addClass('infobox vevent')
		:cssText(infoboxStyle.main_box_raw)
		:css('width', '28em')

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants)
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args.conflit or mw.title.getCurrentTitle().text)
	if self.args.guerre then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext('Partie de ' .. self.args.guerre)
	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.taille_image,
						sizedefault = 'frameless',
						upright = 1,
						alt = self.args.alt
					}},
					self.args.legende and '<br />' or '',
					self.args.legende or ''
				))
	end
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
	self:renderPerCombatant(builder, self.args['libellé combattants'] or 'Belligérants', 'combattants')
	-- peut être découplé une fois que gerrit:165108 sera fusionné
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'combattants', v)
	end
	
	self:renderPerCombatant(builder, self.args['libellé commandants'] or 'Commandants et chefs', 'commandant')
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'commandant', v)
	end
	
	self:renderPerCombatant(builder, self.args['libellé unités'] or 'Unités impliquées', 'unites')
	self:renderPerCombatant(builder, self.args['libellé forces'] or 'Forces en présence', 'forces')
	self:renderPerCombatant(builder, self.args['libellé soutien politique'] or 'Soutien politique', 'soutien_politique')
	self:renderPerCombatant(builder, self.args['libellé soutien militaire'] or 'Soutien militaire', 'soutien_militaire')
	self:renderPerCombatant(builder, self.args['libellé pertes'] or 'Pertes', 'pertes')

	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
	if self.args.geolocalisation then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.internal_border)
				:node(require('Module:Location map').main(self.frame, {
					self.args.geolocalisation,
					relief = self.args.relief_carte,
					coordinates = self.args.coordonnees,
					width = self.args.taille_carte or '100%',
					float = 'center',
					border = 'none',
					mark = self.args.marqueur_carte,
					marksize = self.args.taille_marqueur or 8,
					label = self.args.etiquette_carte,
					alt = self.args.alt_carte,
					caption = self.args.legende_carte or ('Localisation en ' 
						.. (require('Module:Location map').data(self.frame, {self.args.geolocalisation, 'name'})))
				}))
	end
	builder = builder:done()
	if self.args.campagne then
		builder = builder:done()
		builder:wikitext(self.args.campagne .. 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 conflit militaire'})
	end
	local obj = {
		frame = frame,
		args = args
	}

	-- jusqu'à ce que gerrit:165108 soit fusionné, il y a toujours une limite sur les combattants, mais dès que ce sera fait, on pourra mettre à jour ce petit bout de code pour la supprimer
	-- aussi, n'essayez pas de rendre cela plus efficace, sinon les références pourraient être dans le mauvais ordre
	obj.combatants = 2
	for _,v in ipairs{'', 'a', 'b', 'c', 'd'} do
		for i = 1,5 do
			if args['combattants' .. 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