Aller au contenu

« Module:ImageDuJour » : différence entre les versions

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Fonction utilitaire : sélection aléatoire dans une table
function p.afficher(frame)
local function aleatoire(tab)
    -- Récupérer le contenu brut de la page de liste
     math.randomseed(os.time())
    local titreListe = "Modèle:Liste/ImagesDuJour"
     return tab[math.random(#tab)]
     local pageListe = mw.title.new(titreListe)
end
     if not pageListe or not pageListe.exists then
        return "<span style='color:red;'>Erreur : la liste des images n'existe pas.</span>"
    end


-- Fonction : récupère les fichiers d'une catégorie
    local contenu = pageListe:getContent()
function p.depuisCategorie(frame)
     if not contenu then
     local categorie = frame.args[1] or "Tableau"
        return "<span style='color:red;'>Erreur : impossible de lire la liste des images.</span>"
     local titreCat = mw.title.new("Catégorie:" .. categorie)
     end


     if not titreCat or not titreCat.exists then
     -- Extraire tous les fichiers [[Fichier:Nom.ext]]
        return "<span style='color:red;'>Catégorie inexistante.</span>"
    local images = {}
    for fichier in mw.ustring.gmatch(contenu, "%[%[Fichier:([^\n%]]+)%)?%]%]") do
        table.insert(images, fichier)
     end
     end


     -- Récupère les membres de la catégorie
     if #images == 0 then
    local membres = titreCat:getMembers()
        return "<span style='color:gray;'>Aucune image trouvée dans la liste.</span>"
     local fichiers = {}
     end


     for _, membre in ipairs(membres) do
     -- Tirer un fichier au hasard
        if membre.nsText == "Fichier" then
    math.randomseed(os.time())
            table.insert(fichiers, membre.prefixedText)
    local image = images[math.random(#images)]
        end
    end


     if #fichiers == 0 then
     -- Affichage HTML stylisé
        return "<span style='color:red;'>Aucun fichier trouvé dans la catégorie.</span>"
    local html = {}
     end
    table.insert(html, "<div style='text-align:center; margin: 20px auto; max-width:300px;'>")
    table.insert(html, "[[Fichier:" .. image .. "|300px]]")
    table.insert(html, "<div style='font-size:1.1em; margin-top:6px;'>🖼️ Image du jour</div>")
     table.insert(html, "</div>")


     return aleatoire(fichiers)
     return table.concat(html, "\n")
end
end


return p
return p

Version du 13 juillet 2025 à 09:44

La documentation pour ce module peut être créée à Module:ImageDuJour/doc

local p = {}

function p.afficher(frame)
    -- Récupérer le contenu brut de la page de liste
    local titreListe = "Modèle:Liste/ImagesDuJour"
    local pageListe = mw.title.new(titreListe)
    if not pageListe or not pageListe.exists then
        return "<span style='color:red;'>Erreur : la liste des images n'existe pas.</span>"
    end

    local contenu = pageListe:getContent()
    if not contenu then
        return "<span style='color:red;'>Erreur : impossible de lire la liste des images.</span>"
    end

    -- Extraire tous les fichiers [[Fichier:Nom.ext]]
    local images = {}
    for fichier in mw.ustring.gmatch(contenu, "%[%[Fichier:([^\n%]]+)%)?%]%]") do
        table.insert(images, fichier)
    end

    if #images == 0 then
        return "<span style='color:gray;'>Aucune image trouvée dans la liste.</span>"
    end

    -- Tirer un fichier au hasard
    math.randomseed(os.time())
    local image = images[math.random(#images)]

    -- Affichage HTML stylisé
    local html = {}
    table.insert(html, "<div style='text-align:center; margin: 20px auto; max-width:300px;'>")
    table.insert(html, "[[Fichier:" .. image .. "|300px]]")
    table.insert(html, "<div style='font-size:1.1em; margin-top:6px;'>🖼️ Image du jour</div>")
    table.insert(html, "</div>")

    return table.concat(html, "\n")
end

return p