« Module:PageInfo » : différence entre les versions
De Nefald
Autres actions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 13 : | Ligne 13 : | ||
end | end | ||
local content = pageObj:getContent() | local content = pageObj:getContent() | ||
if not content then | if not content then | ||
| Ligne 19 : | Ligne 18 : | ||
end | end | ||
-- 1. Chercher un modèle {{Description|...}} ou {{Short description|...}} | |||
local desc = content:match('{{[Ss]hort description%s*|%s*([^}]+)}}') | |||
-- 1. Chercher | |||
local desc = content:match('{{[ | |||
if desc then | if desc then | ||
return mw.text.trim(desc) | return mw.text.trim(desc) | ||
end | end | ||
desc = content:match('{{[Dd]escription%s*|%s*([^}]+)}}') | |||
desc = content:match(' | |||
if desc then | if desc then | ||
return mw.text.trim(desc) | return mw.text.trim(desc) | ||
end | end | ||
-- | -- 2. Chercher dans un champ description d'infobox | ||
desc = content:match(' | desc = content:match('|%s*description%s*=%s*([^\n|]+)') | ||
if desc then | if desc then | ||
return mw.text.trim(desc) | return mw.text.trim(desc) | ||
end | end | ||
-- | -- 3. Extraire le premier paragraphe significatif | ||
-- | -- Nettoyer le contenu | ||
local cleaned = content | |||
cleaned = cleaned:gsub('%[%[ | |||
cleaned = cleaned:gsub('%[%[ | -- Supprimer les infobox complètes | ||
cleaned = cleaned:gsub('<[^>]+>', '') | cleaned = cleaned:gsub('{{[Ii]nfobox.-}}', '', 1) | ||
cleaned = cleaned:gsub(' | |||
-- Supprimer tous les autres modèles | |||
cleaned = cleaned:gsub('{{[^}]+}}', '') | |||
-- Supprimer les catégories | |||
cleaned = cleaned:gsub('%[%[[Cc]ategory:[^%]]+%]%]', '') | |||
cleaned = cleaned:gsub('%[%[[Cc]atégorie:[^%]]+%]%]', '') | |||
-- Supprimer les balises HTML | |||
cleaned = cleaned:gsub('<[^>]+>', '') | |||
-- Supprimer les titres de section (=== ... ===) | |||
cleaned = cleaned:gsub('\n%s*==[^=].-==%s*\n', '\n') | |||
-- Supprimer les lignes vides multiples | |||
cleaned = cleaned:gsub('\n\n+', '\n') | |||
-- Extraire le premier paragraphe significatif | -- Extraire le premier paragraphe significatif (au moins 20 caractères) | ||
for line in cleaned:gmatch('[^\n]+') do | for line in cleaned:gmatch('[^\n]+') do | ||
line = mw.text.trim(line) | line = mw.text.trim(line) | ||
if line ~= '' and not line:match('^[*#:]') and not line:match('^%s*{|') then | |||
-- Limiter la longueur | -- Ignorer les lignes vides, listes, tableaux | ||
if #line > | if line ~= '' | ||
line = line:sub(1, | and not line:match('^[*#:;]') | ||
and not line:match('^%s*{|') | |||
and not line:match('^%s*|}') | |||
and not line:match('^%s*|') | |||
and #line > 20 then | |||
-- Limiter la longueur à 200 caractères | |||
if #line > 200 then | |||
line = line:sub(1, 197) .. '...' | |||
end | end | ||
return line | return line | ||
end | end | ||
| Ligne 79 : | Ligne 99 : | ||
end | end | ||
-- Chercher | -- Chercher un lien de documentation dans l'infobox | ||
local doc = content:match('|%s*documentation%s*=%s*(%[?https?://[^%s%]|]+)') | |||
if doc then | |||
local | doc = doc:gsub('%[', ''):gsub('%]', '') | ||
return '[' .. doc .. ' Documentation]' | |||
return '[' .. | |||
end | end | ||
-- | -- Chercher un lien dans un champ "lien" ou "site" | ||
doc = content:match('|%s*[Ll]ien%s*=%s*(%[?https?://[^%s%]|]+)') | |||
if not doc then | |||
doc = content:match('|%s*[Ss]ite%s*=%s*(%[?https?://[^%s%]|]+)') | |||
end | end | ||
if doc then | |||
doc = doc:gsub('%[', ''):gsub('%]', '') | |||
if | return '[' .. doc .. ' Site officiel]' | ||
return ' | |||
end | end | ||
| Ligne 122 : | Ligne 120 : | ||
end | end | ||
-- Fonction | -- Fonction de debug complète | ||
function p.getInfo(frame) | function p.getInfo(frame) | ||
local pageName = frame.args[1] | local pageName = frame.args[1] | ||
if not pageName or pageName == '' then | if not pageName or pageName == '' then | ||
return 'Erreur : | return "'''Erreur :''' Aucun nom de page fourni" | ||
end | end | ||
local pageObj = mw.title.new(pageName) | local pageObj = mw.title.new(pageName) | ||
if not pageObj then | if not pageObj then | ||
return 'Erreur : | return "'''Erreur :''' Titre invalide" | ||
end | end | ||
local output = "'''Informations sur la page : " .. pageName .. "'''\n\n" | |||
output = output .. "* '''Existe :''' " .. (pageObj.exists and "Oui" or "Non") .. "\n" | |||
if not pageObj.exists then | if not pageObj.exists then | ||
return | return output | ||
end | end | ||
output = output .. "* '''Namespace :''' " .. pageObj.namespace .. "\n" | |||
output = output .. "* '''Titre complet :''' " .. pageObj.fullText .. "\n" | |||
local content = pageObj:getContent() | local content = pageObj:getContent() | ||
if content then | if content then | ||
output = output .. "* '''Longueur du contenu :''' " .. #content .. " caractères\n" | |||
output = output .. "* '''Premiers 200 caractères :'''\n<pre>" .. content:sub(1, 200) .. "...</pre>\n" | |||
local desc = p.getDescription(frame) | |||
output = output .. "* '''Description extraite :''' " .. desc .. "\n" | |||
local doc = p.getDocumentation(frame) | |||
output = output .. "* '''Documentation extraite :''' " .. doc .. "\n" | |||
end | end | ||
return output | |||
return | |||
end | end | ||
-- Fonction pour obtenir des statistiques | -- Fonction pour obtenir des statistiques | ||
function p.getStats(frame) | function p.getStats(frame) | ||
local pageName = frame.args[1] | local pageName = frame.args[1] | ||
| Ligne 173 : | Ligne 175 : | ||
end | end | ||
local | local chars = #content | ||
local lines = select(2, content:gsub('\n', '\n')) + 1 | |||
local words = select(2, content:gsub('%S+', '')) | |||
return string.format( | return string.format("%d caractères, %d lignes, ~%d mots", chars, lines, words) | ||
end | end | ||
-- Fonction pour extraire l'auteur | -- Fonction pour extraire l'auteur | ||
function p.getAuthor(frame) | function p.getAuthor(frame) | ||
local pageName = frame.args[1] | local pageName = frame.args[1] | ||
| Ligne 199 : | Ligne 199 : | ||
end | end | ||
local author = content:match('|%s*[Aa]uteur%s*=%s*([^\n|]+)') | local author = content:match('|%s*[Aa]uteur%s*=%s*([^\n|]+)') | ||
if author then | if author then | ||
| Ligne 206 : | Ligne 205 : | ||
author = content:match('|%s*[Aa]uthor%s*=%s*([^\n|]+)') | author = content:match('|%s*[Aa]uthor%s*=%s*([^\n|]+)') | ||
if author then | if author then | ||
return mw.text.trim(author) | return mw.text.trim(author) | ||
| Ligne 235 : | Ligne 229 : | ||
end | end | ||
local version = content:match('|%s*[Vv]ersion%s*=%s*([^\n|]+)') | local version = content:match('|%s*[Vv]ersion%s*=%s*([^\n|]+)') | ||
if version then | if version then | ||
| Ligne 241 : | Ligne 234 : | ||
end | end | ||
version = content:match('[Vv]ersion%s*:?%s*(%d+%.%d+[%.%d]*)') | version = content:match('[Vv]ersion%s*:?%s*(%d+%.%d+[%.%d]*)') | ||
if version then | if version then | ||
| Ligne 277 : | Ligne 264 : | ||
end | end | ||
local date = content:match('|%s*[Mm]ise%s*à%s*jour%s*=%s*([^\n|]+)') | local date = content:match('|%s*[Mm]ise%s*à%s*jour%s*=%s*([^\n|]+)') | ||
if date then | if date then | ||