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.

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