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)
amélioration
Hiob (discussion | contributions)
mAucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Palette inspirée de Bootstrap 5 + Citizen
-- =============================================================================
-- PALETTE alignée sur les classes CSS table-color-*
-- Couleurs identiques à celles définies dans ton Common.css/Citizen.css
-- =============================================================================
 
local colors = {
local colors = {
    -- Variantes de succès (vert)
 
    success = { bg = "#d1e7dd", fg = "#0f5132", label = "Succès" },
-- Succès (vert) — table-color-success
    ["success-light"] = { bg = "#e8f5e9", fg = "#1b5e20", label = "Succès clair" },
success = {
   
bg = "#d1e7dd",
    -- Variantes de danger (rouge)
fg = "#0f5132",
    danger = { bg = "#f8d7da", fg = "#842029", label = "Danger" },
border = "#a3cfbb",
    ["danger-light"] = { bg = "#ffebee", fg = "#c62828", label = "Danger clair" },
label = "Succès",
   
cssClass = "table-color-success"
    -- Variantes d'avertissement (jaune/orange)
},
    warning = { bg = "#fff3cd", fg = "#664d03", label = "Avertissement" },
["success-light"] = {
    ["warning-light"] = { bg = "#fffde7", fg = "#f57f17", label = "Avertissement clair" },
bg = "#e8f5e9",
   
fg = "#1b5e20",
    -- Variantes d'information (bleu)
border = "#c8e6c9",
    info = { bg = "#cfe2ff", fg = "#084298", label = "Info" },
label = "Succès clair",
    ["info-light"] = { bg = "#e3f2fd", fg = "#01579b", label = "Info clair" },
cssClass = "table-color-success"
   
},
    -- Variantes neutres
 
    primary = { bg = "#cfe2ff", fg = "#052c65", label = "Primaire" },
-- Danger (rouge) — table-color-danger
    secondary = { bg = "#e2e3e5", fg = "#41464b", label = "Secondaire" },
danger = {
    light = { bg = "#f8f9fa", fg = "#495057", label = "Clair" },
bg = "#f8d7da",
    dark = { bg = "#ced4da", fg = "#212529", label = "Foncé" },
fg = "#842029",
   
border = "#f1aeb5",
    -- Compatibilité anciens noms
label = "Danger",
    vert = { bg = "#d1e7dd", fg = "#0f5132", label = "Vert" },
cssClass = "table-color-danger"
    rouge = { bg = "#f8d7da", fg = "#842029", label = "Rouge" },
},
    jaune = { bg = "#fff3cd", fg = "#664d03", label = "Jaune" },
["danger-light"] = {
    rose = { bg = "#f8d7da", fg = "#842029", label = "Rose" },
bg = "#ffebee",
    gris = { bg = "#e2e3e5", fg = "#41464b", label = "Gris" },
fg = "#c62828",
    bleu = { bg = "#cfe2ff", fg = "#084298", label = "Bleu" },
border = "#ffcdd2",
label = "Danger clair",
cssClass = "table-color-danger"
},
 
-- Avertissement (jaune) — table-color-warning
warning = {
bg = "#fff3cd",
fg = "#664d03",
border = "#ffe69c",
label = "Avertissement",
cssClass = "table-color-warning"
},
["warning-light"] = {
bg = "#fffde7",
fg = "#f57f17",
border = "#fff9c4",
label = "Avertissement clair",
cssClass = "table-color-warning"
},
 
-- Info (bleu) — table-color-info
info = {
bg = "#cfe2ff",
fg = "#084298",
border = "#9ec5fe",
label = "Info",
cssClass = "table-color-info"
},
["info-light"] = {
bg = "#e3f2fd",
fg = "#01579b",
border = "#bbdefb",
label = "Info clair",
cssClass = "table-color-info"
},
 
-- Secondaire (gris) — table-color-secondary
secondary = {
bg = "#e2e3e5",
fg = "#41464b",
border = "#c4c8cb",
label = "Secondaire",
cssClass = "table-color-secondary"
},
light = {
bg = "#f8f9fa",
fg = "#495057",
border = "#dee2e6",
label = "Clair",
cssClass = "table-color-secondary"
},
dark = {
bg = "#ced4da",
fg = "#212529",
border = "#adb5bd",
label = "Foncé",
cssClass = "table-color-secondary"
},
 
-- Aliases francophones
vert   = { bg = "#d1e7dd", fg = "#0f5132", border = "#a3cfbb", label = "Vert",  cssClass = "table-color-success" },
rouge = { bg = "#f8d7da", fg = "#842029", border = "#f1aeb5", label = "Rouge",  cssClass = "table-color-danger"  },
jaune = { bg = "#fff3cd", fg = "#664d03", border = "#ffe69c", label = "Jaune",  cssClass = "table-color-warning" },
bleu  = { bg = "#cfe2ff", fg = "#084298", border = "#9ec5fe", label = "Bleu",  cssClass = "table-color-info"   },
gris   = { bg = "#e2e3e5", fg = "#41464b", border = "#c4c8cb", label = "Gris",  cssClass = "table-color-secondary"},
rose  = { bg = "#f8d7da", fg = "#842029", border = "#f1aeb5", label = "Rose",  cssClass = "table-color-danger" },
}
 
-- =============================================================================
-- ALIASES
-- =============================================================================
 
local aliases = {
["gris-"]      = "info-light",
["gris+"]      = "secondary",
["vert-clair"]  = "success-light",
["rouge-clair"] = "danger-light",
primary        = "info",
}
}


