Aller au contenu

Module:ImageDuJour

De Wikiquestia

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

local p = {}

-- Récupère une image aléatoire depuis Modèle:Liste/ImagesDuJour
function p.afficher(frame)
    local listeTitle = mw.title.new("Modèle:Liste/ImagesDuJour")
    if not listeTitle or not listeTitle.exists then
        return "<span style='color:red;'>Erreur : la liste des images n'existe pas.</span>"
    end

    local contenu = listeTitle:getContent()
    if not contenu then
        return "<span style='color:red;'>Erreur : contenu inaccessible.</span>"
    end

    local fichiers = {}
    for ligne in mw.text.gsplit(contenu, "\n", true) do
        ligne = mw.text.trim(ligne)
        if ligne ~= "" then
            table.insert(fichiers, ligne)
        end
    end

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

    math.randomseed(os.time())
    local nomFichier = fichiers[math.random(#fichiers)]

    -- HTML 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; background:#f8f9fa;'>")
    table.insert(html, "<div style='padding:12px; text-align:center;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold; margin-bottom:8px;'>Image du jour</div>")
    table.insert(html, "<hr style='border:none; border-top:1px solid #ccc; margin:0; margin-bottom:12px;'>")
    table.insert(html, "[[Fichier:" .. nomFichier .. "|300px]]<br>")
    table.insert(html, "<div style='font-size:90%; color:#555; margin-top:6px;'>[[:Fichier:" .. nomFichier .. "]]</div>")
    table.insert(html, "</div></div>")

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

return p