From 9155c481d3d3584be5206eac986a5c9bde6ce505 Mon Sep 17 00:00:00 2001
From: Hans Hagen <pragma@wxs.nl>
Date: Mon, 11 Jun 2012 00:21:00 +0200
Subject: beta 2012.06.11 00:21

---
 scripts/context/lua/mtx-context.lua |  1 +
 scripts/context/lua/mtx-epub.lua    | 53 +++++++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 5 deletions(-)

(limited to 'scripts')

diff --git a/scripts/context/lua/mtx-context.lua b/scripts/context/lua/mtx-context.lua
index 3aff2bee2..890c7455c 100644
--- a/scripts/context/lua/mtx-context.lua
+++ b/scripts/context/lua/mtx-context.lua
@@ -258,6 +258,7 @@ function ctxrunner.checkfile(ctxdata,ctxname,defaultname)
     local flags = ctxdata.flags
 
     for e in xml.collected(xmldata,"/ctx:job/ctx:flags/ctx:flag") do
+        local flag = xml.text(e) or ""
         local key, value = match(flag,"^(.-)=(.+)$")
         if key and value then
             flags[key] = value
diff --git a/scripts/context/lua/mtx-epub.lua b/scripts/context/lua/mtx-epub.lua
index 6b7f7e718..52a144c20 100644
--- a/scripts/context/lua/mtx-epub.lua
+++ b/scripts/context/lua/mtx-epub.lua
@@ -63,6 +63,7 @@ local package = [[
         <dc:identifier id="%s" opf:scheme="UUID">urn:uuid:%s</dc:identifier>
         <dc:creator>%s</dc:creator>
         <dc:date>%s</dc:date>
+        <meta name="cover" content="%s" />
     </metadata>
 
     <manifest>
@@ -70,6 +71,7 @@ local package = [[
     </manifest>
 
     <spine toc="ncx">
+        <itemref idref="cover-xhtml" />
         <itemref idref="%s" />
     </spine>
 
@@ -108,6 +110,23 @@ local toc = [[
 </ncx>
 ]]
 
+local coverxhtml = [[
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>cover.xhtml</title>
+    </head>
+    <body>
+        <div>
+            <img src="%s" alt="The cover image" style="max-width: 100%%;" />
+        </div>
+    </body>
+</html>
+]]
+
 -- 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
@@ -128,6 +147,7 @@ local mimetypes = {
     png     = "image/png",
     jpg     = "image/jpeg",
     ncx     = "application/x-dtbncx+xml",
+    gif     = "image/gif",
  -- default = "text/plain",
 }
 
@@ -202,10 +222,10 @@ function scripts.epub.make()
 
      -- identifier = gsub(identifier,"[^a-zA-z0-9]","")
 
-        if firstpage == "" then
+        if firstpage ~= "" then
             images[firstpage] = firstpage
         end
-        if lastpage == "" then
+        if lastpage ~= "" then
             images[lastpage] = lastpage
         end
 
@@ -216,6 +236,7 @@ function scripts.epub.make()
         local epubfile   = file.replacesuffix(name,"epub")
         local epubroot   = file.replacesuffix(name,"opf")
         local epubtoc    = "toc.ncx"
+        local epubcover  = "cover.xhtml"
 
         application.report("creating paths in tree %s",epubpath)
         lfs.mkdir(epubpath)
@@ -236,6 +257,7 @@ function scripts.epub.make()
             end
         end
 
+        copyone("cover.xhtml")
         copyone("toc.ncx")
 
         local function copythem(files)
@@ -264,14 +286,35 @@ function scripts.epub.make()
 
         local idmaker = idmakers[file.extname(root)] or idmakers.default
 
-        container = format(container,epubroot)
-        package   = format(package,identifier,title,language,identifier,os.uuid(),creator,os.date("!%Y-%m-%dT%H:%M:%SZ"),concat(used,"\n"),idmaker(root))
-        toc       = format(toc,identifier,title,root)
+        container = format(container,
+            epubroot
+        )
+        package = format(package,
+            identifier,
+            title,
+            language,
+            identifier,
+            os.uuid(),
+            creator,
+            os.date("!%Y-%m-%dT%H:%M:%SZ"),
+            idmaker(firstpage),
+            concat(used,"\n"),
+            idmaker(root)
+        )
+        toc = format(toc,
+            identifier,
+            title,
+            root
+        )
+        coverxhtml = format(coverxhtml,
+            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)
 
         application.report("creating archive\n\n")
 
-- 
cgit v1.2.3