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

La documentation pour ce module peut être créée à Module:Infobox/Core/doc

local p = {}

function p.build(args, config)
    if not config then
        return '<div class="error">Configuration manquante pour ce type d\'infobox</div>'
    end
    
    -- Création de la table principale avec la classe CSS appropriée
    local html = mw.html.create('table')
        :addClass('infobox-table')
    
    -- Titre principal (nom)
    local nom = args.nom or mw.title.getCurrentTitle().text
    if nom and nom ~= '' then
        html:tag('tr')
            :tag('th')
            :attr('colspan', '2')
            :addClass('infobox-table-name')
            :wikitext(nom)
    end
    
    -- Image principale
    p.addImage(html, args)
    
    -- Sections
    if config.sections then
        for _, section in ipairs(config.sections) do
            p.addSection(html, args, config, section)
        end
    end
    
    -- Champs personnalisés
    p.addCustomFields(html, args)
    
    return tostring(html)
end

function p.addImage(html, args)
    if args.image and args.image ~= '' then
        local imageSize = args.tailleimage or '280x200px'
        local caption = args.legende or args.legendecarte or mw.title.getCurrentTitle().text
        
        html:tag('tr')
            :attr('id', 'infobox-table-img-bg')
            :tag('td')
            :attr('colspan', '2')
            :addClass('infobox-table-img')
            :wikitext(string.format('[[Fichier:%s|%s|%s]]', args.image, imageSize, caption))
    end
end

function p.addSection(html, args, config, section)
    if not section.fields then return end
    
    -- Vérifier si la section a du contenu
    local hasContent = false
    for _, fieldKey in ipairs(section.fields) do
        local field = p.findField(config.fields, fieldKey)
        if field and args[fieldKey] and args[fieldKey] ~= '' then
            hasContent = true
            break
        end
    end
    
    if not hasContent then return end
    
    -- Ajouter le titre de section
    if section.title then
        html:tag('tr')
            :tag('th')
            :attr('colspan', '2')
            :wikitext(section.title)
    end
    
    -- Ajouter les champs de la section
    for _, fieldKey in ipairs(section.fields) do
        local field = p.findField(config.fields, fieldKey)
        if field and args[fieldKey] and args[fieldKey] ~= '' then
            p.addField(html, args, fieldKey, field)
        end
    end
end

function p.addField(html, args, fieldKey, field)
    local value = args[fieldKey]
    if not value or value == '' then return end
    
    -- Traitement spécial pour les coordonnées
    if fieldKey == 'coordonnees' and args.x and args.y and args.z then
        value = string.format(
            '[https://map.nefald.fr/?worldname=Harlan2&mapname=surface&zoom=4&x=%s&y=%s&z=%s %s, %s, %s]',
            args.x, args.y, args.z, args.x, args.y, args.z
        )
    end
    
    -- Traitement spécial pour les liens wiki
    if field.link then
        value = '[[' .. value .. ']]'
    end
    
    local row = html:tag('tr')
    row:tag('td'):wikitext(field.label)
    row:tag('td'):wikitext(value)
end

function p.addCustomFields(html, args)
    -- Champs personnalisés (champ1/valeur1, champ2/valeur2, etc.)
    for i = 1, 5 do
        local champKey = 'champ' .. i
        local valeurKey = 'valeur' .. i
        
        if args[champKey] and args[valeurKey] and 
           args[champKey] ~= '' and args[valeurKey] ~= '' then
            local row = html:tag('tr')
            row:tag('td'):wikitext(args[champKey])
            row:tag('td'):wikitext(args[valeurKey])
        end
    end
end

function p.findField(fields, key)
    if not fields then return nil end
    for _, field in ipairs(fields) do
        if field.key == key then
            return field
        end
    end
    return nil
end

return p