Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« Module:PageInfo » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
Aucun résumé des modifications
Hiob (discussion | contributions)
Aucun résumé des modifications
Ligne 13 : Ligne 13 :
     end
     end
      
      
    -- Extraire le contenu de la page
     local content = pageObj:getContent()
     local content = pageObj:getContent()
     if not content then
     if not content then
Ligne 19 : Ligne 18 :
     end
     end
      
      
    -- Chercher une description dans différents formats
     -- 1. Chercher un modèle {{Description|...}} ou {{Short description|...}}
   
     local desc = content:match('{{[Ss]hort description%s*|%s*([^}]+)}}')
     -- 1. Chercher dans un modèle {{Description|...}}
     local desc = content:match('{{[Dd]escription%s*|%s*([^}]+)}}')
     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('{{[Dd]escription%s*|%s*([^}]+)}}')
     desc = content:match('|%s*[Dd]escription%s*=%s*([^\n|]+)')
     if desc then
     if desc then
         return mw.text.trim(desc)
         return mw.text.trim(desc)
     end
     end
      
      
     -- 3. Chercher dans un modèle {{Info|description=...}}
     -- 2. Chercher dans un champ description d'infobox
     desc = content:match('{{[Ii]nfo[^}]*|%s*[Dd]escription%s*=%s*([^|}\n]+)')
     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
      
      
     -- 4. Extraire le premier paragraphe (hors modèles)
     -- 3. Extraire le premier paragraphe significatif
     -- Nettoyer les modèles et les balises
    -- Nettoyer le contenu
     local cleaned = content:gsub('{{[^}]+}}', '') -- Enlever les modèles
    local cleaned = content
     cleaned = cleaned:gsub('%[%[Category:[^%]]+%]%]', '') -- Enlever les catégories
   
     cleaned = cleaned:gsub('%[%[Catégorie:[^%]]+%]%]', '')
    -- Supprimer les infobox complètes
     cleaned = cleaned:gsub('<[^>]+>', '') -- Enlever les balises HTML
    cleaned = cleaned:gsub('{{[Ii]nfobox.-}}', '', 1)
     cleaned = cleaned:gsub('^%s*==+[^=]+==[^\n]*\n', '') -- Enlever les titres
   
     -- 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 > 150 then
         if line ~= ''  
                 line = line:sub(1, 147) .. '...'
          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 différents formats de liens de documentation
     -- Chercher un lien de documentation dans l'infobox
   
     local doc = content:match('|%s*documentation%s*=%s*(%[?https?://[^%s%]|]+)')
    -- 1. Lien externe direct [http://... Documentation]
    if doc then
     local docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[Dd]ocumentation%s*%]')
        doc = doc:gsub('%[', ''):gsub('%]', '')
    if docLink then
         return '[' .. doc .. ' Documentation]'
         return '[' .. docLink .. ' Documentation]'
     end
     end
      
      
     -- 2. Lien externe avec texte alternatif
     -- Chercher un lien dans un champ "lien" ou "site"
     docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[^%]]*[Dd]oc[^%]]*%]')
     doc = content:match('|%s*[Ll]ien%s*=%s*(%[?https?://[^%s%]|]+)')
    if docLink then
    if not doc then
        return '[' .. docLink .. ' Documentation]'
        doc = content:match('|%s*[Ss]ite%s*=%s*(%[?https?://[^%s%]|]+)')
     end
     end
      
      
    -- 3. Modèle {{Documentation|url=...}}
     if doc then
    docLink = content:match('{{[Dd]ocumentation%s*|%s*[Uu][Rr][Ll]%s*=%s*([^}|]+)')
         doc = doc:gsub('%[', ''):gsub('%]', '')
     if docLink then
         return '[' .. doc .. ' Site officiel]'
         docLink = mw.text.trim(docLink)
        return '[' .. docLink .. ' Documentation]'
    end
   
    -- 4. Champ documentation dans une infobox
    docLink = content:match('|%s*[Dd]ocumentation%s*=%s*%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
   
    -- 5. Chercher n'importe quel lien externe (dernier recours)
    docLink = content:match('%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Lien externe]'
    end
   
    -- 6. Vérifier si une sous-page /Documentation existe
    local docPage = pageName .. '/Documentation'
    local docPageObj = mw.title.new(docPage)
    if docPageObj and docPageObj.exists then
         return '[[' .. docPage .. '|Documentation]]'
     end
     end
      
      
Ligne 122 : Ligne 120 :
end
end


-- Fonction pour obtenir toutes les infos d'une page (debug)
-- 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 : aucun nom de page fourni'
         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 : titre invalide'
         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 'Erreur : la page n\'existe pas'
         return output
     end
     end
      
      
     local result = {}
     output = output .. "* '''Namespace :''' " .. pageObj.namespace .. "\n"
    table.insert(result, "'''Informations sur la page : " .. pageName .. "'''")
     output = output .. "* '''Titre complet :''' " .. pageObj.fullText .. "\n"
    table.insert(result, "\n* '''Existe :''' Oui")
     table.insert(result, "\n* '''Namespace :''' " .. pageObj.namespace)
    table.insert(result, "\n* '''Titre complet :''' " .. pageObj.fullText)
      
      
     local content = pageObj:getContent()
     local content = pageObj:getContent()
     if content then
     if content then
         table.insert(result, "\n* '''Longueur du contenu :''' " .. #content .. " caractères")
         output = output .. "* '''Longueur du contenu :''' " .. #content .. " caractères\n"
         table.insert(result, "\n* '''Premiers 200 caractères :'''\n<pre>" .. content:sub(1, 200) .. "...</pre>")
         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
      
      
    table.insert(result, "\n* '''Description extraite :''' " .. p.getDescription(frame))
     return output
    table.insert(result, "\n* '''Documentation extraite :''' " .. p.getDocumentation(frame))
   
     return table.concat(result)
end
end


-- Fonction pour obtenir des statistiques sur une page
-- 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 stats = {}
     local chars = #content
    stats.longueur = #content
     local lines = select(2, content:gsub('\n', '\n')) + 1
     stats.lignes = select(2, content:gsub('\n', '\n')) + 1
     local words = select(2, content:gsub('%S+', ''))
     stats.mots = select(2, content:gsub('%S+', ''))
      
      
     return string.format('%d caractères, %d lignes, ~%d mots',  
     return string.format("%d caractères, %d lignes, ~%d mots", chars, lines, words)
        stats.longueur, stats.lignes, stats.mots)
end
end


-- Fonction pour extraire l'auteur/créateur
-- 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
      
      
    -- Chercher un champ auteur
     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
        return mw.text.trim(author)
    end
   
    author = content:match('{{[Aa]uteur%s*|%s*([^}]+)}}')
     if author then
     if author then
         return mw.text.trim(author)
         return mw.text.trim(author)
Ligne 235 : Ligne 229 :
     end
     end
      
      
    -- Chercher un champ version
     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*([^}]+)}}')
    if version then
        return mw.text.trim(version)
    end
   
    -- Chercher un pattern v1.2.3 ou version 1.2.3
     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
      
      
    -- Chercher une date de mise à jour
     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

