« Module:Personnagedujour » : différence entre les versions
Apparence
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- | -- Fonction principale : tire une page au hasard depuis la catégorie "personnage" et affiche l’extrait stylisé | ||
function p.personnageDuJour(frame) | |||
local titrePage = p.selectionAleatoireDepuisCategorie("personnage") | |||
if not titrePage then | |||
return "<span style='color:red;'>Erreur : aucune page trouvée dans la catégorie.</span>" | |||
end | |||
return | |||
end | |||
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 | ||
| Ligne 21 : | Ligne 14 : | ||
local contenu = page:getContent() | local contenu = page:getContent() | ||
if not contenu then | if not contenu then | ||
return "<span style='color:red;'>Erreur : contenu inaccessible.</span>" | return "<span style='color:red;'>Erreur : contenu inaccessible.</span>" | ||
end | end | ||
-- Récupération de la charte déclarée dans l’infobox de la page | |||
local charte = mw.ustring.match(contenu, "|%s*Charte%s*=%s*([%w_%-]+)") | |||
if not charte then | |||
charte = "defaut" | |||
end | |||
-- Appel du module Charte pour obtenir la couleur du <hr> | |||
local moduleCharte = require("Module:Charte") | |||
local couleur = moduleCharte.couleur{ | |||
args = { | |||
charte = charte, | |||
type = "hr" | |||
} | |||
} | |||
-- Récupération de l’extrait 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 44 : | Ligne 39 : | ||
end | end | ||
-- | -- Génération du HTML | ||
local html = {} | local html = {} | ||
table.insert(html, "<div style='max-width: | 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;'>") | ||
table.insert(html, "<div style='font-size:1.2em; font-weight:bold | 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:0; border-top:2px solid " .. couleur .. "; margin:0 0 12px 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;'>") | ||
table.insert(html, "<div style='flex:0 0 200px; text-align:center;'>") | |||
table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px|Portrait]]<br/><div style='font-size:90%; font-style:italic;'>[[" .. titrePage .. "]]</div>") | |||
table.insert(html, "<div style='flex:0 0 200px; | |||
table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px| | |||
table.insert(html, "</div>") | table.insert(html, "</div>") | ||
table.insert(html, "<div style='flex:1;'>") | table.insert(html, "<div style='flex:1;'>") | ||
table.insert(html, "<div style=' | table.insert(html, "<div style='font-weight:bold; margin-bottom:6px;'>[[" .. titrePage .. "]]</div>") | ||
table.insert(html, mw.getCurrentFrame():preprocess(extrait)) | table.insert(html, mw.getCurrentFrame():preprocess(extrait)) | ||
table.insert(html, "</div>") | table.insert(html, "</div>") | ||
table.insert(html, "</div>") | |||
table.insert(html, "</div>") | |||
return table.concat(html, "\n") | |||
end | |||
-- Fonction utilitaire : tire un titre de page au hasard depuis une catégorie donnée | |||
function p.selectionAleatoireDepuisCategorie(nomCategorie) | |||
local catTitle = mw.title.makeTitle("Category", nomCategorie) | |||
if not catTitle or not catTitle.exists then | |||
return nil | |||
end | |||
table.insert( | local pages = {} | ||
for titre, _ in pairs(catTitle:getMembers()) do | |||
table.insert(pages, titre.text) | |||
end | |||
return | if #pages == 0 then | ||
return nil | |||
end | |||
local index = math.random(1, #pages) | |||
return pages[index] | |||
end | end | ||
return p | return p | ||
Version du 12 juillet 2025 à 19:41
La documentation pour ce module peut être créée à Module:Personnagedujour/doc
local p = {}
-- Fonction principale : tire une page au hasard depuis la catégorie "personnage" et affiche l’extrait stylisé
function p.personnageDuJour(frame)
local titrePage = p.selectionAleatoireDepuisCategorie("personnage")
if not titrePage then
return "<span style='color:red;'>Erreur : aucune page trouvée dans la catégorie.</span>"
end
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
-- Récupération de la charte déclarée dans l’infobox de la page
local charte = mw.ustring.match(contenu, "|%s*Charte%s*=%s*([%w_%-]+)")
if not charte then
charte = "defaut"
end
-- Appel du module Charte pour obtenir la couleur du <hr>
local moduleCharte = require("Module:Charte")
local couleur = moduleCharte.couleur{
args = {
charte = charte,
type = "hr"
}
}
-- Récupération de l’extrait 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
-- Génération du HTML
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;'>")
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, "<div style='display:flex; align-items:flex-start; gap:12px;'>")
table.insert(html, "<div style='flex:0 0 200px; text-align:center;'>")
table.insert(html, "[[Fichier:" .. titrePage .. ".png|200px|Portrait]]<br/><div style='font-size:90%; font-style:italic;'>[[" .. titrePage .. "]]</div>")
table.insert(html, "</div>")
table.insert(html, "<div style='flex:1;'>")
table.insert(html, "<div style='font-weight:bold; margin-bottom:6px;'>[[" .. titrePage .. "]]</div>")
table.insert(html, mw.getCurrentFrame():preprocess(extrait))
table.insert(html, "</div>")
table.insert(html, "</div>")
table.insert(html, "</div>")
return table.concat(html, "\n")
end
-- Fonction utilitaire : tire un titre de page au hasard depuis une catégorie donnée
function p.selectionAleatoireDepuisCategorie(nomCategorie)
local catTitle = mw.title.makeTitle("Category", nomCategorie)
if not catTitle or not catTitle.exists then
return nil
end
local pages = {}
for titre, _ in pairs(catTitle:getMembers()) do
table.insert(pages, titre.text)
end
if #pages == 0 then
return nil
end
local index = math.random(1, #pages)
return pages[index]
end
return p