Module:Version imprimable

Un livre de Wikilivres.
Aller à : navigation, rechercher
Lua-logo-nolabel.svg Documentation du module

Utilisé par {{imprimable}} pour réunir automatiquement toutes les pages d'un livre en une seule. Pour ce faire, il utilise les pages de sommaire dont chaque ligne doit commencer par une étoile ou un dièse, puis peut être de la forme :

  • [[Livre/Page]] état d'avancement
  • <small>[[../Page/]]</small> état d'avancement
  • [[Livre/Groupe 1|Groupe 1]]
    • xxx [[Livre/Groupe 1/Page]] yyy
  • Groupe 2
    • xxx [[Livre/Groupe 2/Page]] yyy
  • xxx [[Livre/Page|Page]] yyy
  • xxx [[Livre/Groupe/Page|Page]] yyy
  • xxx [[../Page|Page]] yyy
Si le livre est trop volumineux, il catégorise dans Catégorie:Pages contenant trop d'inclusions de modèles : il suffit alors de passer un maximum de modèles du livre entre balises <noinclude>, surtout les sommaires.
-- Ce script crée la version imprimable d'un livre en affichant le contenu des pages de son sommaire

-- i18n : paramètres traduis localement
Error = "Erreur : sommaire invalide"
Beginning1 = "Notice de version imprimable"
Beginning2 = "Couverture de version imprimable"
Break = "Nouvelle page imprimée"
Ending1 = "GFDL"
Ending2 = ""
TOC = "Sommaire"
sep = "/"
OnlySubpages = true
debug = false


local p = {}

function p.displays_book(frame)
    if frame == nil then return '' end
    if frame.args == nil then return '' end
    if frame.args[1] == nil then return '' end
    local BookName = frame.args[1]
    if (BookName ~= nil and mw.text.trim(BookName) ~= '') then
        title = mw.title.new(BookName)
        if frame.args[2] ~= nil and frame.args[2] ~= '' then
            BookName = frame.args[2]
        else
            if mw.ustring.find(BookName, sep .. TOC) ~= nil then BookName = mw.ustring.gsub(BookName, "^(.*)" .. sep .. TOC .. "$", "%1") end
        end
        if frame.args[3] ~= nil then OnlySubpages = false end
    else
        return Error
    end
    if (title == nil or title == '') then return Error end
    text = title.getContent(title)
    if (text == nil or text == '') then return Error end

    -- Book subpages titles normalization to absolute names
    local lines_ = mw.text.split(text, "\n")
    local chapter
    local PrintVersion = {}
    for i,v in ipairs(lines_) do
        if mw.text.trim(v) ~= '' then
            chapter = p.subpage(BookName, v)
            if chapter ~= nil then
                ChapterTitle = mw.title.new(chapter)
                if (ChapterTitle ~= nil and ChapterTitle.exists) then
	            PageName = mw.ustring.gsub(chapter, "[^"..sep.."]*"..sep.."*(.*)", "%1")
	            if (PageName ~= nil and PageName ~= '') then
	                if Break ~= "" then table.insert(PrintVersion, frame:expandTemplate{title = Break}) end
                        table.insert(PrintVersion, '\n<div style="clear:both;"></div>\n=' .. PageName .. '=\n')
	            end
	            table.insert(PrintVersion, frame:expandTemplate{ title = ':' .. chapter } .. '\n\n')
                else
                    if debug then table.insert(PrintVersion, '<font color=red>Missing subpage "' .. chapter .. '" on line "' .. v .. '" for the book:</font> ' .. BookName .. '\n\n') end
                end
            end
	end
    end
 
    Templates1 = ""
    if Beginning1 ~= "" then Templates1 = Templates1 .. frame:expandTemplate{title = Beginning1} .. '\n' end
    if Beginning2 ~= "" then Templates1 = Templates1 .. frame:expandTemplate{title = Beginning2} .. '\n' end
    Templates2 = ""
    if Ending1 ~= "" then Templates2 = Templates2 .. frame:expandTemplate{title = Ending1} .. '\n' end
    if Ending2 ~= "" then Templates2 = Templates2 .. frame:expandTemplate{title = Ending2} .. '\n' end

    return Templates1 .. table.concat(PrintVersion, "\r\n") .. Templates2
end


function p.extract_subpage(frame)
    if frame == nil then return '' end
    if frame.args == nil then return '' end
    if frame.args[1] == nil then return '' end
    if frame.args[2] == nil then return '' end
    return p.subpage(frame.args[1], frame.args[2])
end

function p.subpage(BookName, chapter)
    if (BookName ~= nil and mw.text.trim(BookName) ~= '') or (chapter ~= nil and mw.text.trim(chapter) ~= '') then
        BookName = mw.text.trim(BookName)
        chapter = mw.text.trim(chapter)
        BookName = mw.ustring.gsub(BookName, "_", " ")
        chapter = mw.ustring.gsub(chapter, "_", " ")
    else
        if debug then chapter = '<font color=red>Incorrect book or chapter name</font>' else chapter = '' end
    end

    chapter = mw.ustring.gsub(chapter, "{{BOOKNAME}}", BookName)
    chapter = mw.ustring.gsub(chapter, "{{Modulo%|([^}]+)}}", "[[%1]]")
    chapter = mw.ustring.gsub(chapter, "{{[^}]*}}", "")
    chapter = mw.ustring.gsub(chapter, "^[%#%*:; ]*", "")
    chapter = mw.ustring.gsub(chapter, "%[%[%.%.?/", "[[" .. BookName .. "/")
    chapter = mw.ustring.gsub(chapter, "%[%[/", "[[" .. BookName .. "/")
    chapter = mw.ustring.gsub(chapter, "%/%]%]", "]]")
    if mw.ustring.find(chapter, "%[%[") ~= nil then
	-- Pages titles extraction from the TOC
	if mw.ustring.find(chapter, "|") == nil or (mw.ustring.find(chapter, "%]") ~= nil and mw.ustring.find(chapter, "|") > mw.ustring.find(chapter, "%]")) then
            Ending = "]"
	else
            Ending = "|"
	end
        chapter = mw.ustring.gsub(chapter, "[^%[]*%[%[([^%"..Ending.."]*).*", "%1") -- brackets and pipes removal
        if chapter == BookName or chapter == BookName .. sep or mw.ustring.find(chapter, "%#") ~= nil then
            if debug then chapter = '<font color=red>Chapter = book name or another subpage name</font>' else chapter = '' end
        else
            if OnlySubpages then
                -- Book subpages only (and ignoring the other links like "see also")
                if mw.ustring.find(chapter, BookName .. sep) == nil then
                    if debug then chapter = '<font color=red>No book subpage into the internal link:</font> ' .. chapter .. " doesn't include " .. BookName .. sep else chapter = '' end
                end
            end
        end
    else
        if debug then chapter = '<font color=red>No internal link</font>' else chapter = '' end
    end
   return chapter
end


return p