Module:Printable version

From Wikibooks, open books for an open world
Jump to: navigation, search
Documentation[edit] [history] [purge]

This module works with Template:Printable.

-- Search and display the book pages from the TOC page, in order to create a printable version.

-- Locally translated parameters
Error = "Error: invalid TOC"
Beginning1 = "Print version notice"
Beginning2 = "Print version cover"
Break = "Page-break"
Ending1 = ""
Ending2 = ""
TOC = "TOC"
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