« Module:TableColors » : différence entre les versions
De Nefald
Autres actions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| (5 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- Palette | -- ============================================================================= | ||
-- 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" }, | |||
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" }, | |||
info | |||
} | } | ||
-- | -- 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) | 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() | ||
colorName = aliases[colorName] or colorName | |||
local content = args[2] or "" | local content = args[2] or "" | ||
local trimmed = mw.text.trim(content) | |||
-- | |||
local | 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 | |||
local | -- On se fie au premier argument vide = cellule de tableau | ||
if trimmed == "" then | |||
return string.format('class="%s" | ', color.class) | |||
return | end | ||
'<span | |||
color. | -- 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 | 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 = {} | ||
for _, name in ipairs(order) do | |||
for _, name in ipairs( | |||
local color = colors[name] | 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( | table.insert(rows, string.format( | ||
'|-\n| %s || | '|-\n| %s || %s || class="%s" | %s || <code>{{Couleur|%s|...}}</code>', | ||
color.label, | color.label, | ||
badge, | |||
color. | color.class, | ||
color.label, | color.label, | ||
name | name | ||
)) | )) | ||
end | end | ||
local wikitext = '{| class="wikitable"\n! Nom !! Badge !! Cellule !! Syntaxe\n' | |||
local wikitext = '{| class="wikitable | .. table.concat(rows, '\n') | ||
.. table.concat(rows, '\n') | |||
.. '\n|}' | .. '\n|}' | ||
return frame:preprocess(wikitext) | 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 | 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