« Module:Version imprimable » : différence entre les versions

Contenu supprimé Contenu ajouté
Aucun résumé des modifications
+footer
Ligne 1 :
-- 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 = ""
templateLeft = '<div style="float: left;">[[File:Go-previous-grey.svg|48px|link=]] [[printf]]</div>'
templateRight = '<div style="float: right;">[[printf]] [[File:Go-next-grey.svg|48px|link=]]</div>'
TOC = "Sommaire"
sep = "/"
OnlySubpages = true
debug = false
 
local p = {}
 
 
-- Cherche et renvoie le contenu des pages du sommaire placé en paramètre, pour créé une version imprimable du livre.
function p.affiche_livre_escapePattern(frame pattern_str )
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
local param = frame.args[1]
end
if (param ~= nil and mw.text.trim(param) ~= '') then
 
title = mw.title.new(param)
 
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, p._escapePattern(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 'Pas de sommaire trouvé'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 lignes = {}chapter
local PrintVersion = {}
for i,v in ipairs(lines_) do
if string.sub(v, 1, 1) == "*" or string.sub(v, 1,if 1) == "#" or stringmw.text.subtrim(v, 1, 1) =~= ":"'' then
chapter = p.subpage(BookName, v)
-- Extrait les titres des pages
if mw.ustring.find(v, "|") = if chapter ~= nil then
ChapterTitle = mw.title.new(chapter)
chapter = mw.ustring.gsub(v, "[ :\*\#\[<small>]*([^%[%]]*).*", "%1")
if (ChapterTitle ~= nil and ChapterTitle.exists) then
else
chapter PageName = mw.ustring.gsub(vchapter, "[ :\*\#\[<small>]*([^\|"..sep.."]*)"..sep.."*(.*)", "%1")
if (PageName ~= nil and PageName ~= '') then
end
if Break ~= "" then table.insert(PrintVersion, frame:expandTemplate{title = Break}) end
--chapter = title.getContent(title)
table.insert(PrintVersion, '\n<div style="clear:both;"></div>\n=' .. PageName .. '=\n')
if (chapter ~= nil) then
end
if (chapter ~= '' and mw.ustring.find(chapter, "{") == nil) then
table.insert(PrintVersion, frame:expandTemplate{ title = ':' .. chapter } .. '\n\n')
titre = mw.ustring.gsub(chapter, "[^/]*/*(.*)", "%1")
else
if (titre ~= nil) then
if debug then table.insert(PrintVersion, '<font color=red>Missing subpage "' .. chapter .. '" on line "' .. v .. '" for the book:</font> ' .. BookName .. '\n\n') end
table.insert(lignes, frame:expandTemplate{title = "Nouvelle page imprimée"} .. '\n=' .. titre .. '=\n')
end
end
end
chapitre = mw.title.new(chapter)
end
if (chapitre ~= nil and chapitre.exists) then
table.insert(lignes, frame:expandTemplate{ title = ':' .. chapter } .. '\n\n')
end
end
end
end
end
Templates1 = ""
debut = frame:expandTemplate{title = "Notice de version imprimable"} ..
'\n' if Beginning1 ~= "" then Templates1 = Templates1 .. frame:expandTemplate{title = "CouvertureBeginning1} de.. version'\n' imprimable"}end
finif Beginning2 ~= "" then Templates1 = Templates1 .. frame:expandTemplate{title = "GFDL"Beginning2} .. '\n' end
Templates2 = ""
local liste = debut .. table.concat(lignes, "\r\n") .. fin
if Ending1 ~= "" then Templates2 = Templates2 .. frame:expandTemplate{title = Ending1} .. '\n' end
return liste
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, "{{[Mm]odulo%|([^}]+)}}", "[[%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, "%/%]%]", "]]")
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
if mw.ustring.find(chapter, "%/%|") == nil or mw.ustring.find(chapter, "%/%|") > mw.ustring.find(chapter, "%|") then
Ending = "%|"
else
Ending = "%/%|"
end
end
chapter = mw.text.split(chapter, Ending)[1] -- extraction of the line beginning
--chapter = mw.text.split(chapter, "%[%[")[2]
chapter = mw.ustring.gsub(chapter, "[^%[]*%[%[(.*)", "%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 = ' .. chapter .. ' => book name or another subpage name</font> with Ending = ' .. Ending else chapter = '' end
else
if OnlySubpages then
-- Book subpages only (and ignoring the other links like "see also")
if mw.ustring.find(chapter, p._escapePattern(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> for: " .. chapter .. "\n" else chapter = '' end
end
return chapter
end
 
 
function p.displays_footer(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]
local currentPageName = frame.args[2]
 
if (BookName ~= nil and mw.text.trim(BookName) ~= '') then
title = mw.title.new(BookName)
if mw.ustring.find(BookName, p._escapePattern(sep .. TOC)) ~= nil then BookName = mw.ustring.gsub(BookName, "^(.*)" .. sep .. TOC .. "$", "%1") end
else
return Error
end
 
if (currentPageName ~= nil and mw.text.trim(currentPageName) ~= '') then
currentPageName = mw.text.trim(currentPageName)
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 footer = {}
local previousChapter = ''
local found = false
local chapter
 
for i,v in ipairs(lines_) do
chapter = p.subpage(BookName, v)
if chapter ~= nil then
if mw.ustring.find(chapter, p._escapePattern(BookName .. sep)) == nil then
chapter = BookName .. sep .. chapter
end
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 found == true then
if debug then table.insert(footer, "<font color=red>Previous & next chapter insertion</font>\n") end
if previousChapter == '' then
theTemplateLeft, nb = mw.ustring.gsub(templateLeft, "printf", BookName .. '|' .. TOC)
else
theTemplateLeft, nb = mw.ustring.gsub(templateLeft, "printf", BookName .. sep .. previousChapter .. '|' .. previousChapter)
end
table.insert(footer, theTemplateLeft)
theTemplateRight, nb = mw.ustring.gsub(templateRight, "printf", BookName .. sep .. PageName .. '|' .. PageName)
table.insert(footer, theTemplateRight)
break
elseif PageName == currentPageName then
found = true
if debug then table.insert(footer, "<font color=red>Page</font> '" .. currentPageName .. "' found\n\n") end
elseif mw.text.trim(v) ~= '' then
previousChapter = PageName
else
if debug then table.insert(footer, "<font color=red>The current page</font> '" .. PageName .. "' is not '" .. currentPageName .. "'") end
end
end
else
if debug then table.insert(footer, "<font color=red>The page</font> '" .. chapter .. "' doesn't exist, for '" .. currentPageName .. "'\n\n") end
end
end
end
 
if found == true and table.getn(footer) == 0 then
if debug then table.insert(footer, "<font color=red>No next chapter</font>\n") end
theTemplateLeft, nb = mw.ustring.gsub(templateLeft, "printf", BookName .. sep .. previousChapter .. '|' .. previousChapter)
table.insert(footer, theTemplateLeft)
theTemplateRight, nb = mw.ustring.gsub(templateRight, "printf", BookName .. '|' .. TOC)
table.insert(footer, theTemplateRight)
end
 
return table.concat(footer, "")
end
 
 
return p