Aller au contenu

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

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


-- Liste manuelle des pages de personnages
-- Fonction pour charger la liste depuis la page Liste/Personnages
local listePersonnages = {
local function getListeDepuisPage()
     "Gore Sinkelk",
     local titreListe = mw.title.new("Liste/Personnages")
     "Borald Kan",
     if not titreListe or not titreListe.exists then
     "Harald Kan",
        return nil, "<span style='color:red;'>Erreur : la page de liste n'existe pas.</span>"
     "Cerald Kan",
     end
    "Imrald Kan",
 
     "Aldwimn Myłähył",
    local contenu = titreListe:getContent()
     "Kyoto Anashi",
    if not contenu then
     "Dante Terhunt",
        return nil, "<span style='color:red;'>Erreur : contenu de la liste inaccessible.</span>"
     "Seliaroth Melkitim",
     end
}
 
    local liste = {}
    for ligne in mw.text.gsplit(contenu, "\n", true) do
        ligne = mw.text.trim(ligne)
        if ligne ~= "" and not ligne:match("^<.->") then
            table.insert(liste, ligne)
        end
     end
 
     if #liste == 0 then
        return nil, "<span style='color:gray;'>Aucun personnage trouvé dans la liste.</span>"
     end
 
     return liste
end


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


-- Fonction principale
-- Fonction principale
function p.personnageDuJour(frame)
function p.Personnagedujour(frame)
     local titrePage = pageAleatoire()
    local liste, err = getListeDepuisPage()
    if not liste then return err end
 
     local titrePage = pageAleatoire(liste)
     local page = mw.title.new(titrePage)
     local page = mw.title.new(titrePage)
     if not page or not page.exists then
     local pageExiste = page and page.exists
        return "<span style='color:red;'>Erreur : la page '" .. titrePage .. "' n'existe pas.</span>"
    end


     local contenu = page:getContent()
     local contenu = pageExiste and page:getContent() or ""
     if not contenu then
     local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut"
        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*%-%->")
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
     if not extrait then
     if not extrait then
         return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
         extrait = "<span style='color:gray;'>Aucun extrait disponible.</span>"
     end
     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,
     table.insert(html, "<div class='" .. theme .. "-picto' style='flex: 1 1 43%; margin-right:1.5em; 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;'>")
        "<div class='" .. theme .. "-picto' style='max-width:45%; min-width:45%; float:left; margin-right: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 style='padding:12px;'>")
     table.insert(html, "<div style='padding:12px; flex:1;'>")
     table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>Personnage du jour</div>")
     table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>Personnage du jour</div>")
     table.insert(html,
     table.insert(html, "<hr style='border:none; border-top:1px solid " .. couleurHR .. "; margin:0; margin-bottom:12px;'>")
        "<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 avec étiquette "Portrait"
     -- Bloc image
     table.insert(html, "<div style='flex:0 0 auto; width:150px; position:relative;'>")
     table.insert(html, "<div style='flex:0 0 auto; width:150px; position:relative;'>")
     table.insert(html, "[[Fichier:Portrait " .. titrePage .. ".png|150px|class=portrait-image]]")
     table.insert(html, "[[Fichier:Portrait " .. titrePage .. ".png|150px|class=portrait-image]]")
     table.insert(html,
     table.insert(html, "<div style='position:absolute; bottom:0; left:0; width:100%; text-align:center; font-size:80%; font-weight:bold; color:white; background:rgba(0,0,0,0.6); padding:2px;'>Portrait</div>")
        "<div style='position:absolute; bottom:0; left:0; width:100%; text-align:center; font-size:80%; font-weight:bold; color:white; background:rgba(0,0,0,0.6); padding:2px;'>Portrait</div>")
     table.insert(html, "</div>")
     table.insert(html, "</div>")


     -- Titre + extrait + lien
     -- Bloc texte
     table.insert(html, "<div style='flex:1;'>")
     table.insert(html, "<div style='flex:1;'>")
     table.insert(html,
     if pageExiste then
        "<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>")
    else
table.insert(html, "<div style='font-size:1.1em; font-weight:bold; margin-bottom:4px;'>[[" .. titrePage .. "]]</div>")
    end
     table.insert(html, mw.getCurrentFrame():preprocess(extrait))
     table.insert(html, mw.getCurrentFrame():preprocess(extrait))
     table.insert(html,
     if pageExiste then
        "<div style='text-align:right; margin-top:10px; font-size:90%;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
        table.insert(html, "<div style='text-align:right; margin-top:10px; font-size:90%;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
     table.insert(html, "</div>") -- fin colonne droite
    end
 
     table.insert(html, "</div></div></div></div>")
    table.insert(html, "</div>") -- fin flex container
    table.insert(html, "</div>") -- fin padding
    table.insert(html, "</div>") -- fin bloc principal


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

Dernière version du 15 juillet 2025 à 15:27

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

local p = {}

-- Fonction pour charger la liste depuis la page Liste/Personnages
local function getListeDepuisPage()
    local titreListe = mw.title.new("Liste/Personnages")
    if not titreListe or not titreListe.exists then
        return nil, "<span style='color:red;'>Erreur : la page de liste n'existe pas.</span>"
    end

    local contenu = titreListe:getContent()
    if not contenu then
        return nil, "<span style='color:red;'>Erreur : contenu de la liste inaccessible.</span>"
    end

    local liste = {}
    for ligne in mw.text.gsplit(contenu, "\n", true) do
        ligne = mw.text.trim(ligne)
        if ligne ~= "" and not ligne:match("^<.->") then
            table.insert(liste, ligne)
        end
    end

    if #liste == 0 then
        return nil, "<span style='color:gray;'>Aucun personnage trouvé dans la liste.</span>"
    end

    return liste
end

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

-- Fonction principale
function p.Personnagedujour(frame)
    local liste, err = getListeDepuisPage()
    if not liste then return err end

    local titrePage = pageAleatoire(liste)
    local page = mw.title.new(titrePage)
    local pageExiste = page and page.exists

    local contenu = pageExiste and page:getContent() or ""
    local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut"

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

    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 43%; margin-right:1.5em; 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;'>Personnage 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;'>")

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

    -- Bloc texte
    table.insert(html, "<div style='flex:1;'>")
    if pageExiste 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;'>[[" .. titrePage .. "]]</div>")
    end
    table.insert(html, mw.getCurrentFrame():preprocess(extrait))
    if pageExiste then
        table.insert(html, "<div style='text-align:right; margin-top:10px; font-size:90%;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
    end
    table.insert(html, "</div></div></div></div>")

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

return p