-- Fonction pour assombrir une couleur (pour la bordure)
-- =============================================================================
local function darkenColor(hex)
-- AUTO-DÉTECTION du mode (cellule tableau vs badge inline)
    local r = tonumber(hex:sub(2, 3), 16)
-- =============================================================================
    local g = tonumber(hex:sub(4, 5), 16)
 
    local b = tonumber(hex:sub(6, 7), 16)
local function detectMode(content)
   
local trimmed = mw.text.trim(content)
    r = math.floor(r * 0.7)
 
    g = math.floor(g * 0.7)
if trimmed == "" then return "cell" end
    b = math.floor(b * 0.7)
 
   
-- Indicateurs tableau
    return string.format("#%02x%02x%02x", r, g, b)
if content:match("%|%|") or content:match("!!") or content:match("%|-") then return "cell" end
if trimmed:match("^<code>") then return "cell" end
 
-- Indicateurs texte inline
if trimmed:match("[💡⚠️🔥ℹ️✓✗🎯📌❌✅⭐🎨]") then return "badge" end
if trimmed:match("'''[^']+'''%s*:") then return "badge" end
if mw.ustring.len(trimmed) > 40 then return "badge" end
if trimmed:match("[%.%,%;%!%?]") and mw.ustring.len(trimmed) > 15 then return "badge" end
 
-- Court et sans ponctuation → cellule tableau
if mw.ustring.len(trimmed) <= 20 then return "cell" end
 
return "badge"
end
end


-- Fonction principale unifiée avec auto-détection AMÉLIORÉE
-- =============================================================================
-- RENDU CELLULE TABLEAU
-- Utilise la classe CSS table-color-* pour le support dark mode automatique
-- =============================================================================
 
local function renderCell(color, content)
local trimmed = mw.text.trim(content)
-- On utilise la classe CSS → dark mode géré par Common.css
local style = 'class="table-color-cell ' .. color.cssClass .. '"'
 
if trimmed == "" then
return style .. ' | '
end
return style .. ' | ' .. content
end
 
-- =============================================================================
-- RENDU BADGE INLINE
-- Utilise les couleurs hex directement (contexte hors tableau)
-- =============================================================================
 
local function renderBadge(color, content)
local trimmed = mw.text.trim(content)
if trimmed == "" then return "" end
 
local spanStyle = string.format(
'background-color:%s; color:%s; padding:4px 10px; border-radius:6px; ' ..
'border:1.5px solid %s; display:inline-block; font-weight:500; font-size:0.875em;',
color.bg, color.fg, color.border
)
return string.format('<span style="%s">%s</span>', spanStyle, trimmed)
end
 
-- =============================================================================
-- FONCTION PRINCIPALE : {{Couleur|nom|contenu}}
-- =============================================================================
 
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] or ""
local content   = args[2] or ""
   
 
    -- Gestion des alias
-- Résolution alias
    local aliases = {
colorName = aliases[colorName] or colorName
        ["gris-"] = "info-light",
local color = colors[colorName] or colors.light
        ["gris+"] = "secondary",
 
        ["vert-clair"] = "success-light",
-- Détection du mode
        ["rouge-clair"] = "danger-light",
local mode = detectMode(content)
    }
 
   
