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)
    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)]
    local nomAffiche = nomFichier:gsub("^Fichier:", ""):gsub("%.%a+$", "")

    -- Construction du HTML
    local html = {}
    table.insert(html, "<div style='flex: 0 0 38%; border:1px solid #aaa; border-radius:10px; box-shadow:1px 1px 3px #ccc; overflow:hidden; background:#f8f9fa; display:flex; flex-direction:column; justify-content:space-between;'>")

    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;'>")

    -- Image avec dimensions fixes
    table.insert(html, "<div style='display:inline-block; border:1px solid #aaa; padding:4px; background:white;'>")
    table.insert(html, "[[Fichier:" .. nomFichier .. "|300x200px|center]]")
    table.insert(html, "</div>")

    table.insert(html, "<div style='font-size:90%; color:#555; margin-top:6px;'>[[:Fichier:" .. nomFichier .. "|" .. nomAffiche .. "]]</div>")
    table.insert(html, "</div>")
    table.insert(html, "</div>")

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

return p