Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 10 : Ligne 10 :
     math.randomseed(os.time())
     math.randomseed(os.time())
     return listePersonnages[math.random(#listePersonnages)]
     return listePersonnages[math.random(#listePersonnages)]
end
-- Récupère une propriété depuis une infobox avec le nom du champ
local function extraireChampDepuisInfobox(contenu, champ)
    local pattern = "|" .. champ .. "%s-=%s-([^|\n]+)"
    return mw.ustring.match(contenu, pattern)
end
end


Ligne 25 : Ligne 31 :
     end
     end


     -- Détection charte
     -- Extrait entre les balises <!--extrait--> ... <!--/extrait-->
    local charte = mw.ustring.match(contenu, "|%s*Charte%s*=%s*([%w_%-]+)") or "defaut"
 
    -- Détection thème
    local theme = mw.ustring.match(contenu, "|%s*Theme%s*=%s*([%w_%-]+)")
 
    -- Appel module Charte pour couleur
    local couleur = "#aaa"
    local success, moduleCharte = pcall(require, "Module:Charte")
    if success and type(moduleCharte.couleur) == "function" then
        couleur = moduleCharte.couleur{
            args = {
                charte = charte,
                type = "hr"
            }
        }
    end
 
    -- Extraction de l’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 49 : Ligne 37 :
     end
     end


     -- Image de fond si thème trouvé
     -- Récupération du thème (icône) et de la charte (couleur)
     local fondStyle = ""
     local theme = extraireChampDepuisInfobox(contenu, "Theme") or ""
     if theme and theme ~= "" then
     local charte = extraireChampDepuisInfobox(contenu, "Charte") or "defaut"
        local imageURL = "Fichier:" .. theme .. ".png"
 
        fondStyle = "background: url(\"" .. mw.uri.fullUrl(imageURL, "download") .. "\") center/140px no-repeat; background-color: #f8f9fa;"
    -- Couleur personnalisée de la barre <hr>
    else
    local couleur = frame:callParserFunction("#invoke:Charte|couleur", { "charte=" .. charte, "type=hr" })
        fondStyle = "background-color: #f8f9fa;"
    local backgroundIcon = theme ~= "" and "url(/images/" .. theme .. ".svg)" or "none"
    end


     -- Construction HTML
     -- Construction HTML stylisé
     local html = {}
     local html = {}
     table.insert(html, "<div style='max-width:850px; margin:0 auto; border:1px solid #aaa; " .. fondStyle .. " padding:12px; margin:12px 0; border-radius:10px; box-shadow:1px 1px 3px #ccc;'>")
     table.insert(html, "<div style='max-width:900px; border:1px solid #aaa; background:#f8f9fa; padding:12px; margin:12px auto; border-radius:10px; box-shadow:1px 1px 3px #ccc; background-image:" .. backgroundIcon .. "; background-position:top right; background-repeat:no-repeat; background-size:100px;'>")
     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, "<hr style='border:0; border-top:2px solid " .. couleur .. "; margin:0 0 12px 0;'/>")
     table.insert(html, "<hr style='border:none; height:3px; background:" .. couleur .. "; margin:4px 0;'/>")
 
     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 + texte "Portrait"
     -- Image avec étiquette "Portrait"
     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|200px|class=portrait-image]]")
Ligne 74 : Ligne 62 :
     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>")
     table.insert(html, mw.getCurrentFrame():preprocess(extrait))
     table.insert(html, tostring(mw.getCurrentFrame():preprocess(extrait)))
     table.insert(html, "</div>")
     table.insert(html, "</div>")



Version du 12 juillet 2025 à 19:50

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

local p = {}

-- Liste manuelle des pages de personnages
local listePersonnages = {
    "Gore Sinkelk",
}

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

-- Récupère une propriété depuis une infobox avec le nom du champ
local function extraireChampDepuisInfobox(contenu, champ)
    local pattern = "|" .. champ .. "%s-=%s-([^|\n]+)"
    return mw.ustring.match(contenu, pattern)
end

-- Fonction principale
function p.personnageDuJour(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

    -- Extrait entre les balises <!--extrait--> ... <!--/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

    -- Récupération du thème (icône) et de la charte (couleur)
    local theme = extraireChampDepuisInfobox(contenu, "Theme") or ""
    local charte = extraireChampDepuisInfobox(contenu, "Charte") or "defaut"

    -- Couleur personnalisée de la barre <hr>
    local couleur = frame:callParserFunction("#invoke:Charte|couleur", { "charte=" .. charte, "type=hr" })
    local backgroundIcon = theme ~= "" and "url(/images/" .. theme .. ".svg)" or "none"

    -- Construction HTML stylisé
    local html = {}
    table.insert(html, "<div style='max-width:900px; border:1px solid #aaa; background:#f8f9fa; padding:12px; margin:12px auto; border-radius:10px; box-shadow:1px 1px 3px #ccc; background-image:" .. backgroundIcon .. "; background-position:top right; background-repeat:no-repeat; background-size:100px;'>")
    table.insert(html, "<div style='font-size:1.2em; font-weight:bold;'>👤 Personnage du jour</div>")
    table.insert(html, "<hr style='border:none; height:3px; background:" .. couleur .. "; margin:4px 0;'/>")

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

    -- Image avec étiquette "Portrait"
    table.insert(html, "<div style='flex:0 0 200px; position:relative;'>")
    table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px|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;'>Portrait</div>")
    table.insert(html, "</div>")

    -- Nom + 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, tostring(mw.getCurrentFrame():preprocess(extrait)))
    table.insert(html, "</div>")

    table.insert(html, "</div></div>")

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

return p