From 564b126de3c406f7dc430de4fc2863b0309442b0 Mon Sep 17 00:00:00 2001 From: Context Git Mirror Bot Date: Sat, 6 Sep 2014 13:15:04 +0200 Subject: 2014-09-06 12:52:00 --- scripts/context/lua/mtx-epub.lua | 182 ++++++++++++++++++++++++++++++--------- 1 file changed, 140 insertions(+), 42 deletions(-) (limited to 'scripts/context/lua/mtx-epub.lua') diff --git a/scripts/context/lua/mtx-epub.lua b/scripts/context/lua/mtx-epub.lua index 9eb7a5c04..5c6f2cf10 100644 --- a/scripts/context/lua/mtx-epub.lua +++ b/scripts/context/lua/mtx-epub.lua @@ -18,7 +18,17 @@ if not modules then modules = { } end modules ['mtx-epub'] = { -- first we need a decent strategy to export them. More information will be -- available on the wiki. -local format, gsub = string.format, string.gsub +-- META-INF +-- container.xml +-- OEBPS +-- content.opf +-- toc.ncx +-- Images +-- Styles +-- Text +-- mimetype + +local format, gsub, find = string.format, string.gsub, string.find local concat = table.concat local replace = utilities.templates.replace @@ -71,18 +81,33 @@ local t_container = [[ ]] +-- urn:uuid: + +-- %uuid% + local t_package = [[ - + %title% %language% - urn:uuid:%uuid% + %uuid% %creator% %date% + + + %date% @@ -97,16 +122,18 @@ local t_package = [[ ]] -local t_item = [[ ]] +local t_item = [[ ]] +local t_nav = [[ ]] -local t_toc = [[ - +-- - +local t_toc = [[ + + @@ -133,23 +160,48 @@ local t_toc = [[ ]] -local t_coverxhtml = [[ +local t_navtoc = [[ - + + + navtoc + + + + + +]] + +-- +-- + +local t_coverxhtml = [[ + - cover.xhtml + cover page -
- The cover image +
+ %content%
]] +local t_coverimg = [[ + The cover image +]] + -- We need to figure out what is permitted. Numbers only seem to give -- problems is some applications as do names with dashes. Also the -- optional toc is supposed to be there and although id's are by @@ -165,6 +217,7 @@ end local mimetypes = { xhtml = "application/xhtml+xml", xml = "application/xhtml+xml", + html = "application/html", css = "text/css", svg = "image/svg+xml", png = "image/png", @@ -231,26 +284,32 @@ function scripts.epub.make() local specification = lfs.isfile(specfile) and dofile(specfile) or { } local name = specification.name or file.removesuffix(filename) - local identifier = specification.identifier or os.uuid(true) + local identifier = specification.identifier or "" local files = specification.files or { file.addsuffix(filename,"xhtml") } local images = specification.images or { } local root = specification.root or files[1] local language = specification.language or "en" - local creator = specification.author or "My Self" - local title = specification.title or "My Title" + local creator = specification.author or "context" + local title = specification.title or name local firstpage = specification.firstpage or "" local lastpage = specification.lastpage or "" -- identifier = gsub(identifier,"[^a-zA-z0-9]","") - if firstpage ~= "" then + if firstpage == "" then + -- firstpage = "firstpage.jpg" -- dummy + else images[firstpage] = firstpage end - if lastpage ~= "" then + if lastpage == "" then + -- lastpage = "lastpage.jpg" -- dummy + else images[lastpage] = lastpage end - identifier = "BookId" -- weird requirement + local uuid = format("urn:uuid:%s",os.uuid(true)) -- os.uuid() + + identifier = "bookid" -- for now local epubname = name local epubpath = file.replacesuffix(name,"tree") @@ -266,30 +325,51 @@ function scripts.epub.make() local used = { } - local function copyone(filename) + local function registerone(filename) local suffix = file.suffix(filename) local mime = mimetypes[suffix] if mime then local idmaker = idmakers[suffix] or idmakers.default - local target = file.join(epubpath,"OEBPS",filename) - file.copy(filename,target) - application.report("copying %s to %s",filename,target) used[#used+1] = replace(t_item, { - id = idmaker(filename), - filename = filename, - mime = mime, + id = idmaker(filename), + filename = filename, + mime = mime, } ) + return true end end - copyone("cover.xhtml") + local function copyone(filename,alternative) + if registerone(filename) then + local target = file.join(epubpath,"OEBPS",filename) + file.copy(alternative or filename,target) + application.report("copying %s to %s",alternative or filename,target) + end + end + + if lfs.isfile(epubcover) then + copyone(epubcover) + epubcover = false + else + registerone(epubcover) + end + copyone("toc.ncx") local function copythem(files) for i=1,#files do local filename = files[i] if type(filename) == "string" then +if file.suffix(filename) == "xhtml" then + local alternative = file.replacesuffix(filename,"html") + if lfs.isfile(alternative) then + copyone(filename,alternative) + else + copyone(filename) + end +else copyone(filename) +end end end end @@ -307,39 +387,55 @@ function scripts.epub.make() end end + used[#used+1] = replace(t_nav, { + id = "nav", + filename = "nav.xhtml", + properties = "nav", + mime = "application/xhtml+xml", + }) + + io.savedata(file.join(epubpath,"OEBPS","nav.xhtml"),replace(t_navtoc, { -- version 3.0 + root = root, + } ) ) + copythem(theimages) local idmaker = idmakers[file.suffix(root)] or idmakers.default - container = replace(t_container, { + io.savedata(file.join(epubpath,"mimetype"),mimetype) + + io.savedata(file.join(epubpath,"META-INF","container.xml"),replace(t_container, { -- version 2.0 rootfile = epubroot - } ) - package = replace(t_package, { + } ) ) + + io.savedata(file.join(epubpath,"OEBPS",epubroot),replace(t_package, { identifier = identifier, title = title, language = language, - uuid = os.uuid(), + uuid = uuid, creator = creator, date = os.date("!%Y-%m-%dT%H:%M:%SZ"), firstpage = idmaker(firstpage), manifest = concat(used,"\n"), rootfile = idmaker(root) - } ) - toc = replace(t_toc, { - identifier = identifier, + } ) ) + + -- t_toc is replaced by t_navtoc in >= 3 + + io.savedata(file.join(epubpath,"OEBPS",epubtoc), replace(t_toc, { + identifier = uuid, -- identifier, title = title, author = author, root = root, - } ) - coverxhtml = replace(t_coverxhtml, { - image = firstpage - } ) + } ) ) - io.savedata(file.join(epubpath,"mimetype"),mimetype) - io.savedata(file.join(epubpath,"META-INF","container.xml"),container) - io.savedata(file.join(epubpath,"OEBPS",epubroot),package) - io.savedata(file.join(epubpath,"OEBPS",epubtoc),toc) - io.savedata(file.join(epubpath,"OEBPS",epubcover),coverxhtml) + if epubcover then + + io.savedata(file.join(epubpath,"OEBPS",epubcover), replace(t_coverxhtml, { + content = firstpage ~= "" and replace(t_coverimg, { image = firstpage }) or "no cover page defined", + } ) ) + + end application.report("creating archive\n\n") @@ -383,3 +479,5 @@ elseif environment.argument("exporthelp") then else application.help() end + +-- java -jar d:\epubcheck\epubcheck-3.0.1.jar -v 3.0 -mode xhtml mkiv-publications.tree\mkiv-publications.epub -- cgit v1.2.3