« Module:Paysdujour » : différence entre les versions
Apparence
Aucun résumé des modifications Balise : Révoqué |
Aucun résumé des modifications Balise : Révocation manuelle |
||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
-- | -- Fonction pour lire la liste depuis Liste/Pays | ||
local function lireListeDepuisPage() | local function lireListeDepuisPage() | ||
local titreListe = mw.title.new("Liste/Pays") | local titreListe = mw.title.new("Liste/Pays") | ||
if not titreListe or not titreListe.exists then return {} end | if not titreListe or not titreListe.exists then | ||
return {} | |||
end | |||
local contenu = titreListe:getContent() | local contenu = titreListe:getContent() | ||
if not contenu then return {} end | if not contenu then | ||
return {} | |||
end | |||
local resultats = {} | local resultats = {} | ||
for ligne in mw.text.gsplit(contenu, "\n", true) do | for ligne in mw.text.gsplit(contenu, "\n", true) do | ||
ligne = mw.text.trim(ligne) | |||
if | if ligne ~= "" then | ||
table.insert(resultats, | table.insert(resultats, ligne) | ||
end | end | ||
end | end | ||
| Ligne 31 : | Ligne 23 : | ||
end | end | ||
-- Tirage | -- Tirage au hasard | ||
local function pageAleatoire() | local function pageAleatoire() | ||
math.randomseed(os.time()) | math.randomseed(os.time()) | ||
| Ligne 46 : | Ligne 38 : | ||
end | end | ||
local page = mw.title.new(titrePage) | local page = mw.title.new(titrePage) | ||
local contenu = page and page.exists and page:getContent() or nil | local contenu = page and page.exists and page:getContent() or nil | ||
local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut" | local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut" | ||
local extrait = contenu and mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->") or nil | |||
local texteExtrait = extrait and mw.getCurrentFrame():preprocess(extrait) | local texteExtrait = extrait and mw.getCurrentFrame():preprocess(extrait) | ||
or "<span style='color:gray;'>Aucun extrait disponible pour ce pays.</span>" | or "<span style='color:gray;'>Aucun extrait disponible pour ce pays.</span>" | ||
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" } } | ||
local html = {} | local html = {} | ||
table.insert(html, "<div class='" .. theme .. "-picto' style='flex: 1 1 54%; 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 class='" .. theme .. "-picto' style='flex: 1 1 54%; 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;'>") | ||
| Ligne 74 : | Ligne 58 : | ||
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;'>[[Fichier:Bannière " .. titrePage .. ".png|150px|class=portrait-image]]</div>") | |||
table.insert(html, "<div style='flex:0 0 200px;'>" .. | |||
table.insert(html, "<div style='flex:1;'>") | table.insert(html, "<div style='flex:1;'>") | ||
Version du 15 juillet 2025 à 15:48
La documentation pour ce module peut être créée à Module:Paysdujour/doc
local p = {}
-- Fonction pour lire la liste depuis Liste/Pays
local function lireListeDepuisPage()
local titreListe = mw.title.new("Liste/Pays")
if not titreListe or not titreListe.exists then
return {}
end
local contenu = titreListe:getContent()
if not contenu then
return {}
end
local resultats = {}
for ligne in mw.text.gsplit(contenu, "\n", true) do
ligne = mw.text.trim(ligne)
if ligne ~= "" then
table.insert(resultats, ligne)
end
end
return resultats
end
-- Tirage au hasard
local function pageAleatoire()
math.randomseed(os.time())
local liste = lireListeDepuisPage()
if #liste == 0 then return nil end
return liste[math.random(#liste)]
end
-- Fonction principale
function p.paysDuJour(frame)
local titrePage = pageAleatoire()
if not titrePage then
titrePage = "Page inconnue"
end
local page = mw.title.new(titrePage)
local contenu = page and page.exists and page:getContent() or nil
local theme = contenu and mw.ustring.match(contenu, "|%s*Theme%s*=%s*(%a+)") or "defaut"
local extrait = contenu and mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->") or nil
local texteExtrait = extrait and mw.getCurrentFrame():preprocess(extrait)
or "<span style='color:gray;'>Aucun extrait disponible pour ce pays.</span>"
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 54%; 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;'>Pays 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='flex:0 0 200px;'>[[Fichier:Bannière " .. titrePage .. ".png|150px|class=portrait-image]]</div>")
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, texteExtrait)
table.insert(html, "<div style='text-align:right; font-size:90%; margin-top:8px;'>[[" .. titrePage .. "|→ Vers l’article]]</div>")
table.insert(html, "</div></div></div></div>")
return table.concat(html, "\n")
end
return p