if mode == "cell" then
    colorName = aliases[colorName] or colorName
return renderCell(color, content)
    local color = colors[colorName] or colors.light
else
   
return renderBadge(color, content)
    -- DÉTECTION AUTOMATIQUE STRICTE
end
    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
   
    -- FORCER MODE TEXTE si indicateurs clairs
    if hasEmoji or hasBoldWithColon or hasLongText or hasPunctuation then
        useTableMode = false
    end
   
    -- Construction des styles
    local cellStyle = string.format(
        'background-color:%s; color:%s; padding:8px 12px;',
        color.bg,
        color.fg
    )
   
    local borderColor = darkenColor(color.bg)
    local spanStyle = string.format(
        '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
   
    -- MODE TEXTE (badge)
    if trimmedContent == "" then
        return ''
    end
   
    return string.format('<span style="%s">%s</span>', spanStyle, content)
end
end


-- Fonction pour lister les couleurs disponibles
-- =============================================================================
-- LISTE DES COULEURS DISPONIBLES (pour documentation)
-- Génère un tableau wikitable avec aperçu
-- =============================================================================
 
function p.list(frame)
function p.list(frame)
    local rows = {}
local rows = {}
   
 
    local sorted = {}
-- Tri par nom
    for name, _ in pairs(colors) do
local sorted = {}
        table.insert(sorted, name)
for name in pairs(colors) do
    end
table.insert(sorted, name)
    table.sort(sorted)
end
   
table.sort(sorted)
    for _, name in ipairs(sorted) do
 
        local color = colors[name]
for _, name in ipairs(sorted) do
       
local color = colors[name]
        -- Badge exemple
 
        local borderColor = darkenColor(color.bg)
local badge = renderBadge(color, color.label)
        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>',
-- Cellule : on génère le wikitext de la ligne entière
            color.bg,
local cellStyle = string.format(
            color.fg,
'background-color:%s; color:%s; padding:8px 12px;',
            borderColor,
color.bg, color.fg
            color.label
)
        )
 
       
table.insert(rows, string.format(
        -- Cellule exemple
'|-\n| %s || %s || style="%s" | %s || <code>table-color-%s</code> || <code>{{Couleur|%s|...}}</code>',
        local cell = string.format(
color.label,
            'style="background-color:%s; color:%s; padding:8px 12px;" | %s',
badge,
            color.bg,
cellStyle,
            color.fg,
color.label,
            color.label
name,
        )
name
       
))
        table.insert(rows, string.format(
end
            '|-\n| %s || %s || %s || <code>{{Couleur|%s|...}}</code>',
 
            color.label,
local header = '{| class="wikitable sortable"\n' ..
            badge,
'! Nom !! Badge !! Cellule !! Classe CSS !! Wikicode\n'
            cell,
 
            name
return frame:preprocess(header .. table.concat(rows, '\n') .. '\n|}')
        ))
end
    end
 
   
-- =============================================================================
    local wikitext = '{| class="wikitable sortable"\n! Nom !! Badge (texte) !! Cellule (tableau) !! Code\n'  
-- APERÇU PALETTE (pour Aide:Charte graphique)
        .. table.concat(rows, '\n')  
-- Génère les {{Card}} avec bgcolor des couleurs de la palette
        .. '\n|}'
-- =============================================================================
      
 
    return frame:preprocess(wikitext)
function p.palette(frame)
local args = frame.args
local group = mw.text.trim(args[1] or "all"):lower()
 
-- Groupes à afficher
local groups = {
success  = { "success", "success-light" },
danger  = { "danger",  "danger-light"  },
warning  = { "warning", "warning-light" },
info     = { "info",    "info-light"    },
neutral  = { "secondary", "light", "dark" },
}
 
local targets = {}
if group == "all" then
for _, names in pairs(groups) do
for _, n in ipairs(names) do table.insert(targets, n) end
end
table.sort(targets)
else
targets = groups[group] or { group }
end
 
local cards = {}
for _, name in ipairs(targets) do
local color = colors[name]
if color then
-- Appel du modèle Card avec bgcolor
local card = string.format(
'{{Card|title=%s|subtitle=%s / %s|bgcolor=%s|colorheight=80|text=Classe : <code>%s</code>}}',
color.label,
color.bg,
color.fg,
color.bg,
color.cssClass
)
table.insert(cards, card)
end
end
 
