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.

« Module:Infobox/Core » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
Aucun résumé des modifications
Balises : Modification par mobile Modification par le web mobile
Hiob (discussion | contributions)
Aucun résumé des modifications
Balises : Modification par mobile Modification par le web mobile
Ligne 1 : Ligne 1 :
local core = require('Module:Infobox/Core')
local configs = require('Module:Infobox/Configs')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}


function p.build(args, config)
function p.main(frame)
     if not config then
     local args = getArgs(frame, {parentFirst = true})
        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 infoboxType = args.type or 'monde'
     local html = mw.html.create('table')
    local config = configs[infoboxType]
        :addClass('infobox-table')
      
      
    -- Titre principal (nom)
     if not config then
    local nom = args.nom or mw.title.getCurrentTitle().text
         return '<div class="error">Type d\'infobox non reconnu : ' .. (infoboxType or 'non spécifié') .. '</div>'
     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
     end
      
      
     -- Champs personnalisés
     -- CSS intégré directement
     p.addCustomFields(html, args)
     local cssContent = frame:extensionTag('templatestyles', '', {src = 'Modèle:Infobox/styles.css'})
   
    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
     return cssContent .. core.build(args, config)
    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
end


return p
return p

Version du 23 septembre 2025 à 16:49

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

local core = require('Module:Infobox/Core')
local configs = require('Module:Infobox/Configs')
local getArgs = require('Module:Arguments').getArgs

local p = {}

function p.main(frame)
    local args = getArgs(frame, {parentFirst = true})
    
    local infoboxType = args.type or 'monde'
    local config = configs[infoboxType]
    
    if not config then
        return '<div class="error">Type d\'infobox non reconnu : ' .. (infoboxType or 'non spécifié') .. '</div>'
    end
    
    -- CSS intégré directement
    local cssContent = frame:extensionTag('templatestyles', '', {src = 'Modèle:Infobox/styles.css'})
    
    return cssContent .. core.build(args, config)
end

return p