and not easy to do.
-- So the user will see the image legends and know a bit about
-- the images they chose to not get.
end
-- Force a GC to free the memory we used (the second call may help
-- reclaim more memory).
collectgarbage()
collectgarbage()
return images, html
end
function EpubBuilder:setTitle(title)
self.title = title
end
function EpubBuilder:addToc(chapters)
local toc_ncx_parts = {}
local depth = 0
local num = 0
for index, chapter in ipairs(chapters) do
-- Add nav part for each chapter.
table.insert(
toc_ncx_parts,
string.format([[
%s]],
num,
num,
chapter.title,
chapter.md5
)
)
num = num + 1
end
-- Prepend NCX head.
table.insert(
toc_ncx_parts,
1,
string.format([[
%s
]],
"placeholder_bookid",
depth,
self.title
)
)
-- Append NCX tail.
table.insert(
toc_ncx_parts,
[[
]]
)
self.ncx_toc = toc_ncx_parts
end
function EpubBuilder:addManifest(chapters, images)
local content_opf_parts = {}
local spine_parts = {}
local meta_cover = ""
if #images > 0 then
for inum, image in ipairs(images) do
table.insert(
content_opf_parts,
string.format([[
%s]],
image.imgid,
image.imgpath,
image.mimetype,
"\n"
)
)
-- See if the image has the tag we previously set indicating
-- it can be used as a cover image.
if image.cover_image then
meta_cover = string.format([[
]], image.imgid)
end
end
end
if #chapters > 0 then
for index, chapter in ipairs(chapters) do
table.insert(
content_opf_parts,
string.format([[
%s]],
chapter.md5,
chapter.md5,
"\n"
)
)
table.insert(
spine_parts,
string.format([[
%s]],
chapter.md5,
"\n"
)
)
end
end
logger.dbg("meta_cover:", meta_cover)
table.insert(
content_opf_parts,
1,
string.format([[
%s
KOReader %s
%s
]], self.title, Version:getCurrentRevision(), meta_cover)
)
-- tail
table.insert(
content_opf_parts,
string.format([[
%s
]], table.concat(spine_parts)
)
)
self.ncx_manifest = content_opf_parts
end
function EpubBuilder:addContents(chapters)
local contents = {}
for index, chapter in ipairs(chapters) do
table.insert(
contents,
{
filename = chapter.md5 .. ".html",
html = chapter.html,
}
)
end
self.ncx_contents = contents
end
function EpubBuilder:addImages(images)
local images_table = {}
for index, image in ipairs(images) do
if not image.src then
return
end
local src = image.src
local success, content = NewsHelpers:getUrlContent(src)
-- success, content = NewsHelpers:getUrlContent(src..".unexistant") -- to simulate failure
if success then
logger.dbg("EpubBuilder:addImages = success, size:", #content)
else
logger.dbg("EpubBuilder:addImages = failure fetching:", src)
end
if success then
-- Images do not need to be compressed, so spare some cpu cycles
local no_compression = true
if image.mimetype == "image/svg+xml" then -- except for SVG images (which are XML text)
no_compression = false
end
table.insert(
images_table,
{
path = image.imgpath,
content = content,
compression = no_compression
}
)
end
end
self.ncx_images = images_table
end
-- There can be multiple links.
-- For now we just assume the first link is probably the right one.
--- @todo Write unit tests.
-- Some feeds that can be used for unit test.
-- http://fransdejonge.com/feed/ for multiple links.
-- https://github.com/koreader/koreader/commits/master.atom for single link with attributes.
function EpubBuilder:getFeedLink(possible_link)
local E = {}
logger.dbg("Possible link", possible_link)
if type(possible_link) == "string" then
return possible_link
elseif (possible_link._attr or E).href then
return possible_link._attr.href
elseif ((possible_link[1] or E)._attr or E).href then
return possible_link[1]._attr.href
end
end
return EpubBuilder