Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 2 : Ligne 2 :


function p.depuis(frame)
function p.depuis(frame)
     local titre = frame.args["page"]
     local brut = frame.args["page"] or ""
     if not titre or titre == "" then
    local titre = mw.text.trim(brut)  -- 🔧 Supprime espaces et retours à la ligne
 
     if titre == "" then
         return "<span style='color:red;'>Erreur : aucun titre fourni.</span>"
         return "<span style='color:red;'>Erreur : aucun titre fourni.</span>"
     end
     end
Ligne 17 : Ligne 19 :
     end
     end


    -- Extraction multi-ligne avec mws.ustring
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
     local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
     if not extrait then
     if not extrait then
         return "<span style='color:gray;'>Aucun extrait trouvé. Vérifie les balises : &lt;!--extrait--&gt; ... &lt;!--/extrait--&gt;</span>"
         return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
     end
     end



Version du 12 juillet 2025 à 18:48

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

local p = {}

function p.depuis(frame)
    local brut = frame.args["page"] or ""
    local titre = mw.text.trim(brut)  -- 🔧 Supprime espaces et retours à la ligne

    if titre == "" then
        return "<span style='color:red;'>Erreur : aucun titre fourni.</span>"
    end

    local page = mw.title.new(titre)
    if not page or not page.exists then
        return "<span style='color:red;'>Erreur : la page '" .. titre .. "' n'existe pas.</span>"
    end

    local contenu = page:getContent()
    if not contenu then
        return "<span style='color:red;'>Erreur : contenu inaccessible.</span>"
    end

    local extrait = mw.ustring.match(contenu, "<!%-%-%s*extrait%s*%-%->(.-)<!%-%-%s*/extrait%s*%-%->")
    if not extrait then
        return "<span style='color:gray;'>Aucun extrait trouvé dans la page.</span>"
    end

    return mw.getCurrentFrame():preprocess(extrait)
end

return p