Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Liste manuelle des pays
-- Liste manuelle des pages de pays
local listePays = {
local listePays = {
     "Elnastor",
     "Elnastor",
}
}


-- Fonction utilitaire : retourne un nom de page au hasard
-- Tirage au hasard
local function pageAleatoire()
local function pageAleatoire()
     math.randomseed(os.time())
     math.randomseed(os.time())
     return listePays[math.random(#listePays)]
     return listePays[math.random(#listePays)]
end
-- Fonction pour extraire le thème depuis l’infobox
local function extraireTheme(contenu)
    local theme = mw.ustring.match(contenu, "|%s*Theme%s*=%s*(.-)\n")
    if theme then
        return mw.text.trim(theme)
    else
        return "defaut"
    end
end
end


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


Ligne 35 : Ligne 25 :
     end
     end


    -- Extraire le thème depuis l'infobox
    local theme = mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)")
    if not theme then theme = "defaut" end
    -- Extraire le bloc entre <!--extrait--> et <!--/extrait-->
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
     if not extrait then
     if not extrait then
Ligne 40 : Ligne 35 :
     end
     end


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


    -- Construction HTML
     local html = {}
     local html = {}
     table.insert(html, "<div class='" .. theme .. "' style='float:left; max-width:640px; border:1px solid #aaa; background:#f8f9fa; padding:12px; margin:12px 16px 16px 0; border-radius:10px; box-shadow:1px 1px 3px #ccc;'>")
     table.insert(html, "<div class='" .. theme .. "-picto' style='max-width:60em; float:left; margin:auto; 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 style='font-size:1.2em; font-weight:bold; margin-bottom:4px;'>🌍 Pays du jour</div>")
 
     table.insert(html, "<hr style='border:none; border-top:1px solid " .. frame:callParserFunction("Charte|couleur", "charte=" .. theme .. "|type=hr") .. "; margin:0; margin-bottom:12px;'>")
    table.insert(html, "<div style='padding:12px;'>")
     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;'>")
     table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>")


     -- Image + label
     -- Image
     table.insert(html, "<div style='flex:0 0 200px; position:relative;'>")
     table.insert(html, "<div style='flex:0 0 200px; position:relative;'>")
     table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px|class=portrait-image]]")
     table.insert(html, "[[Fichier:" .. titrePage .. ".png|150px|class=portrait-image]]")
     table.insert(html, "<div style='position:absolute; bottom:6px; left:0; width:100%; text-align:center; font-size:80%; font-weight:bold; color:white; background:rgba(0,0,0,0.6); padding:2px;'>Drapeau</div>")
     table.insert(html, "<div style='position:absolute; bottom:6px; left:0; width:100%; text-align:center; font-size:80%; font-weight:bold; color:white; background:rgba(0,0,0,0.6); padding:2px;'>Drapeau</div>")
     table.insert(html, "</div>")
     table.insert(html, "</div>")


     -- Nom + extrait
     -- Titre + extrait
     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>")
     table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
Ligne 60 : Ligne 61 :
     table.insert(html, "</div>")
     table.insert(html, "</div>")


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


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

Version du 12 juillet 2025 à 20:32

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

local p = {}

-- Liste manuelle des pages de pays
local listePays = {
    "Elnastor",
}

-- Tirage au hasard
local function pageAleatoire()
    math.randomseed(os.time())
    return listePays[math.random(#listePays)]
end

-- Fonction principale
function p.paysDuJour(frame)
    local titrePage = pageAleatoire()
    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

    -- Extraire le thème depuis l'infobox
    local theme = mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)")
    if not theme then theme = "defaut" end

    -- Extraire le bloc entre <!--extrait--> et <!--/extrait-->
    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

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

    -- Construction HTML
    local html = {}
    table.insert(html, "<div class='" .. theme .. "-picto' style='max-width:60em; float:left; margin:auto; 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 style='padding:12px;'>")
    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;'>")

    -- Image
    table.insert(html, "<div style='flex:0 0 200px; position:relative;'>")
    table.insert(html, "[[Fichier:" .. titrePage .. ".png|150px|class=portrait-image]]")
    table.insert(html, "<div style='position:absolute; bottom:6px; left:0; width:100%; text-align:center; font-size:80%; font-weight:bold; color:white; background:rgba(0,0,0,0.6); padding:2px;'>Drapeau</div>")
    table.insert(html, "</div>")

    -- Titre + extrait
    table.insert(html, "<div style='flex:1;'>")
    table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
    table.insert(html, mw.getCurrentFrame():preprocess(extrait))
    table.insert(html, "</div>")

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

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

return p