Aller au contenu

Module:Personnagedujour

De Wikiquestia

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