« Module:Personnagedujour » : différence entre les versions
Apparence
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| (35 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- Fonction | -- Fonction pour charger la liste depuis la page Liste/Personnages | ||
function | local function getListeDepuisPage() | ||
local | local titreListe = mw.title.new("Liste/Personnages") | ||
if not | if not titreListe or not titreListe.exists then | ||
return "<span style='color:red;'>Erreur : | return nil, "<span style='color:red;'>Erreur : la page de liste n'existe pas.</span>" | ||
end | end | ||
local | local contenu = titreListe:getContent() | ||
if not | if not contenu then | ||
return "<span style='color:red;'>Erreur : la | return nil, "<span style='color:red;'>Erreur : contenu de la liste inaccessible.</span>" | ||
end | end | ||
local contenu = | 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 | end | ||
if #liste == 0 then | |||
return nil, "<span style='color:gray;'>Aucun personnage trouvé dans la liste.</span>" | |||
end | end | ||
-- | return liste | ||
local | end | ||
local | |||
-- 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*%-%->") | local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->") | ||
if not extrait then | if not extrait then | ||
extrait = "<span style='color:gray;'>Aucun extrait disponible.</span>" | |||
end | end | ||
local charteModule = require("Module:Charte") | |||
local couleurHR = charteModule.couleur{ args = { charte = theme, type = "hr" } } | |||
local html = {} | local html = {} | ||
table.insert(html, "<div style=' | 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='font-size:1.2em; font-weight:bold;'> | |||
table.insert(html, "<hr style='border: | 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;'>") | table.insert(html, "<div style='display:flex; align-items:flex-start; gap:12px;'>") | ||
table.insert(html, "<div style='flex:0 0 | |||
table.insert(html, "[[Fichier:" .. titrePage .. ".png| | -- 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>") | table.insert(html, "</div>") | ||
-- Bloc texte | |||
table.insert(html, "<div style='flex:1;'>") | table.insert(html, "<div style='flex:1;'>") | ||
table.insert(html, "<div style='font-weight:bold; margin-bottom: | 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)) | table.insert(html, mw.getCurrentFrame():preprocess(extrait)) | ||
table.insert(html, "< | if pageExiste then | ||
table.insert(html, "<div style='text-align:right; margin-top:10px; font-size:90%;'>[[" .. titrePage .. "|→ Vers l’article]]</div>") | |||
table.insert(html, "</div>") | end | ||
table.insert(html, "</div></div></div></div>") | |||
return table.concat(html, "\n") | return table.concat(html, "\n") | ||
end | end | ||
return p | return p | ||
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