Aller au contenu

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

De Wikiquestia
Page créée avec « local p = {} -- Fonction de base, utilise le parser pour exécuter la balise #dpl function p.dpl(args) local frame = mw.getCurrentFrame() local dplArgs = {} for k, v in pairs(args) do table.insert(dplArgs, string.format("%s=%s", k, v)) end local fullCall = "<DPL>\n" .. table.concat(dplArgs, "\n") .. "\n</DPL>" return frame:preprocess(fullCall) end return p »
 
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Fonction de base, utilise le parser pour exécuter la balise #dpl
-- Fonction principale : tire une page au hasard depuis une catégorie et extrait son bloc <!--extrait-->
function p.dpl(args)
function p.personnageDuJour(frame)
     local frame = mw.getCurrentFrame()
     local titrePage = p.selectionAleatoireDepuisCategorie("personnage")
     local dplArgs = {}
     if not titrePage then
        return "<span style='color:red;'>Erreur : aucune page trouvée dans la catégorie.</span>"
    end


     for k, v in pairs(args) do
     local page = mw.title.new(titrePage)
         table.insert(dplArgs, string.format("%s=%s", k, v))
    if not page or not page.exists then
         return "<span style='color:red;'>Erreur : la page '" .. titrePage .. "' n'existe pas.</span>"
     end
     end


     local fullCall = "<DPL>\n" .. table.concat(dplArgs, "\n") .. "\n</DPL>"
     local contenu = page:getContent()
     return frame:preprocess(fullCall)
    if not contenu then
        return "<span style='color:red;'>Erreur : contenu inaccessible.</span>"
    end
 
    local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
    if not extrait then
        return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
    end
 
    -- Affichage stylisé avec titre + extrait
    local html = {}
    table.insert(html, "<div style='border:1px solid #aaa; background:#f8f9fa; padding:12px; margin:12px 0; border-radius:10px; box-shadow:1px 1px 3px #ccc;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold; margin-bottom:8px;'>👤 Personnage du jour</div>")
    table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")
    table.insert(html, "<div style='flex:0 0 200px;'>")
    table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px]]<br/><div style='text-align:center; font-size:90%;'><i>[[" .. titrePage .. "]]</i></div>")
    table.insert(html, "</div>")
    table.insert(html, "<div style='flex:1;'>" .. mw.getCurrentFrame():preprocess(extrait) .. "</div>")
    table.insert(html, "</div></div>")
 
     return table.concat(html, "\n")
end
 
-- Fonction : tire une page au hasard depuis une catégorie (tous espaces de noms)
function p.selectionAleatoireDepuisCategorie(nomCategorie)
    local dpl = require('Module:DynamicPageList').dpl
    local pages = dpl{
        category = nomCategorie,
        count = 500,
        randomcount = 1,
    }
 
    if #pages > 0 then
        return pages[1]
    end
    return nil
end
end


return p
return p

Version du 12 juillet 2025 à 19:04

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

local p = {}

-- Fonction principale : tire une page au hasard depuis une catégorie et extrait son bloc <!--extrait-->
function p.personnageDuJour(frame)
    local titrePage = p.selectionAleatoireDepuisCategorie("personnage")
    if not titrePage then
        return "<span style='color:red;'>Erreur : aucune page trouvée dans la catégorie.</span>"
    end

    local page = mw.title.new(titrePage)
    if not page or not page.exists then
        return "<span style='color:red;'>Erreur : la page '" .. titrePage .. "' n'existe pas.</span>"
    end

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

    local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
    if not extrait then
        return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
    end

    -- Affichage stylisé avec titre + extrait
    local html = {}
    table.insert(html, "<div style='border:1px solid #aaa; background:#f8f9fa; padding:12px; margin:12px 0; border-radius:10px; box-shadow:1px 1px 3px #ccc;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold; margin-bottom:8px;'>👤 Personnage du jour</div>")
    table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")
    table.insert(html, "<div style='flex:0 0 200px;'>")
    table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px]]<br/><div style='text-align:center; font-size:90%;'><i>[[" .. titrePage .. "]]</i></div>")
    table.insert(html, "</div>")
    table.insert(html, "<div style='flex:1;'>" .. mw.getCurrentFrame():preprocess(extrait) .. "</div>")
    table.insert(html, "</div></div>")

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

-- Fonction : tire une page au hasard depuis une catégorie (tous espaces de noms)
function p.selectionAleatoireDepuisCategorie(nomCategorie)
    local dpl = require('Module:DynamicPageList').dpl
    local pages = dpl{
        category = nomCategorie,
        count = 500,
        randomcount = 1,
    }

    if #pages > 0 then
        return pages[1]
    end
    return nil
end

return p