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.
Version datée du 3 octobre 2025 à 21:32 par Hiob (discussion | contributions) (Page créée avec « local p = {} -- Fonction pour extraire la description function p.getDescription(frame) local pageName = frame.args[1] if not pageName or pageName == '' then return "''Erreur : nom de page manquant''" end local pageObj = mw.title.new(pageName) if not pageObj or not pageObj.exists then return "''Page inexistante''" end local texte = pageObj:getContent() if not texte then return "''Auc... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

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

local p = {}

-- Fonction pour extraire la description
function p.getDescription(frame)
    local pageName = frame.args[1]
    
    if not pageName or pageName == '' then
        return "''Erreur : nom de page manquant''"
    end
    
    local pageObj = mw.title.new(pageName)
    
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
    
    local texte = pageObj:getContent()
    
    if not texte then
        return "''Aucune description''"
    end
    
    -- 1. Chercher {{Short description|...}}
    local shortDesc = texte:match('{{[Ss]hort%s+description|([^}]+)}}')
    if shortDesc then
        return mw.text.trim(shortDesc)
    end
    
    -- 2. Chercher dans les infobox (paramètre description)
    local infoboxDesc = texte:match('|%s*description%s*=%s*([^\n|]+)')
    if infoboxDesc then
        infoboxDesc = mw.text.trim(infoboxDesc)
        if #infoboxDesc > 10 then
            return infoboxDesc
        end
    end
    
    -- 3. Utiliser TextExtract (premier paragraphe nettoyé)
    local extract = getTextExtract(texte)
    if extract and #extract > 10 then
        return extract
    end
    
    return "''Aucune description''"
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 texte = pageObj:getContent()
    
    if not texte then
        return '—'
    end
    
    -- Chercher le paramètre documentation dans n'importe quelle infobox
    local docParam = texte:match('|%s*documentation%s*=%s*([^\n|]+)')
    
    if docParam then
        docParam = mw.text.trim(docParam)
        
        -- Si c'est déjà un lien wiki formaté [url texte]
        if docParam:match('^%[http') then
            return docParam
        end
        
        -- Si c'est une URL brute
        if docParam:match('^https?://') then
            return '[' .. docParam .. ' 📖 Documentation]'
        end
        
        -- Si c'est un lien wiki interne [[Page]]
        if docParam:match('^%[%[') then
            return docParam
        end
        
        -- Si c'est juste du texte
        if #docParam > 0 then
            return docParam
        end
    end
    
    -- Chercher aussi |lien=, |url=, |site=
    for _, param in ipairs({'lien', 'url', 'site', 'website'}) do
        local link = texte:match('|%s*' .. param .. '%s*=%s*([^\n|]+)')
        if link then
            link = mw.text.trim(link)
            if link:match('^https?://') then
                return '[' .. link .. ' 🔗 Lien]'
            end
        end
    end
    
    return '—'
end

-- Fonction interne pour extraire le premier paragraphe (façon TextExtract)
function getTextExtract(texte)
    -- Supprimer les infobox et templates au début
    texte = texte:gsub('{{.-}}', '')
    
    -- Supprimer les balises HTML
    texte = texte:gsub('<[^>]+>', '')
    
    -- Supprimer les références
    texte = texte:gsub('<ref[^>]*>.-</ref>', '')
    texte = texte:gsub('<ref[^/>]*/', '')
    
    -- Supprimer les liens internes mais garder le texte affiché
    texte = texte:gsub('%[%[([^%]|]+)|([^%]]+)%]%]', '%2')
    texte = texte:gsub('%[%[([^%]]+)%]%]', '%1')
    
    -- Supprimer les liens externes
    texte = texte:gsub('%[http[^%]]+%]', '')
    
    -- Supprimer les balises wiki (gras, italique)
    texte = texte:gsub("'''", '')
    texte = texte:gsub("''", '')
    
    -- Supprimer les fichiers/images
    texte = texte:gsub('%[%[[Ff]ichier:.-]]', '')
    texte = texte:gsub('%[%[[Ii]mage:.-]]', '')
    texte = texte:gsub('%[%[[Ff]ile:.-]]', '')
    
    -- Trouver le premier paragraphe significatif
    for ligne in texte:gmatch('[^\n]+') do
        ligne = mw.text.trim(ligne)
        
        -- Ignorer les lignes vides, titres, listes
        if #ligne > 30 and not ligne:match('^[=*#:;]') and not ligne:match('^{|') then
            -- Limiter à 200 caractères
            if #ligne > 200 then
                ligne = ligne:sub(1, 197) .. '...'
            end
            return ligne
        end
    end
    
    return nil
end

return p