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)
mAucun résumé des modifications
Hiob (discussion | contributions)
Aucun résumé des modifications
 
Ligne 2 : Ligne 2 :


-- =============================================================================
-- =============================================================================
-- PALETTE alignée sur les classes CSS table-color-*
-- Palette : uniquement les noms → classe CSS + variables --nefald-*
-- Couleurs identiques à celles définies dans ton Common.css/Citizen.css
-- Pas de couleurs en dur : tout est géré par Citizen.css
-- =============================================================================
-- =============================================================================


local colors = {
local colors = {
 
    success = { class = "table-color-success",   label = "Succès",       var = "nefald-success" },
-- Succès (vert) — table-color-success
    danger   = { class = "table-color-danger",   label = "Danger",       var = "nefald-danger"   },
success = {
    warning = { class = "table-color-warning",   label = "Avertissement", var = "nefald-warning" },
bg = "#d1e7dd",
    info     = { class = "table-color-info",     label = "Info",         var = "nefald-info"     },
fg = "#0f5132",
    secondary = { class = "table-color-secondary", label = "Neutre",       var = "nefald-neutral"  },
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 → entrée canonique
-- ALIASES
-- =============================================================================
 
local aliases = {
local aliases = {
["gris-"]      = "info-light",
    vert    = "success",
["gris+"]      = "secondary",
    rouge  = "danger",
["vert-clair"] = "success-light",
    jaune  = "warning",
["rouge-clair"] = "danger-light",
    bleu    = "info",
primary         = "info",
    gris    = "secondary",
    neutre = "secondary",
    neutral = "secondary",
    primary = "info",
}
}
-- Ordre d'affichage stable
local order = { "success", "danger", "warning", "info", "secondary" }


-- =============================================================================
-- =============================================================================
-- AUTO-DÉTECTION du mode (cellule tableau vs badge inline)
-- Résolution d'un nom (gère alias + trim + lowercase)
-- =============================================================================
-- =============================================================================


local function detectMode(content)
local function resolve(name)
local trimmed = mw.text.trim(content)
    name = mw.text.trim(name or ""):lower()
 
    name = aliases[name] or name
if trimmed == "" then return "cell" end
    return colors[name], name
 
-- 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
end


-- =============================================================================
-- =============================================================================
-- RENDU CELLULE TABLEAU
-- p.cell — génère une cellule de tableau OU un badge inline
-- Utilise la classe CSS table-color-* pour le support dark mode automatique
--
-- Usage tableau  : {{Couleur|success|Texte}}  → style="..." | Texte
-- Usage texte    : {{Couleur|danger|Texte}}  → <span class="...">Texte</span>
-- =============================================================================
-- =============================================================================


local function renderCell(color, content)
function p.cell(frame)
local trimmed = mw.text.trim(content)
    local args = frame:getParent().args
-- On utilise la classe CSS → dark mode géré par Common.css
    local colorName = mw.text.trim(args[1] or ""):lower()
local style = 'class="table-color-cell ' .. color.cssClass .. '"'
    colorName = aliases[colorName] or colorName
    local content = args[2] or ""
    local trimmed = mw.text.trim(content)
 
    local color = colors[colorName]
    if not color then
        return content
    end


if trimmed == "" then
    -- Détection du contexte : tableau si le contenu est court et sans ponctuation lourde
return style .. ' | '
    local isTableMode = frame:getParent():getTitle() == "" or
end
        (trimmed:len() < 60 and not trimmed:match("[%.%!%?]") and not trimmed:match("\n"))
return style .. ' | ' .. content
end


-- =============================================================================
    -- Forcer texte si le parent est un wikitable en cours de parsing
-- RENDU BADGE INLINE
    -- On se fie au premier argument vide = cellule de tableau
-- Utilise les couleurs hex directement (contexte hors tableau)
    if trimmed == "" then
-- =============================================================================
        return string.format('class="%s" | ', color.class)
    end


local function renderBadge(color, content)
    -- Heuristique simple : contenu court sans ponctuation → mode tableau
local trimmed = mw.text.trim(content)
    local hasLong      = trimmed:len() > 80
if trimmed == "" then return "" end
    local hasPunct    = trimmed:match("[%.!%?;]") ~= nil
    local hasNewline  = trimmed:match("\n") ~= nil


local spanStyle = string.format(
    if hasLong or hasPunct or hasNewline then
'background-color:%s; color:%s; padding:4px 10px; border-radius:6px; ' ..
        -- Badge inline
'border:1.5px solid %s; display:inline-block; font-weight:500; font-size:0.875em;',
        return string.format('<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
color.bg, color.fg, color.border
            color.class, color.var, content)
)
    else
return string.format('<span style="%s">%s</span>', spanStyle, trimmed)
        -- Cellule wikitable
        return string.format('class="%s" | %s', color.class, content)
    end
end
end


-- =============================================================================
-- =============================================================================
-- FONCTION PRINCIPALE : {{Couleur|nom|contenu}}
-- p.badge — badge inline explicite (pas d'ambiguïté)
-- Usage : {{#invoke:TableColors|badge|success|Texte}}
-- =============================================================================
-- =============================================================================


function p.cell(frame)
function p.badge(frame)
local args = frame:getParent().args
    local args = frame.args
local colorName = mw.text.trim(args[1] or ""):lower()
    local color, _ = resolve(args[1] or "")
local content   = args[2] or ""
    local content = args[2] or ""
    if not color then return content end


-- Résolution alias
    return string.format(
colorName = aliases[colorName] or colorName
        '<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
local color = colors[colorName] or colors.light
        color.class, color.var, content
 
    )
-- Détection du mode
local mode = detectMode(content)
 
if mode == "cell" then
return renderCell(color, content)
else
return renderBadge(color, content)
end
end
end


-- =============================================================================
-- =============================================================================
-- LISTE DES COULEURS DISPONIBLES (pour documentation)
-- p.list — tableau de référence de toutes les couleurs
-- Génère un tableau wikitable avec aperçu
-- Usage : {{#invoke:TableColors|list}}
-- =============================================================================
-- =============================================================================


function p.list(frame)
function p.list(frame)
local rows = {}
    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)
    for _, name in ipairs(order) do
        local color = colors[name]


-- Cellule : on génère le wikitext de la ligne entière
        local badge = string.format(
local cellStyle = string.format(
            '<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
'background-color:%s; color:%s; padding:8px 12px;',
            color.class, color.var, color.label
color.bg, color.fg
        )
)


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


local header = '{| class="wikitable sortable"\n' ..
    local wikitext = '{| class="wikitable"\n! Nom !! Badge !! Cellule !! Syntaxe\n'
'! Nom !! Badge !! Cellule !! Classe CSS !! Wikicode\n'
        .. table.concat(rows, '\n')
        .. '\n|}'


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


-- =============================================================================
-- =============================================================================
-- APERÇU PALETTE (pour Aide:Charte graphique)
-- p.palette — grille de cards pour Aide:Charte graphique
-- Génère les {{Card}} avec bgcolor des couleurs de la palette
-- Usage : {{#invoke:TableColors|palette}}
-- =============================================================================
-- =============================================================================


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


-- Groupes à afficher
     for _, name in ipairs(order) do
local groups = {
        local color = colors[name]
success  = { "success", "success-light" },
        local card = string.format(
danger  = { "danger",  "danger-light"  },
            '{{Card|title=%s|subtitle=var(--%s-bg)|bgcolor=var(--%s-bg)|colorheight=80|text=<code>%s</code>}}',
warning  = { "warning", "warning-light" },
            color.label,
info     = { "info",    "info-light"    },
            color.var,
neutral  = { "secondary", "light", "dark" },
            color.var,
}
            color.class
 
        )
local targets = {}
        table.insert(cards, card)
if group == "all" then
    end
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' ..
    local output = '<div class="ds-swatch-grid">\n'
table.concat(cards, '\n') ..
        .. table.concat(cards, '\n')
'\n</div>'
        .. '\n</div>'


return frame:preprocess(output)
    return frame:preprocess(output)
end
end


return p
return p

Dernière version du 2 mars 2026 à 14:41

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

local p = {}

-- =============================================================================
-- Palette : uniquement les noms → classe CSS + variables --nefald-*
-- Pas de couleurs en dur : tout est géré par Citizen.css
-- =============================================================================

local colors = {
    success  = { class = "table-color-success",   label = "Succès",        var = "nefald-success"  },
    danger   = { class = "table-color-danger",    label = "Danger",        var = "nefald-danger"   },
    warning  = { class = "table-color-warning",   label = "Avertissement", var = "nefald-warning"  },
    info     = { class = "table-color-info",      label = "Info",          var = "nefald-info"     },
    secondary = { class = "table-color-secondary", label = "Neutre",       var = "nefald-neutral"  },
}

-- Aliases → entrée canonique
local aliases = {
    vert    = "success",
    rouge   = "danger",
    jaune   = "warning",
    bleu    = "info",
    gris    = "secondary",
    neutre  = "secondary",
    neutral = "secondary",
    primary = "info",
}

-- Ordre d'affichage stable
local order = { "success", "danger", "warning", "info", "secondary" }

-- =============================================================================
-- Résolution d'un nom (gère alias + trim + lowercase)
-- =============================================================================

local function resolve(name)
    name = mw.text.trim(name or ""):lower()
    name = aliases[name] or name
    return colors[name], name
end

-- =============================================================================
-- p.cell — génère une cellule de tableau OU un badge inline
--
-- Usage tableau  : {{Couleur|success|Texte}}  → style="..." | Texte
-- Usage texte    : {{Couleur|danger|Texte}}   → <span class="...">Texte</span>
-- =============================================================================

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

    local color = colors[colorName]
    if not color then
        return content
    end

    -- Détection du contexte : tableau si le contenu est court et sans ponctuation lourde
    local isTableMode = frame:getParent():getTitle() == "" or
        (trimmed:len() < 60 and not trimmed:match("[%.%!%?]") and not trimmed:match("\n"))

    -- Forcer texte si le parent est un wikitable en cours de parsing
    -- On se fie au premier argument vide = cellule de tableau
    if trimmed == "" then
        return string.format('class="%s" | ', color.class)
    end

    -- Heuristique simple : contenu court sans ponctuation → mode tableau
    local hasLong      = trimmed:len() > 80
    local hasPunct     = trimmed:match("[%.!%?;]") ~= nil
    local hasNewline   = trimmed:match("\n") ~= nil

    if hasLong or hasPunct or hasNewline then
        -- Badge inline
        return string.format('<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
            color.class, color.var, content)
    else
        -- Cellule wikitable
        return string.format('class="%s" | %s', color.class, content)
    end
end

-- =============================================================================
-- p.badge — badge inline explicite (pas d'ambiguïté)
-- Usage : {{#invoke:TableColors|badge|success|Texte}}
-- =============================================================================

function p.badge(frame)
    local args = frame.args
    local color, _ = resolve(args[1] or "")
    local content = args[2] or ""
    if not color then return content end

    return string.format(
        '<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
        color.class, color.var, content
    )
end

-- =============================================================================
-- p.list — tableau de référence de toutes les couleurs
-- Usage : {{#invoke:TableColors|list}}
-- =============================================================================

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

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

        local badge = string.format(
            '<span class="%s" style="padding:4px 10px;border-radius:6px;display:inline-block;font-weight:500;border-left:none;border:1.5px solid var(--%s-border)">%s</span>',
            color.class, color.var, color.label
        )

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

    local wikitext = '{| class="wikitable"\n! Nom !! Badge !! Cellule !! Syntaxe\n'
        .. table.concat(rows, '\n')
        .. '\n|}'

    return frame:preprocess(wikitext)
end

-- =============================================================================
-- p.palette — grille de cards pour Aide:Charte graphique
-- Usage : {{#invoke:TableColors|palette}}
-- =============================================================================

function p.palette(frame)
    local cards = {}

    for _, name in ipairs(order) do
        local color = colors[name]
        local card = string.format(
            '{{Card|title=%s|subtitle=var(--%s-bg)|bgcolor=var(--%s-bg)|colorheight=80|text=<code>%s</code>}}',
            color.label,
            color.var,
            color.var,
            color.class
        )
        table.insert(cards, card)
    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.