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)
création
 
Hiob (discussion | contributions)
Aucun résumé des modifications
 
(9 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Palette inspirée de Bootstrap 5 + Citizen
-- =============================================================================
-- Palette : uniquement les noms → classe CSS + variables --nefald-*
-- Pas de couleurs en dur : tout est géré par Citizen.css
-- =============================================================================
 
local colors = {
local colors = {
    -- Variantes de succès (vert)
     success = { class = "table-color-success",   label = "Succès",       var = "nefald-success" },
     success = { bg = "#d1e7dd", fg = "#0f5132", label = "Succès" },
     danger  = { class = "table-color-danger",   label = "Danger",       var = "nefald-danger"   },
     ["success-light"] = { bg = "#e8f5e9", fg = "#1b5e20", label = "Succès clair" },
     warning  = { class = "table-color-warning",   label = "Avertissement", var = "nefald-warning" },
   
     info    = { class = "table-color-info",     label = "Info",         var = "nefald-info"    },
    -- Variantes de danger (rouge)
     secondary = { class = "table-color-secondary", label = "Neutre",       var = "nefald-neutral" },
    danger = { bg = "#f8d7da", fg = "#842029", label = "Danger" },
     ["danger-light"] = { bg = "#ffebee", fg = "#c62828", label = "Danger clair" },
   
    -- Variantes d'avertissement (jaune/orange)
    warning = { bg = "#fff3cd", fg = "#664d03", label = "Avertissement" },
    ["warning-light"] = { bg = "#fffde7", fg = "#f57f17", label = "Avertissement clair" },
   
    -- Variantes d'information (bleu)
     info = { bg = "#cfe2ff", fg = "#084298", label = "Info" },
     ["info-light"] = { bg = "#e3f2fd", fg = "#01579b", label = "Info clair" },
   
    -- Variantes neutres
    primary = { bg = "#cfe2ff", fg = "#052c65", label = "Primaire" },
    secondary = { bg = "#e2e3e5", fg = "#41464b", label = "Secondaire" },
     light = { bg = "#f8f9fa", fg = "#495057", label = "Clair" },
     dark = { bg = "#ced4da", fg = "#212529", label = "Foncé" },
   
    -- Compatibilité anciens noms
    vert = { bg = "#d1e7dd", fg = "#0f5132", label = "Vert" },
    rouge = { bg = "#f8d7da", fg = "#842029", label = "Rouge" },
    jaune = { bg = "#fff3cd", fg = "#664d03", label = "Jaune" },
    rose = { bg = "#f8d7da", fg = "#842029", label = "Rose" },
    gris = { bg = "#e2e3e5", fg = "#41464b", label = "Gris" },
    bleu = { bg = "#cfe2ff", fg = "#084298", label = "Bleu" },
}
}


-- Fonction principale
-- Aliases → entrée canonique
function p.style(frame)
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 args = frame:getParent().args
     local colorName = mw.text.trim(args[1] or ""):lower()
     local colorName = mw.text.trim(args[1] or ""):lower()
    local customLabel = args[2]
   
    -- Gestion des alias
    local aliases = {
        ["gris-"] = "info-light",
        ["gris+"] = "secondary",
        ["vert-clair"] = "success-light",
        ["rouge-clair"] = "danger-light",
    }
   
     colorName = aliases[colorName] or colorName
     colorName = aliases[colorName] or colorName
      
     local content = args[2] or ""
     -- Récupération de la couleur
     local trimmed = mw.text.trim(content)
     local color = colors[colorName] or colors.light
 
      
     local color = colors[colorName]
     -- Construction du style avec classes CSS
     if not color then
     local style = string.format(
        return content
        'class="table-color-cell table-color-%s" style="background-color: %s; color: %s"',
    end
         colorName:gsub("%W", "-"),
 
         color.bg,
     -- Détection du contexte : tableau si le contenu est court et sans ponctuation lourde
        color.fg
     local isTableMode = frame:getParent():getTitle() == "" or
     )
         (trimmed:len() < 60 and not trimmed:match("[%.%!%?]") and not trimmed:match("\n"))
   
 
     -- Ajout du label si fourni
    -- Forcer texte si le parent est un wikitable en cours de parsing
     local label = customLabel and mw.text.trim(customLabel) or ""
    -- On se fie au premier argument vide = cellule de tableau
     if label ~= "" then
    if trimmed == "" then
         return style .. ' | ' .. label
         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
     else
         return style
        -- Cellule wikitable
         return string.format('class="%s" | %s', color.class, content)
     end
     end
end
end


-- Fonction pour lister les couleurs disponibles
-- =============================================================================
-- 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)
function p.list(frame)
     local output = '{| class="wikitable sortable"\n! Nom !! Aperçu !! Code\n'
     local rows = {}
   
 
    local sorted = {}
     for _, name in ipairs(order) do
    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 color = colors[name]
         output = output .. string.format(
 
             '|-\n| %s || style="background:%s; color:%s; padding:5px 10px;" | %s || <code>{{Couleur|%s}}</code>\n',
         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,
             color.label,
             color.bg,
             badge,
             color.fg,
             color.class,
             color.label,
             color.label,
             name
             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
     end
   
 
     return output .. '|}'
     local output = '<div class="ds-swatch-grid">\n'
        .. table.concat(cards, '\n')
        .. '\n</div>'
 
    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.