« Module:TableColors » : différence entre les versions
De Nefald
Autres actions
mAucun résumé des modifications |
Parsed frame:preprocess() |
||
Ligne 34 : | Ligne 34 : | ||
} | } | ||
-- Fonction principale | -- Fonction principale pour styliser une cellule | ||
function p.style(frame) | function p.style(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 content = args[2] | local content = args[2] | ||
-- Gestion des alias | -- Gestion des alias | ||
Ligne 55 : | Ligne 55 : | ||
-- Construction du style | -- Construction du style | ||
local style = string.format( | local style = string.format( | ||
'background-color: %s; color: %s', | 'style="background-color: %s; color: %s"', | ||
color.bg, | color.bg, | ||
color.fg | color.fg | ||
) | ) | ||
-- Si pas de contenu ou contenu vide | -- Si pas de contenu ou contenu vide/nil | ||
if not content | if not content then | ||
return | return style | ||
end | end | ||
-- Si contenu présent | local trimmedContent = mw.text.trim(content) | ||
return | if trimmedContent == "" then | ||
return style | |||
end | |||
-- Si contenu présent, retourner avec séparateur | |||
return style .. ' | ' .. content | |||
end | end | ||
-- Fonction pour lister les couleurs disponibles | -- Fonction pour lister les couleurs disponibles | ||
function p.list(frame) | function p.list(frame) | ||
local | local rows = {} | ||
-- Trier les noms de couleurs | |||
local sorted = {} | local sorted = {} | ||
for name, _ in pairs(colors) do | for name, _ in pairs(colors) do | ||
Ligne 81 : | Ligne 85 : | ||
table.sort(sorted) | table.sort(sorted) | ||
-- Construire les lignes du tableau | |||
for _, name in ipairs(sorted) do | for _, name in ipairs(sorted) do | ||
local color = colors[name] | local color = colors[name] | ||
local row = string.format( | |||
'|-\n| %s || style="background:%s; color:%s; padding:8px 15px; font-weight:500;" | %s || <code>{{Couleur|%s}}</code>', | |||
'| %s || style="background:%s; color:%s; padding:8px 15px; font-weight:500;" | %s || <code> | |||
color.label, | color.label, | ||
color.bg, | color.bg, | ||
Ligne 91 : | Ligne 95 : | ||
color.label, | color.label, | ||
name | name | ||
)) | ) | ||
table.insert(rows, row) | |||
end | end | ||
table. | -- Assembler le tableau complet | ||
local wikitext = '{| class="wikitable sortable"\n! Nom !! Aperçu !! Code\n' | |||
.. table.concat(rows, '\n') | |||
.. '\n|}' | |||
return | -- Parser le wikitext avant de le retourner | ||
return frame:preprocess(wikitext) | |||
end | end | ||
return p | return p |