Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
 
(15 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Liste manuelle des pages de pays
-- Fonction pour lire la liste depuis Liste/Pays
local listePays = {
local function lireListeDepuisPage()
     "Elnastor",
    local titreListe = mw.title.new("Liste/Pays")
}
    if not titreListe or not titreListe.exists then
        return {}
    end
 
    local contenu = titreListe:getContent()
     if not contenu then
        return {}
    end
 
    local resultats = {}
 
    for nom in mw.ustring.gmatch(contenu, "[^\n]+") do
        nom = mw.text.trim(nom)
        nom = mw.ustring.gsub(nom, "^[%s%p%c‣•·‥●◦‧]+", "")
        nom = mw.ustring.gsub(nom, " ", "")
        if nom ~= "" and not nom:match("<") then -- ignore les balises HTML
            table.insert(resultats, nom)
        end
    end
 
    return resultats
end
 


-- Tirage au hasard
-- Tirage au hasard
local function pageAleatoire()
local function pageAleatoire()
     math.randomseed(os.time())
     math.randomseed(os.time())
     return listePays[math.random(#listePays)]
    local liste = lireListeDepuisPage()
    if #liste == 0 then return nil end
     return liste[math.random(#liste)]
end
end


Ligne 15 : Ligne 39 :
function p.paysDuJour(frame)
function p.paysDuJour(frame)
     local titrePage = pageAleatoire()
     local titrePage = pageAleatoire()
     local page = mw.title.new(titrePage)
     local pageExiste = false
    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 titrePage then
    if not contenu then
        titrePage = nil
         return "<span style='color:red;'>Erreur : contenu inaccessible.</span>"
    else
        local page = mw.title.new(titrePage)
         pageExiste = page and page.exists
     end
     end


     -- Extraire le thème depuis l'infobox
     local contenu = (pageExiste and mw.title.new(titrePage):getContent()) or nil
     local theme = mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)")
     local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut"
     if not theme then theme = "defaut" end
     local extrait = contenu and mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->") or nil


    -- Extraire le bloc entre <!--extrait--> et <!--/extrait-->
     local texteExtrait = extrait and mw.getCurrentFrame():preprocess(extrait)
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
         or "<span style='color:gray;'>Aucun extrait disponible pour ce pays.</span>"
    if not extrait then
         return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
    end


    -- Charger Module:Charte pour récupérer la couleur
     local charteModule = require("Module:Charte")
     local charteModule = require("Module:Charte")
     local couleurHR = charteModule.couleur{ args = { charte = theme, type = "hr" } }
     local couleurHR = charteModule.couleur{ args = { charte = theme, type = "hr" } }


    -- Construction HTML
     local html = {}
     local html = {}
     table.insert(html, "<div class='" .. theme .. "-picto' style='max-width:54%; min-width:54%; float:right; margin-left:1.5em; margin-bottom:2em; border:1px solid #aaa; border-radius:10px; box-shadow:1px 1px 3px #ccc; overflow:hidden; position:relative; background-repeat:no-repeat; background-position:right center; background-size:cover;'>")
     table.insert(html, "<div class='" .. theme .. "-picto' style='flex: 1 1 54%; border:1px solid #aaa; border-radius:10px; box-shadow:1px 1px 3px #ccc; overflow:hidden; background-repeat:no-repeat; background-position:right center; background-size:cover; display:flex; flex-direction:column;'>")


     table.insert(html, "<div style='padding:12px;'>")
     table.insert(html, "<div style='padding:12px; flex:1;'>")
     table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>Pays du jour</div>")
     table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>Pays du jour</div>")
     table.insert(html, "<hr style='border:none; border-top:1px solid " .. couleurHR .. "; margin:0; margin-bottom:12px;'>")
     table.insert(html, "<hr style='border:none; border-top:1px solid " .. couleurHR .. "; margin:0; margin-bottom:12px;'>")
Ligne 49 : Ligne 67 :
     table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")
     table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")


     -- Image sans étiquette
     -- Partie gauche (image)
     table.insert(html, "<div style='flex:0 0 auto; width:150px;'>")
     table.insert(html, "<div style='flex:0 0 200px;'>")
     table.insert(html, "[[Fichier:Bannière " .. titrePage .. ".png|150px|class=portrait-image]]")
     if titrePage and pageExiste then
        table.insert(html, "[[Fichier:Bannière " .. titrePage .. ".png|150px|class=portrait-image]]")
    else
        table.insert(html, "<span style='color:gray;'>Aucune image disponible</span>")
    end
     table.insert(html, "</div>")
     table.insert(html, "</div>")


     -- Titre + extrait + lien
     -- Partie droite (texte)
     table.insert(html, "<div style='flex:1;'>")
     table.insert(html, "<div style='flex:1;'>")
     table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
     if titrePage then
     table.insert(html, mw.getCurrentFrame():preprocess(extrait))
        table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
     table.insert(html, "<div style='text-align:right; margin-top:10px; font-size:90%;'>[["
     else
        .. titrePage .. "|→ Vers l’article]]</div>")
        table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>Page inconnue</div>")
     table.insert(html, "</div>")
    end
 
    table.insert(html, texteExtrait)
 
     if titrePage and pageExiste then
        table.insert(html, "<div style='text-align:right; font-size:90%; margin-top:8px;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
     end


     table.insert(html, "</div>") -- fin flex
     table.insert(html, "</div>") -- fermeture texte
     table.insert(html, "</div>") -- fin padding
     table.insert(html, "</div>") -- fermeture flex
     table.insert(html, "</div>") -- fin bloc principal
     table.insert(html, "</div></div>") -- fermeture padding + bloc global


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

Dernière version du 18 juillet 2025 à 17:20

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

local p = {}

-- Fonction pour lire la liste depuis Liste/Pays
local function lireListeDepuisPage()
    local titreListe = mw.title.new("Liste/Pays")
    if not titreListe or not titreListe.exists then
        return {}
    end

    local contenu = titreListe:getContent()
    if not contenu then
        return {}
    end

    local resultats = {}

    for nom in mw.ustring.gmatch(contenu, "[^\n]+") do
        nom = mw.text.trim(nom)
        nom = mw.ustring.gsub(nom, "^[%s%p%c‣•·‥●◦‧]+", "")
        nom = mw.ustring.gsub(nom, "&nbsp;", "")
        if nom ~= "" and not nom:match("<") then -- ignore les balises HTML
            table.insert(resultats, nom)
        end
    end

    return resultats
end


-- Tirage au hasard
local function pageAleatoire()
    math.randomseed(os.time())
    local liste = lireListeDepuisPage()
    if #liste == 0 then return nil end
    return liste[math.random(#liste)]
end

-- Fonction principale
function p.paysDuJour(frame)
    local titrePage = pageAleatoire()
    local pageExiste = false

    if not titrePage then
        titrePage = nil
    else
        local page = mw.title.new(titrePage)
        pageExiste = page and page.exists
    end

    local contenu = (pageExiste and mw.title.new(titrePage):getContent()) or nil
    local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut"
    local extrait = contenu and mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->") or nil

    local texteExtrait = extrait and mw.getCurrentFrame():preprocess(extrait)
        or "<span style='color:gray;'>Aucun extrait disponible pour ce pays.</span>"

    local charteModule = require("Module:Charte")
    local couleurHR = charteModule.couleur{ args = { charte = theme, type = "hr" } }

    local html = {}
    table.insert(html, "<div class='" .. theme .. "-picto' style='flex: 1 1 54%; border:1px solid #aaa; border-radius:10px; box-shadow:1px 1px 3px #ccc; overflow:hidden; background-repeat:no-repeat; background-position:right center; background-size:cover; display:flex; flex-direction:column;'>")

    table.insert(html, "<div style='padding:12px; flex:1;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>Pays du jour</div>")
    table.insert(html, "<hr style='border:none; border-top:1px solid " .. couleurHR .. "; margin:0; margin-bottom:12px;'>")

    table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")

    -- Partie gauche (image)
    table.insert(html, "<div style='flex:0 0 200px;'>")
    if titrePage and pageExiste then
        table.insert(html, "[[Fichier:Bannière " .. titrePage .. ".png|150px|class=portrait-image]]")
    else
        table.insert(html, "<span style='color:gray;'>Aucune image disponible</span>")
    end
    table.insert(html, "</div>")

    -- Partie droite (texte)
    table.insert(html, "<div style='flex:1;'>")
    if titrePage then
        table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
    else
        table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>Page inconnue</div>")
    end

    table.insert(html, texteExtrait)

    if titrePage and pageExiste then
        table.insert(html, "<div style='text-align:right; font-size:90%; margin-top:8px;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
    end

    table.insert(html, "</div>") -- fermeture texte
    table.insert(html, "</div>") -- fermeture flex
    table.insert(html, "</div></div>") -- fermeture padding + bloc global

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

return p