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 » : différence entre les versions

De ProleWiki
CriticalResist (discussion | contributions)
Aucun résumé des modifications
CriticalResist (discussion | contributions)
Aucun résumé des modifications
Ligne 2 : Ligne 2 :


local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style') -- Adaptation du nom de module
local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style') -- Adaptation du nom de module
local templatestyles = 'Module:Infobox militaire/styles.css'
local templatestyles = 'Module:Infobox conflit militaire/styles.css'


local IMC = {}
local IMC = {}

Version du 14 avril 2026 à 14:08

require('Module:No globals')

local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style') -- Adaptation du nom de module
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 = {}
    
    -- Cela peut entraîner la définition de colspans[1] deux fois, mais
    -- ce n'est pas grave. La deuxième définition sera correcte.
    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 suffixe donnés.
    if colspans[1] then
        -- Comme chaque argument trouvé définissait le colspan pour le précédent,
        -- le dernier n'a pas été défini ci-dessus, donc on le définit maintenant.
        colspans[lastCombatant] = self.combatants - lastCombatant + 1
        builder = builder:tag('tr')
        for i = 1,self.combatants do
            -- À ce stade, colspans[i] sera 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 s'embêter à émettre colspan="1"
                    :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 remplissage à 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') -- Traduction de 'Location'
        :done()
        :tag('td')
            :tag('div')
                :addClass('location')
                :wikitext(self.args.place) -- astuce pour que les gens qui ne connaissent pas Lua sachent que ce paramètre est requis
            :done()
    if self.args.coordinates then
        builder:wikitext(self.args.coordinates)
    end
    builder = builder:done():done()

    -- seulement 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.status or self.args.result then
        builder:tag('tr')
            :tag('th')
                :css('padding-right', '1em')
                :wikitext(self.args.status and 'Statut' or 'Résultat') -- Traduction de 'Status' / '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('Changements<br />territoriaux') -- Traduction de 'Territorial changes'
            :done()
            :tag('td')
                :newline()
                :wikitext(self.args.territory)
    end
end

function IMC:render()
    local builder = mw.html.create()
    if self.args.campaignbox then
        -- cela devrait être la même chose que d'utiliser {{stack|clear=right|...}}
        -- Note: 'stack begin' et 'stack end' sont des modèles spécifiques, vérifiez leurs équivalents sur votre wiki
        builder:wikitext(self.frame:expandTemplate{ title = 'Début de pile', 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.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('Partie de ' .. self.args.partof) -- Traduction de 'Part of'
    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:Infobox Image').InfoboxImage{args = { -- Module couramment utilisé sur les wikis FR
                        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 'Belligérants', 'combatant')
    -- peut être retiré une fois que gerrit:165108 est fusionné
    for _,v in ipairs{'a','b','c','d'} do
        self:renderPerCombatant(builder, nil, 'combatant', v)
    end
    
    self:renderPerCombatant(builder, self.args.commanders_header or 'Commandants et dirigeants', '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 'Unités engagées', 'units')
    self:renderPerCombatant(builder, self.args.strengths_header or 'Forces en présence', 'strength')
    self:renderPerCombatant(builder, self.args.polstrengths_header or 'Soutien politique', 'polstrength')
    self:renderPerCombatant(builder, self.args.milstrengths_header or 'Soutien militaire', 'milstrength')
    self:renderPerCombatant(builder, self.args.casualties_header or 'Pertes', '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
    if self.args.map_type then
        builder:tag('tr')
            :tag('td')
                :attr('colspan', self.combatants)
                :css('border-top', infoboxStyle.internal_border)
                :node(require('Module:Carte').main(self.frame, { -- Module de carte standard en français
                    self.args.map_type,
                    relief = self.args.map_relief,
                    coordinates = self.args.coordinates,
                    width = self.args.map_size or '100%',
                    float = 'center',
                    border = 'none',
                    mark = self.args.map_mark,
                    marksize = self.args.map_marksize or 8,
                    label = self.args.map_label,
                    alt = self.args.map_alt,
                    caption = self.args.map_caption or ('Localisation dans ' 
                        .. (require('Module:Carte').data(self.frame, {self.args.map_type, 'name'})))
                }))
    end
    builder = builder:done()
    if self.args.campaignbox then
        builder = builder:done()
        builder:wikitext(self.args.campaignbox .. self.frame:expandTemplate{ title = 'Fin de pile'})
    end
    return builder
end

function IMC.new(frame, args)
    if not args then
        args = require('Module:Arguments').getArgs(frame, {wrappers = 'Modèle:Infobox Conflit militaire'})
    end
    local obj = {
        frame = frame,
        args = args
    }

    -- jusqu'à ce que gerrit:165108 soit fusionné, il y a toujours une limite de combattants, mais dès que ça fusionne, on peut mettre à jour ce bout de code pour la supprimer
    -- aussi, n'essayez pas de rendre cela plus efficace, ou 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['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