Aller au contenu

Module:ImageDuJour

De Wikiquestia

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)]

    -- Bloc stylisé
    local html = {}
    table.insert(html, "<div style='max-width:60em; float:left; margin:auto; border:1px solid #aaa; border-radius:10px; box-shadow:1px 1px 3px #ccc; overflow:hidden;'>")
    table.insert(html, "<div style='padding:12px;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>🖼️ Image du jour</div>")
    table.insert(html, "<hr style='border:none; border-top:1px solid #ccc; margin:0; margin-bottom:12px;'>")

    -- Image centrée
    table.insert(html, "<div style='text-align:center;'>")
    table.insert(html, "[[Fichier:" .. image .. "|300px]]")
    table.insert(html, "</div>")

    table.insert(html, "</div>") -- fin padding
    table.insert(html, "</div>") -- fin bloc

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

return p