local output = '<div class="ds-swatch-grid">\n' ..
table.concat(cards, '\n') ..
'\n</div>'
 
return frame:preprocess(output)
end
end


return p
return p

Version du 2 mars 2026 à 14:38

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

local p = {}

-- =============================================================================
-- PALETTE alignée sur les classes CSS table-color-*
-- Couleurs identiques à celles définies dans ton Common.css/Citizen.css
-- =============================================================================

local colors = {

-- Succès (vert) — table-color-success
success = {
bg = "#d1e7dd",
fg = "#0f5132",
border = "#a3cfbb",
label = "Succès",
cssClass = "table-color-success"
},
["success-light"] = {
bg = "#e8f5e9",
fg = "#1b5e20",
border = "#c8e6c9",
label = "Succès clair",
cssClass = "table-color-success"
},

-- Danger (rouge) — table-color-danger
danger = {
bg = "#f8d7da",
fg = "#842029",
border = "#f1aeb5",
label = "Danger",
cssClass = "table-color-danger"
},
["danger-light"] = {
bg = "#ffebee",
fg = "#c62828",
border = "#ffcdd2",
label = "Danger clair",
cssClass = "table-color-danger"
},

-- Avertissement (jaune) — table-color-warning
warning = {
bg = "#fff3cd",
fg = "#664d03",
border = "#ffe69c",
label = "Avertissement",
cssClass = "table-color-warning"
},
["warning-light"] = {
bg = "#fffde7",
fg = "#f57f17",
border = "#fff9c4",
label = "Avertissement clair",
cssClass = "table-color-warning"
},

-- Info (bleu) — table-color-info
info = {
bg = "#cfe2ff",
fg = "#084298",
border = "#9ec5fe",
label = "Info",
cssClass = "table-color-info"
},
["info-light"] = {
bg = "#e3f2fd",
fg = "#01579b",
border = "#bbdefb",
label = "Info clair",
cssClass = "table-color-info"
},

-- Secondaire (gris) — table-color-secondary
secondary = {
bg = "#e2e3e5",
fg = "#41464b",
border = "#c4c8cb",
label = "Secondaire",
cssClass = "table-color-secondary"
},
light = {
bg = "#f8f9fa",
fg = "#495057",
border = "#dee2e6",
label = "Clair",
cssClass = "table-color-secondary"
},
dark = {
bg = "#ced4da",
fg = "#212529",
border = "#adb5bd",
label = "Foncé",
cssClass = "table-color-secondary"
},

-- Aliases francophones
vert   = { bg = "#d1e7dd", fg = "#0f5132", border = "#a3cfbb", label = "Vert",   cssClass = "table-color-success" },
rouge  = { bg = "#f8d7da", fg = "#842029", border = "#f1aeb5", label = "Rouge",  cssClass = "table-color-danger"  },
jaune  = { bg = "#fff3cd", fg = "#664d03", border = "#ffe69c", label = "Jaune",  cssClass = "table-color-warning" },
bleu   = { bg = "#cfe2ff", fg = "#084298", border = "#9ec5fe", label = "Bleu",   cssClass = "table-color-info"    },
gris   = { bg = "#e2e3e5", fg = "#41464b", border = "#c4c8cb", label = "Gris",   cssClass = "table-color-secondary"},
rose   = { bg = "#f8d7da", fg = "#842029", border = "#f1aeb5", label = "Rose",   cssClass = "table-color-danger"  },
}

-- =============================================================================
-- ALIASES
-- =============================================================================

local aliases = {
["gris-"]       = "info-light",
["gris+"]       = "secondary",
["vert-clair"]  = "success-light",
["rouge-clair"] = "danger-light",
primary         = "info",
}

-- =============================================================================
-- AUTO-DÉTECTION du mode (cellule tableau vs badge inline)
-- =============================================================================

local function detectMode(content)
local trimmed = mw.text.trim(content)

if trimmed == "" then return "cell" end

-- Indicateurs tableau
if content:match("%|%|") or content:match("!!") or content:match("%|-") then return "cell" end
if trimmed:match("^<code>") then return "cell" end

-- Indicateurs texte inline
if trimmed:match("[💡⚠️🔥ℹ️✓✗🎯📌❌✅⭐🎨]") then return "badge" end
if trimmed:match("'''[^']+'''%s*:") then return "badge" end
if mw.ustring.len(trimmed) > 40 then return "badge" end
if trimmed:match("[%.%,%;%!%?]") and mw.ustring.len(trimmed) > 15 then return "badge" end

