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)
Parsed frame:preprocess()
Hiob (discussion | contributions)
amélioration
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 34 : Ligne 34 :
}
}


-- Fonction principale pour styliser une cellule
-- Fonction pour assombrir une couleur (pour la bordure)
function p.style(frame)
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
 
-- Fonction principale unifiée avec auto-détection AMÉLIORÉE
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 ""
      
      
     -- Gestion des alias
     -- Gestion des alias
Ligne 49 : Ligne 62 :
      
      
     colorName = aliases[colorName] or colorName
     colorName = aliases[colorName] or colorName
    local color = colors[colorName] or colors.light
   
    -- DÉTECTION AUTOMATIQUE STRICTE
    local useTableMode = false
    local trimmedContent = mw.text.trim(content)
   
    -- Indicateurs de contexte TABLEAU
    local hasTableSyntax = content:match("%|%|") or content:match("!!") or content:match("%|-")
    local startsWithCode = trimmedContent:match("^<code>")
    local isShortLabel = mw.ustring.len(trimmedContent) <= 20 and not trimmedContent:match("%s%s")
   
    -- Indicateurs de contexte TEXTE (badge inline)
    local hasEmoji = trimmedContent:match("[💡⚠️🔥ℹ️✓✗🎯📌❌✅⭐🎨]")
    local hasBoldWithColon = trimmedContent:match("'''[^']+'''%s*:")
    local hasLongText = mw.ustring.len(trimmedContent) > 40
    local hasPunctuation = trimmedContent:match("[%.%,%;%!%?]") and mw.ustring.len(trimmedContent) > 15
   
    -- DÉCISION : MODE TABLEAU si critères stricts
    if hasTableSyntax or startsWithCode then
        useTableMode = true
    elseif trimmedContent == "" then
        useTableMode = true
    elseif isShortLabel and not hasEmoji and not hasBoldWithColon then
        useTableMode = true
    end
      
      
     -- Récupération de la couleur
     -- FORCER MODE TEXTE si indicateurs clairs
     local color = colors[colorName] or colors.light
     if hasEmoji or hasBoldWithColon or hasLongText or hasPunctuation then
        useTableMode = false
    end
      
      
     -- Construction du style
     -- Construction des styles
     local style = string.format(
     local cellStyle = string.format(
         'style="background-color: %s; color: %s"',
         'background-color:%s; color:%s; padding:8px 12px;',
         color.bg,
         color.bg,
         color.fg
         color.fg
     )
     )
      
      
     -- Si pas de contenu ou contenu vide/nil
     local borderColor = darkenColor(color.bg)
     if not content then
    local spanStyle = string.format(
         return style
        'background-color:%s; color:%s; padding:6px 12px; border-radius:6px; border:1.5px solid %s; display:inline-block; font-weight:500;',
        color.bg,
        color.fg,
        borderColor
    )
   
     -- MODE TABLEAU
    if useTableMode then
        if trimmedContent == "" then
            return 'style="' .. cellStyle .. '" | '
        end
         return 'style="' .. cellStyle .. '" | ' .. content
     end
     end
      
      
     local trimmedContent = mw.text.trim(content)
     -- MODE TEXTE (badge)
     if trimmedContent == "" then
     if trimmedContent == "" then
         return style
         return ''
     end
     end
      
      
    -- Si contenu présent, retourner avec séparateur
     return string.format('<span style="%s">%s</span>', spanStyle, content)
     return style .. ' | ' .. content
end
end


Ligne 78 : Ligne 128 :
     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 85 : Ligne 134 :
     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]
         local row = string.format(
       
             '|-\n| %s || style="background:%s; color:%s; padding:8px 15px; font-weight:500;" | %s || <code>{{Couleur|%s}}</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.fg,
            borderColor,
             color.label
        )
       
        -- Cellule exemple
        local cell = string.format(
            'style="background-color:%s; color:%s; padding:8px 12px;" | %s',
             color.bg,
             color.bg,
             color.fg,
             color.fg,
            color.label
        )
       
        table.insert(rows, string.format(
            '|-\n| %s || %s || %s || <code>{{Couleur|%s|...}}</code>',
             color.label,
             color.label,
            badge,
            cell,
             name
             name
         )
         ))
        table.insert(rows, row)
     end
     end
      
      
    -- Assembler le tableau complet
     local wikitext = '{| class="wikitable sortable"\n! Nom !! Badge (texte) !! Cellule (tableau) !! Code\n'  
     local wikitext = '{| class="wikitable sortable"\n! Nom !! Aperçu !! Code\n'  
         .. table.concat(rows, '\n')  
         .. table.concat(rows, '\n')  
         .. '\n|}'
         .. '\n|}'
      
      
    -- Parser le wikitext avant de le retourner
     return frame:preprocess(wikitext)
     return frame:preprocess(wikitext)
end
end


return p
return p