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

De Nefald
Hiob (discussion | contributions)
m Avec ou sans tableau
Hiob (discussion | contributions)
Auto détection tableau / texte en ligne
Ligne 34 : Ligne 34 :
}
}


-- Fonction principale unifiée
-- Fonction pour assombrir une couleur (pour la bordure)
local function darkenColor(hex)
    local r = tonumber(hex:sub(2, 3), 16)
    local g = tonumber(hex:sub(4, 5), 16)
    local b = tonumber(hex:sub(6, 7), 16)
   
    r = math.floor(r * 0.7)
    g = math.floor(g * 0.7)
    b = math.floor(b * 0.7)
   
    return string.format("#%02x%02x%02x", r, g, b)
end
 
-- Détection automatique du contexte (tableau vs texte)
local function isInTableContext(frame)
    -- Récupère le wikitext parent
    local parent = frame:getParent()
    if not parent then return false end
   
    -- Récupère le titre de la page
    local title = mw.title.getCurrentTitle()
    local content = title:getContent()
   
    if not content then return false end
   
    -- Cherche si le modèle est appelé dans un contexte de tableau
    -- On détecte les patterns typiques : {| ... {{Couleur ... |}
    local pattern = "%{%|.-%{%{[Cc]ouleur%|.-%}%}.-%|%}"
   
    return content:match(pattern) ~= nil
end
 
-- Fonction principale unifiée avec auto-détection
function p.cell(frame)
function p.cell(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local colorName = mw.text.trim(args[1] or ""):lower()
     local colorName = mw.text.trim(args[1] or ""):lower()
     local content = args[2]
     local content = args[2] or ""
    local mode = mw.text.trim(args.mode or args[3] or "auto"):lower()
      
      
     -- Gestion des alias
     -- Gestion des alias
Ligne 50 : Ligne 81 :
      
      
     colorName = aliases[colorName] or colorName
     colorName = aliases[colorName] or colorName
    local color = colors[colorName] or colors.light
      
      
     -- Récupération de la couleur
     -- DÉTECTION AUTOMATIQUE AMÉLIORÉE
     local color = colors[colorName] or colors.light
    local useTableMode = false
   
    -- Méthode 1 : Détection par le contenu vide (cellule de tableau)
     if mw.text.trim(content) == "" then
        useTableMode = true
   
    -- Méthode 2 : Détection par balises HTML typiques de tableaux
    elseif content:match("^%s*<code>") or content:match("^%s*'''") then
        useTableMode = true
   
    -- Méthode 3 : Contenu très court (probablement un en-tête de tableau)
    elseif mw.ustring.len(mw.text.trim(content)) <= 20 and not content:match("%s%s") then
        useTableMode = true
   
    -- Méthode 4 : Présence de syntaxe wiki spécifique aux tableaux
    elseif content:match("||") or content:match("!!") then
        useTableMode = true
    end
      
      
     -- Construction du style
     -- Construction des styles
     local styleAttr = string.format(
     local cellStyle = string.format(
         'background-color:%s; color:%s; padding:8px 12px;',
         'background-color:%s; color:%s; padding:8px 12px;',
         color.bg,
         color.bg,
Ligne 61 : Ligne 110 :
     )
     )
      
      
     -- Détection automatique du contexte
     local borderColor = darkenColor(color.bg)
     if mode == "auto" then
     local spanStyle = string.format(
         -- Si pas de contenu, c'est probablement pour un tableau
         'background-color:%s; color:%s; padding:6px 12px; border-radius:6px; border:1.5px solid %s; display:inline-block; font-weight:500;',
         if not content or mw.text.trim(content) == "" then
         color.bg,
            mode = "table"
         color.fg,
         else
         borderColor
            mode = "span"
     )
         end
     end
      
      
     -- Mode tableau (pour les cellules)
     -- MODE TABLEAU
     if mode == "table" or mode == "cellule" then
     if useTableMode then
         if not content or mw.text.trim(content) == "" then
         if mw.text.trim(content) == "" then
             return 'style="' .. styleAttr .. '" | '
             return 'style="' .. cellStyle .. '" | '
         end
         end
         return 'style="' .. styleAttr .. '" | ' .. content
         return 'style="' .. cellStyle .. '" | ' .. content
     end
     end
      
      
     -- Mode span (pour le texte normal)
     -- MODE TEXTE (badge)
     if not content or mw.text.trim(content) == "" then
     if mw.text.trim(content) == "" then
         return ''
         return ''
     end
     end
      
      
     return string.format(
     return string.format('<span style="%s">%s</span>', spanStyle, content)
        '<span style="%s">%s</span>',
        styleAttr,
        content
    )
end
end


Ligne 95 : Ligne 138 :
     local rows = {}
     local rows = {}
      
      
    -- Trier les noms de couleurs
     local sorted = {}
     local sorted = {}
     for name, _ in pairs(colors) do
     for name, _ in pairs(colors) do
Ligne 102 : Ligne 144 :
     table.sort(sorted)
     table.sort(sorted)
      
      
    -- Construire les lignes du tableau
     for _, name in ipairs(sorted) do
     for _, name in ipairs(sorted) do
         local color = colors[name]
         local color = colors[name]
         table.insert(rows, string.format(
          
             '|-\n| %s || style="background-color:%s; color:%s; padding:8px 15px; font-weight:500;" | %s || <code>{{Couleur|%s|Exemple}}</code>',
        -- Badge exemple
            color.label,
        local borderColor = darkenColor(color.bg)
        local badge = string.format(
             '<span style="background-color:%s; color:%s; padding:6px 12px; border-radius:6px; border:1.5px solid %s; display:inline-block; font-weight:500;">%s</span>',
             color.bg,
             color.bg,
             color.fg,
             color.fg,
            borderColor,
            color.label
        )
       
        table.insert(rows, string.format(
            '|-\n| %s || %s || <code>{{Couleur|%s|Texte}}</code>',
             color.label,
             color.label,
            badge,
             name
             name
         ))
         ))
     end
     end
      
      
    -- Assembler le tableau complet
     local wikitext = '{| class="wikitable sortable"\n! Nom !! Aperçu (badge) !! Code\n'  
     local wikitext = '{| class="wikitable sortable"\n! Nom !! Aperçu !! Code\n'  
         .. table.concat(rows, '\n')  
         .. table.concat(rows, '\n')  
         .. '\n|}'
         .. '\n|}'