-- Court et sans ponctuation → cellule tableau
if mw.ustring.len(trimmed) <= 20 then return "cell" end

return "badge"
end

-- =============================================================================
-- RENDU CELLULE TABLEAU
-- Utilise la classe CSS table-color-* pour le support dark mode automatique
-- =============================================================================

local function renderCell(color, content)
local trimmed = mw.text.trim(content)
-- On utilise la classe CSS → dark mode géré par Common.css
local style = 'class="table-color-cell ' .. color.cssClass .. '"'

if trimmed == "" then
return style .. ' | '
end
return style .. ' | ' .. content
end

-- =============================================================================
-- RENDU BADGE INLINE
-- Utilise les couleurs hex directement (contexte hors tableau)
-- =============================================================================

local function renderBadge(color, content)
local trimmed = mw.text.trim(content)
if trimmed == "" then return "" end

local spanStyle = string.format(
'background-color:%s; color:%s; padding:4px 10px; border-radius:6px; ' ..
'border:1.5px solid %s; display:inline-block; font-weight:500; font-size:0.875em;',
color.bg, color.fg, color.border
)
return string.format('<span style="%s">%s</span>', spanStyle, trimmed)
end

-- =============================================================================
-- FONCTION PRINCIPALE : {{Couleur|nom|contenu}}
-- =============================================================================

function p.cell(frame)
local args = frame:getParent().args
local colorName = mw.text.trim(args[1] or ""):lower()
local content   = args[2] or ""

-- Résolution alias
colorName = aliases[colorName] or colorName
local color = colors[colorName] or colors.light

-- Détection du mode
local mode = detectMode(content)

if mode == "cell" then
return renderCell(color, content)
else
return renderBadge(color, content)
end
end

-- =============================================================================
-- LISTE DES COULEURS DISPONIBLES (pour documentation)
-- Génère un tableau wikitable avec aperçu
-- =============================================================================

function p.list(frame)
local rows = {}

-- Tri par nom
local sorted = {}
for name in pairs(colors) do
table.insert(sorted, name)
end
table.sort(sorted)

for _, name in ipairs(sorted) do
local color = colors[name]

local badge = renderBadge(color, color.label)

-- Cellule : on génère le wikitext de la ligne entière
local cellStyle = string.format(
'background-color:%s; color:%s; padding:8px 12px;',
color.bg, color.fg
)

table.insert(rows, string.format(
'|-\n| %s || %s || style="%s" | %s || <code>table-color-%s</code> || <code>{{Couleur|%s|...}}</code>',
color.label,
badge,
cellStyle,
color.label,
name,
name
))
end

local header = '{| class="wikitable sortable"\n' ..
'! Nom !! Badge !! Cellule !! Classe CSS !! Wikicode\n'

return frame:preprocess(header .. table.concat(rows, '\n') .. '\n|}')
end

-- =============================================================================
-- APERÇU PALETTE (pour Aide:Charte graphique)
-- Génère les {{Card}} avec bgcolor des couleurs de la palette
-- =============================================================================

function p.palette(frame)
local args = frame.args
local group = mw.text.trim(args[1] or "all"):lower()

-- Groupes à afficher
local groups = {
success  = { "success", "success-light" },
danger   = { "danger",  "danger-light"  },
warning  = { "warning", "warning-light" },
info     = { "info",    "info-light"    },
neutral  = { "secondary", "light", "dark" },
}

local targets = {}
if group == "all" then
for _, names in pairs(groups) do
for _, n in ipairs(names) do table.insert(targets, n) end
end
table.sort(targets)
else
targets = groups[group] or { group }
end

local cards = {}
for _, name in ipairs(targets) do
local color = colors[name]
if color then
-- Appel du modèle Card avec bgcolor
local card = string.format(
'{{Card|title=%s|subtitle=%s / %s|bgcolor=%s|colorheight=80|text=Classe : <code>%s</code>}}',
color.label,
color.bg,
color.fg,
color.bg,
color.cssClass
)
table.insert(cards, card)
end
end

local output = '<div class="ds-swatch-grid">\n' ..
table.concat(cards, '\n') ..
'\n</div>'

return frame:preprocess(output)
end

return p
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.