« Module:ImageDuJour » : différence entre les versions
Apparence
Page créée avec « local p = {} -- Liste manuelle de fichiers via une catégorie local fichiers = { "Fichier:GuerreDesTénèbres.jpg", "Fichier:ChuteDeLempire.png", -- ajoute d'autres fichiers ici si tu veux un fallback } -- Fonction utilitaire : sélection aléatoire dans une table local function aleatoire(tab) math.randomseed(os.time()) return tab[math.random(#tab)] end -- Fonction principale function p.depuisCategorie(frame) -- Tu peux remplacer cette... » |
Aucun résumé des modifications |
||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- Fonction utilitaire : sélection aléatoire dans une table | -- Fonction utilitaire : sélection aléatoire dans une table | ||
| Ligne 14 : | Ligne 7 : | ||
end | end | ||
-- Fonction | -- Fonction : récupère les fichiers d'une catégorie | ||
function p.depuisCategorie(frame) | function p.depuisCategorie(frame) | ||
-- | local categorie = frame.args[1] or "Tableau" | ||
local titreCat = mw.title.new("Catégorie:" .. categorie) | |||
if not titreCat or not titreCat.exists then | |||
return "<span style='color:red;'>Catégorie inexistante.</span>" | |||
end | |||
-- Récupère les membres de la catégorie | |||
local membres = titreCat:getMembers() | |||
local fichiers = {} | |||
for _, membre in ipairs(membres) do | |||
if membre.nsText == "Fichier" then | |||
table.insert(fichiers, membre.prefixedText) | |||
end | |||
end | |||
if #fichiers == 0 then | |||
return "<span style='color:red;'>Aucun fichier trouvé dans la catégorie.</span>" | |||
end | |||
return aleatoire(fichiers) | return aleatoire(fichiers) | ||
end | end | ||
return p | return p | ||
Version du 13 juillet 2025 à 09:28
La documentation pour ce module peut être créée à Module:ImageDuJour/doc
local p = {}
-- Fonction utilitaire : sélection aléatoire dans une table
local function aleatoire(tab)
math.randomseed(os.time())
return tab[math.random(#tab)]
end
-- Fonction : récupère les fichiers d'une catégorie
function p.depuisCategorie(frame)
local categorie = frame.args[1] or "Tableau"
local titreCat = mw.title.new("Catégorie:" .. categorie)
if not titreCat or not titreCat.exists then
return "<span style='color:red;'>Catégorie inexistante.</span>"
end
-- Récupère les membres de la catégorie
local membres = titreCat:getMembers()
local fichiers = {}
for _, membre in ipairs(membres) do
if membre.nsText == "Fichier" then
table.insert(fichiers, membre.prefixedText)
end
end
if #fichiers == 0 then
return "<span style='color:red;'>Aucun fichier trouvé dans la catégorie.</span>"
end
return aleatoire(fichiers)
end
return p