Version du 4 octobre 2025 à 08:33

La documentation pour ce module peut être créée à Module:PageInfo/doc

local p = {}

-- Fonction pour extraire la description d'une page
function p.getDescription(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- 1. Chercher un modèle {{Description|...}} ou {{Short description|...}}
    local desc = content:match('{{[Ss]hort description%s*|%s*([^}]+)}}')
    if desc then
        return mw.text.trim(desc)
    end
    
    desc = content:match('{{[Dd]escription%s*|%s*([^}]+)}}')
    if desc then
        return mw.text.trim(desc)
    end
    
    -- 2. Chercher dans un champ description d'infobox
    desc = content:match('|%s*description%s*=%s*([^\n|]+)')
    if desc then
        return mw.text.trim(desc)
    end
    
    -- 3. Extraire le premier paragraphe significatif
    -- Nettoyer le contenu
    local cleaned = content
    
    -- Supprimer les infobox complètes
    cleaned = cleaned:gsub('{{[Ii]nfobox.-}}', '', 1)
    
    -- 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 (au moins 20 caractères)
    for line in cleaned:gmatch('[^\n]+') do
        line = mw.text.trim(line)
        
        -- Ignorer les lignes vides, listes, tableaux
        if line ~= '' 
           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
            
            return line
        end
    end
    
    return "''Aucune description disponible''"
end

-- Fonction pour extraire le lien de documentation
function p.getDocumentation(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher un lien de documentation dans l'infobox
    local doc = content:match('|%s*documentation%s*=%s*(%[?https?://[^%s%]|]+)')
    if doc then
        doc = doc:gsub('%[', ''):gsub('%]', '')
        return '[' .. doc .. ' Documentation]'
    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
    
    if doc then
        doc = doc:gsub('%[', ''):gsub('%]', '')
        return '[' .. doc .. ' Site officiel]'
    end
    
    return "''Aucune documentation''"
end

-- Fonction de debug complète
function p.getInfo(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return "'''Erreur :''' Aucun nom de page fourni"
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj then
        return "'''Erreur :''' Titre invalide"
    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
        return output
    end
    
    output = output .. "* '''Namespace :''' " .. pageObj.namespace .. "\n"
    output = output .. "* '''Titre complet :''' " .. pageObj.fullText .. "\n"
    
    local content = pageObj:getContent()
    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
    
    return output
end

-- Fonction pour obtenir des statistiques
function p.getStats(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    local chars = #content
    local lines = select(2, content:gsub('\n', '\n')) + 1
    local words = select(2, content:gsub('%S+', ''))
    
    return string.format("%d caractères, %d lignes, ~%d mots", chars, lines, words)
end

-- Fonction pour extraire l'auteur
function p.getAuthor(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    local author = content:match('|%s*[Aa]uteur%s*=%s*([^\n|]+)')
    if author then
        return mw.text.trim(author)
    end
    
    author = content:match('|%s*[Aa]uthor%s*=%s*([^\n|]+)')
    if author then
        return mw.text.trim(author)
    end
    
    return ''
end

-- Fonction pour extraire la version
function p.getVersion(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    local version = content:match('|%s*[Vv]ersion%s*=%s*([^\n|]+)')
    if version then
        return mw.text.trim(version)
    end
    
    version = content:match('[Vv]ersion%s*:?%s*(%d+%.%d+[%.%d]*)')
    if version then
        return version
    end
    
    version = content:match('v(%d+%.%d+[%.%d]*)')
    if version then
        return version
    end
    
    return ''
end

-- Fonction pour extraire la date de mise à jour
function p.getUpdateDate(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    local date = content:match('|%s*[Mm]ise%s*à%s*jour%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    date = content:match('|%s*[Uu]pdate[Dd]?%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    date = content:match('|%s*[Dd]ate%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    return ''
end

return p