« Module:Liste » : différence entre les versions
De Nefald
Autres actions
Page créée avec « local p = {} function p.main(frame) local args = frame:getParent().args -- Paramètres local categorie = args.categorie or args[1] or '' local colonnes = tonumber(args.colonnes) or 3 local ordre = args.ordre or 'alpha' -- alpha, récent, taille local format = args.format or 'liste' -- liste, tableau, cartes local afficherDescription = args.description or 'oui' local afficherImage = args.image or 'non' if categorie ==... » |
mAucun résumé des modifications |
||
Ligne 22 : | Ligne 22 : | ||
end | end | ||
-- Récupérer les pages de la catégorie | -- Récupérer les pages de la catégorie directement | ||
local | local pages = {} | ||
-- Méthode 1 : Via mw.site.stats (simple mais limité) | |||
for _, page in ipairs(catTitle:getContent() and {} or {}) do | |||
-- Cette méthode ne fonctionne pas directement | |||
end | |||
-- Méthode 2 : Construire la requête DPL comme string | |||
local dplQuery = '<dpl>\n' | |||
dplQuery = dplQuery .. 'category=' .. categorie .. '\n' | |||
dplQuery = dplQuery .. 'namespace=\n' | |||
if ordre == 'récent' then | if ordre == 'récent' then | ||
dplQuery = dplQuery .. 'ordermethod=lastedit\n' | |||
dplQuery = dplQuery .. 'order=descending\n' | |||
elseif ordre == 'taille' then | elseif ordre == 'taille' then | ||
dplQuery = dplQuery .. 'ordermethod=size\n' | |||
dplQuery = dplQuery .. 'order=descending\n' | |||
else | |||
dplQuery = dplQuery .. 'ordermethod=title\n' | |||
end | end | ||
dplQuery = dplQuery .. 'mode=userformat\n' | |||
dplQuery = dplQuery .. 'listseparators=@@START@@,@@PAGE@@,@@END@@\n' | |||
dplQuery = dplQuery .. 'format=,%PAGE%,\n' | |||
dplQuery = dplQuery .. '</dpl>' | |||
-- Parser le | -- Parser le DPL | ||
local pages | local dplResult = frame:preprocess(dplQuery) | ||
-- Extraire les pages | |||
for page in dplResult:gmatch('@@PAGE@@([^@]+)') do | for page in dplResult:gmatch('@@PAGE@@([^@]+)') do | ||
page = mw.text.trim(page) | |||
if page ~= '' then | if page ~= '' then | ||
table.insert(pages, | table.insert(pages, page) | ||
end | end | ||
end | end | ||
Ligne 76 : | Ligne 85 : | ||
if afficherDescription == 'oui' then | if afficherDescription == 'oui' then | ||
local desc = frame: | local desc = frame:expandTemplate{ | ||
if desc and desc ~= '' and desc | title = 'Invoke:PageInfo', | ||
args = {'getDescription', page} | |||
} | |||
if desc and desc ~= '' and not desc:match("Aucune description") then | |||
table.insert(result, "<br/><small>" .. desc .. "</small>") | table.insert(result, "<br/><small>" .. desc .. "</small>") | ||
end | end | ||
Ligne 109 : | Ligne 121 : | ||
if afficherImage == 'oui' then | if afficherImage == 'oui' then | ||
local image = frame: | local image = frame:expandTemplate{ | ||
if image and image ~= '' and image | title = 'Invoke:PageInfo', | ||
args = {'getMainImage', page} | |||
} | |||
if image and image ~= '' and not image:match("Aucune image") then | |||
table.insert(cells, '| [[Fichier:' .. image .. '|100px]]') | table.insert(cells, '| [[Fichier:' .. image .. '|100px]]') | ||
else | else | ||
Ligne 118 : | Ligne 133 : | ||
if afficherDescription == 'oui' then | if afficherDescription == 'oui' then | ||
local desc = frame: | local desc = frame:expandTemplate{ | ||
if desc and desc ~= '' and desc | title = 'Invoke:PageInfo', | ||
args = {'getDescription', page} | |||
} | |||
if desc and desc ~= '' and not desc:match("Aucune description") then | |||
table.insert(cells, '| ' .. desc) | table.insert(cells, '| ' .. desc) | ||
else | else | ||
Ligne 137 : | Ligne 155 : | ||
function p.formatCartes(frame, pages, colonnes, afficherDescription, afficherImage) | function p.formatCartes(frame, pages, colonnes, afficherDescription, afficherImage) | ||
local result = {} | local result = {} | ||
local pageInfo = require('Module:PageInfo') | |||
-- CSS inline pour les cartes | -- CSS inline pour les cartes | ||
Ligne 142 : | Ligne 161 : | ||
for _, page in ipairs(pages) do | for _, page in ipairs(pages) do | ||
table.insert(result, '<div style="border: 1px solid #ddd; border-radius: 8px; padding: 1em; background: #f9f9f9 | table.insert(result, '<div style="border: 1px solid #ddd; border-radius: 8px; padding: 1em; background: #f9f9f9;">') | ||
-- Image si demandée | -- Image si demandée | ||
if afficherImage == 'oui' then | if afficherImage == 'oui' then | ||
local | local imageFrame = frame:newChild{ | ||
if image and image ~= '' and image | title = 'temp', | ||
args = {page} | |||
} | |||
local image = pageInfo.getMainImage(imageFrame) | |||
if image and image ~= '' and not image:match("Aucune image") then | |||
table.insert(result, '<div style="text-align: center; margin-bottom: 0.5em;">[[Fichier:' .. image .. '|150px]]</div>') | table.insert(result, '<div style="text-align: center; margin-bottom: 0.5em;">[[Fichier:' .. image .. '|150px]]</div>') | ||
end | end | ||
Ligne 157 : | Ligne 180 : | ||
-- Description si demandée | -- Description si demandée | ||
if afficherDescription == 'oui' then | if afficherDescription == 'oui' then | ||
local | local descFrame = frame:newChild{ | ||
if desc and desc ~= '' and desc | title = 'temp', | ||
args = {page} | |||
} | |||
local desc = pageInfo.getDescription(descFrame) | |||
if desc and desc ~= '' and not desc:match("Aucune description") then | |||
table.insert(result, '<div style="color: #666; font-size: 0.9em;">' .. desc .. '</div>') | table.insert(result, '<div style="color: #666; font-size: 0.9em;">' .. desc .. '</div>') | ||
end | end |
Dernière version du 4 octobre 2025 à 08:52
La documentation pour ce module peut être créée à Module:Liste/doc
local p = {}
function p.main(frame)
local args = frame:getParent().args
-- Paramètres
local categorie = args.categorie or args[1] or ''
local colonnes = tonumber(args.colonnes) or 3
local ordre = args.ordre or 'alpha' -- alpha, récent, taille
local format = args.format or 'liste' -- liste, tableau, cartes
local afficherDescription = args.description or 'oui'
local afficherImage = args.image or 'non'
if categorie == '' then
return '<span class="error">Erreur : Aucune catégorie spécifiée</span>'
end
-- Vérifier que la catégorie existe
local catTitle = mw.title.new(categorie, 'Catégorie')
if not catTitle or not catTitle.exists then
return '<span class="error">Erreur : La catégorie "' .. categorie .. '" n\'existe pas</span>'
end
-- Récupérer les pages de la catégorie directement
local pages = {}
-- Méthode 1 : Via mw.site.stats (simple mais limité)
for _, page in ipairs(catTitle:getContent() and {} or {}) do
-- Cette méthode ne fonctionne pas directement
end
-- Méthode 2 : Construire la requête DPL comme string
local dplQuery = '<dpl>\n'
dplQuery = dplQuery .. 'category=' .. categorie .. '\n'
dplQuery = dplQuery .. 'namespace=\n'
if ordre == 'récent' then
dplQuery = dplQuery .. 'ordermethod=lastedit\n'
dplQuery = dplQuery .. 'order=descending\n'
elseif ordre == 'taille' then
dplQuery = dplQuery .. 'ordermethod=size\n'
dplQuery = dplQuery .. 'order=descending\n'
else
dplQuery = dplQuery .. 'ordermethod=title\n'
end
dplQuery = dplQuery .. 'mode=userformat\n'
dplQuery = dplQuery .. 'listseparators=@@START@@,@@PAGE@@,@@END@@\n'
dplQuery = dplQuery .. 'format=,%PAGE%,\n'
dplQuery = dplQuery .. '</dpl>'
-- Parser le DPL
local dplResult = frame:preprocess(dplQuery)
-- Extraire les pages
for page in dplResult:gmatch('@@PAGE@@([^@]+)') do
page = mw.text.trim(page)
if page ~= '' then
table.insert(pages, page)
end
end
if #pages == 0 then
return '<div class="notice">Aucune page dans la catégorie "' .. categorie .. '"</div>'
end
-- Générer l'affichage selon le format
if format == 'tableau' then
return p.formatTableau(frame, pages, afficherDescription, afficherImage)
elseif format == 'cartes' then
return p.formatCartes(frame, pages, colonnes, afficherDescription, afficherImage)
else
return p.formatListe(frame, pages, colonnes, afficherDescription)
end
end
-- Format : Liste simple en colonnes
function p.formatListe(frame, pages, colonnes, afficherDescription)
local result = {}
table.insert(result, '<div style="column-count:' .. colonnes .. '; column-gap: 2em;">')
for _, page in ipairs(pages) do
table.insert(result, '<div style="break-inside: avoid; margin-bottom: 1em;">')
table.insert(result, "* '''[[" .. page .. "]]'''")
if afficherDescription == 'oui' then
local desc = frame:expandTemplate{
title = 'Invoke:PageInfo',
args = {'getDescription', page}
}
if desc and desc ~= '' and not desc:match("Aucune description") then
table.insert(result, "<br/><small>" .. desc .. "</small>")
end
end
table.insert(result, '</div>')
end
table.insert(result, '</div>')
return table.concat(result, '\n')
end
-- Format : Tableau
function p.formatTableau(frame, pages, afficherDescription, afficherImage)
local result = {}
table.insert(result, '{| class="wikitable sortable"')
-- En-têtes
local headers = {'! Nom'}
if afficherImage == 'oui' then
table.insert(headers, '! Image')
end
if afficherDescription == 'oui' then
table.insert(headers, '! Description')
end
table.insert(result, table.concat(headers, ' || '))
-- Lignes
for _, page in ipairs(pages) do
local cells = {'| [[' .. page .. ']]'}
if afficherImage == 'oui' then
local image = frame:expandTemplate{
title = 'Invoke:PageInfo',
args = {'getMainImage', page}
}
if image and image ~= '' and not image:match("Aucune image") then
table.insert(cells, '| [[Fichier:' .. image .. '|100px]]')
else
table.insert(cells, '| —')
end
end
if afficherDescription == 'oui' then
local desc = frame:expandTemplate{
title = 'Invoke:PageInfo',
args = {'getDescription', page}
}
if desc and desc ~= '' and not desc:match("Aucune description") then
table.insert(cells, '| ' .. desc)
else
table.insert(cells, '| —')
end
end
table.insert(result, '|-')
table.insert(result, table.concat(cells, ' || '))
end
table.insert(result, '|}')
return table.concat(result, '\n')
end
-- Format : Cartes (style moderne)
function p.formatCartes(frame, pages, colonnes, afficherDescription, afficherImage)
local result = {}
local pageInfo = require('Module:PageInfo')
-- CSS inline pour les cartes
table.insert(result, '<div style="display: grid; grid-template-columns: repeat(' .. colonnes .. ', 1fr); gap: 1.5em; margin: 1em 0;">')
for _, page in ipairs(pages) do
table.insert(result, '<div style="border: 1px solid #ddd; border-radius: 8px; padding: 1em; background: #f9f9f9;">')
-- Image si demandée
if afficherImage == 'oui' then
local imageFrame = frame:newChild{
title = 'temp',
args = {page}
}
local image = pageInfo.getMainImage(imageFrame)
if image and image ~= '' and not image:match("Aucune image") then
table.insert(result, '<div style="text-align: center; margin-bottom: 0.5em;">[[Fichier:' .. image .. '|150px]]</div>')
end
end
-- Titre
table.insert(result, '<div style="font-size: 1.2em; font-weight: bold; margin-bottom: 0.5em;">[[' .. page .. ']]</div>')
-- Description si demandée
if afficherDescription == 'oui' then
local descFrame = frame:newChild{
title = 'temp',
args = {page}
}
local desc = pageInfo.getDescription(descFrame)
if desc and desc ~= '' and not desc:match("Aucune description") then
table.insert(result, '<div style="color: #666; font-size: 0.9em;">' .. desc .. '</div>')
end
end
table.insert(result, '</div>')
end
table.insert(result, '</div>')
return table.concat(result, '\n')
end
return p