« Module:TableColors » : différence entre les versions
De Nefald
Autres actions
mAucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 2 : | Ligne 2 : | ||
-- ============================================================================= | -- ============================================================================= | ||
-- | -- Palette : uniquement les noms → classe CSS + variables --nefald-* | ||
-- | -- 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" }, | |||
danger = { class = "table-color-danger", label = "Danger", var = "nefald-danger" }, | |||
success = { | 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" }, | |||
label = "Succès | |||
}, | |||
danger = { | |||
label = "Danger | |||
}, | |||
warning = { | |||
label = "Avertissement | |||
}, | |||
info = { | |||
label = "Info | |||
}, | |||
secondary = { | |||
} | } | ||
-- | -- Aliases → entrée canonique | ||
local aliases = { | local aliases = { | ||
vert = "success", | |||
rouge = "danger", | |||
jaune = "warning", | |||
bleu = "info", | |||
primary | 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 | local function resolve(name) | ||
name = mw.text.trim(name or ""):lower() | |||
name = aliases[name] or name | |||
return colors[name], name | |||
end | 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> | |||
-- ============================================================================= | -- ============================================================================= | ||
local | function p.cell(frame) | ||
local | local args = frame:getParent().args | ||
local colorName = mw.text.trim(args[1] or ""):lower() | |||
local | 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 | |||
local | -- Heuristique simple : contenu court sans ponctuation → mode tableau | ||
local trimmed | 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. | color.class, color.var, content) | ||
else | |||
return string.format(' | -- Cellule wikitable | ||
return string.format('class="%s" | %s', color.class, content) | |||
end | |||
end | end | ||
-- ============================================================================= | -- ============================================================================= | ||
-- | -- p.badge — badge inline explicite (pas d'ambiguïté) | ||
-- Usage : {{#invoke:TableColors|badge|success|Texte}} | |||
-- ============================================================================= | -- ============================================================================= | ||
function p. | function p.badge(frame) | ||
local args = frame | local args = frame.args | ||
local | local color, _ = resolve(args[1] or "") | ||
local content | 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 | 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 rows = {} | local rows = {} | ||
local | for _, name in ipairs(order) do | ||
local color = colors[name] | |||
local badge = string.format( | |||
local | '<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 | ||
color. | ) | ||
) | |||
table.insert(rows, string.format( | table.insert(rows, string.format( | ||
'|-\n| %s || %s || | '|-\n| %s || %s || class="%s" | %s || <code>{{Couleur|%s|...}}</code>', | ||
color.label, | color.label, | ||
badge, | badge, | ||
color.class, | |||
color.label, | color.label, | ||
name | |||
name | )) | ||
)) | end | ||
end | |||
local | local wikitext = '{| class="wikitable"\n! Nom !! Badge !! Cellule !! Syntaxe\n' | ||
.. table.concat(rows, '\n') | |||
.. '\n|}' | |||
return frame:preprocess( | return frame:preprocess(wikitext) | ||
end | end | ||
-- ============================================================================= | -- ============================================================================= | ||
-- | -- p.palette — grille de cards pour Aide:Charte graphique | ||
-- | -- Usage : {{#invoke:TableColors|palette}} | ||
-- ============================================================================= | -- ============================================================================= | ||
function p.palette(frame) | function p.palette(frame) | ||
local | 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 | |||
for _, name in ipairs( | |||
local color = colors[name] | |||
local card = string.format( | |||
'{{Card|title=%s|subtitle=%s | |||
color.label, | |||
color. | |||
color. | |||
color. | |||
) | |||
table.insert(cards, card) | |||
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