Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
 
(7 versions intermédiaires par le même utilisateur non affichées)
Ligne 2 : Ligne 2 :


function p.afficher(frame)
function p.afficher(frame)
    -- Utilise #lst pour charger le contenu comme une variable wiki
     local listeTitle = mw.title.new("Modèle:Liste/ImagesDuJour")
     local contenu = frame:preprocess("{{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


     if not contenu or contenu == "" then
    local contenu = listeTitle:getContent()
         return "<span style='color:gray;'>Aucune image trouvée dans la liste.</span>"
     if not contenu then
         return "<span style='color:red;'>Erreur : contenu inaccessible.</span>"
     end
     end


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


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


     local imageChoisie = images[math.random(#images)]
    math.randomseed(os.time())
     local nomFichier = fichiers[math.random(#fichiers)]
    local nomAffiche = nomFichier:gsub("^Fichier:", ""):gsub("%.%a+$", "")


     -- Bloc stylisé avec l'image
     -- Construction du HTML
     local html = {}
     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; padding:12px;'>")
     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='font-size:1.2em; font-weight:bold;'>🎨 Image du jour</div>")
 
     table.insert(html, "<hr style='border:none; border-top:1px solid #aaa; margin:0; margin-bottom:12px;'>")
    table.insert(html, "<div style='padding:12px; text-align:center;'>")
     table.insert(html, "[[Fichier:" .. imageChoisie .. "|300px]]")
     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>")
     table.insert(html, "</div>")



Dernière version du 13 juillet 2025 à 19:06

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