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

Contenu supprimé Contenu ajouté
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 :
-- Ce script crée la version imprimable d'un livre en affichant le contenu des pages de son sommaire
-- Cherche et renvoie le contenu des pages du sommaire placé en paramètre, pour créé une version imprimable du livre.
 
-- Paramètresi18n : paramètres traduis localement
Error = "Erreur : sommaire invalide"
User = "Utilisateur:"
Wikibooks = "Wikilivres:"
Help = "Aide:"
Beginning1 = "Notice de version imprimable"
Beginning2 = "Couverture de version imprimable"
Ligne 11 ⟶ 8 :
Ending1 = "GFDL"
Ending2 = ""
TOC = "Sommaire"
sep = "/"
OnlySubpages = true
debug = false
 
 
Ligne 16 ⟶ 17 :
 
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
Ligne 26 ⟶ 36 :
if (text == nil or text == '') then return Error end
 
-- Book subpages titles normalization to absolute names
if mw.ustring.find(BookName, "/") ~= nil then BookName = mw.ustring.gsub(BookName, "^(.*)/(.*)", "%1") end
text = mw.ustring.gsub(text, "%[%[%.%./", "[[" .. BookName .. "/")
text = mw.ustring.gsub(text, "%[%[/", "[[" .. BookName .. "/")
text = mw.ustring.gsub(text, "{{BOOKNAME}}", BookName)
text = mw.ustring.gsub(text, "%/%]%]", "]]")
 
local lines_ = mw.text.split(text, "\n")
local chapter
local PrintVersion = {}
for i,v in ipairs(lines_) do
if mw.text.trim(v) ~= '' then
if mw.ustring.find(v, "%[%[") ~= nil and (string.sub(v, 1, 1) == "*" or string.sub(v, 1, 1) == "#" or string.sub(v, 1, 1) == ":" or string.sub(v, 1, 1) == "=") then
chapter = p.subpage(BookName, v)
 
if chapter ~= nil then
-- Pages titles extraction from the TOC
ChapterTitle = mw.title.new(chapter)
if mw.ustring.find(v, "|") == nil or (mw.ustring.find(v, "%]") ~= nil and mw.ustring.find(v, "|") > mw.ustring.find(v, "%]")) then
if (ChapterTitle ~= nil and ChapterTitle.exists) Ending = "]"then
PageName = mw.ustring.gsub(chapter, "[^"..sep.."]*"..sep.."*(.*)", "%1")
else
if (PageName ~= nil and PageName ~= '') Ending = "|"then
table.insert(PrintVersion, frame:expandTemplate{title = Break} .. '\n<div style="clear:both;"></div>\n=' .. PageName .. '=\n')
end
end
chapter = mw.ustring.gsub(v, "[^%[]*%[%[([^%"..Ending.."]*).*", "%1")
table.insert(PrintVersion, frame:expandTemplate{ title = ':' .. chapter } .. '\n\n')
 
else
if (chapter ~= 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
if (chapter ~= '' and mw.ustring.find(chapter, "{") == nil and mw.ustring.find(chapter, "#") == nil and mw.ustring.find(chapter, User) == nil and mw.ustring.find(chapter, Wikibooks) == nil and mw.ustring.find(chapter, Help) == nil) then
end
chapter = mw.ustring.gsub(chapter, "_", " ")
end
ChapterTitle = mw.title.new(chapter)
end
if (ChapterTitle ~= nil and ChapterTitle.exists) then
PageName = mw.ustring.gsub(chapter, "[^/]*/*(.*)", "%1")
if (PageName ~= nil and PageName ~= '') then
table.insert(PrintVersion, frame:expandTemplate{title = Break} .. '\n=' .. PageName .. '=\n')
end
table.insert(PrintVersion, frame:expandTemplate{ title = ':' .. chapter } .. '\n\n')
end
end
end
end
end
Ligne 71 ⟶ 67 :
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, "{{[^}]*}}", "")
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.text.truncate(chapter, string.len(BookName .. sep)) ~= (BookName .. sep .. "…") and mw.text.truncate(chapter, string.len(BookName .. sep)) ~= (BookName .. sep .. "...") then
if debug then chapter = '<font color=red>No book subpage into the internal link:</font> ' .. mw.text.truncate(chapter, string.len(BookName .. sep)) .. " ~= " .. 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