Module:TableColors
De Nefald
Autres actions
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