From 9ae8cd92650ca8d0153d663b8fdba4c04c7fd588 Mon Sep 17 00:00:00 2001
From: Hans Hagen <pragma@wxs.nl>
Date: Sat, 6 Sep 2014 15:41:00 +0200
Subject: beta 2014.09.06 15:41

---
 scripts/context/lua/mtx-context.lua |  51 ++++--
 scripts/context/lua/mtx-context.xml |   9 +-
 scripts/context/lua/mtx-epub.lua    | 344 +++++++++++++++++++-----------------
 3 files changed, 225 insertions(+), 179 deletions(-)

(limited to 'scripts')

diff --git a/scripts/context/lua/mtx-context.lua b/scripts/context/lua/mtx-context.lua
index caf7e2f2a..2e60a629b 100644
--- a/scripts/context/lua/mtx-context.lua
+++ b/scripts/context/lua/mtx-context.lua
@@ -30,6 +30,7 @@ local removesuffix  = file.removesuffix
 local validfile     = lfs.isfile
 local removefile    = os.remove
 local renamefile    = os.rename
+local formatters    = string.formatters
 
 local application = logs.application {
     name     = "mtx-context",
@@ -281,26 +282,32 @@ local function multipass_changed(oldhash, newhash)
     return false
 end
 
+local f_tempfile = formatters["%s-%s-%02d.tmp"]
+
+local function backup(run,kind,filename)
+    if run == 1 then
+        for i=1,10 do
+            local tmpname = f_tempfile(jobname,kind,i)
+            if validfile(tmpname) then
+                removefile(tmpname)
+                report("removing %a",tmpname)
+            end
+        end
+    end
+    if validfile(filename) then
+        local tmpname = f_tempfile(jobname,kind,run or 1)
+        report("copying %a into %a",filename,tmpname)
+        file.copy(filename,tmpname)
+    else
+        report("no file %a, nothing kept",filename)
+    end
+end
+
 local function multipass_copyluafile(jobname,run)
     local tuaname, tucname = jobname..".tua", jobname..".tuc"
     if validfile(tuaname) then
         if run then
-            if run == 1 then
-                for i=1,10 do
-                    local tmpname = format("%s-tuc-%02d.tmp",jobname,i)
-                    if validfile(tmpname) then
-                        removefile(tmpname)
-                        report("removing %a",tmpname)
-                    end
-                end
-            end
-            if validfile(tucname) then
-                local tmpname = format("%s-tuc-%02d.tmp",jobname,run or 1)
-                report("copying %a into %a",tucname,tmpname)
-                file.copy(tucname,tmpname)
-            else
-                report("no file %a, nothing kept",filename)
-            end
+            backup(run,"tuc",tucname)
             report("copying %a into %a",tuaname,tucname)
             report()
         end
@@ -309,6 +316,16 @@ local function multipass_copyluafile(jobname,run)
     end
 end
 
+local function multipass_copylogfile(jobname,run)
+    local logname = jobname..".log"
+    if validfile(logname) then
+        if run then
+            backup(run,"log",logname)
+            report()
+        end
+    end
+end
+
 --
 
 local pattern = lpegpatterns.utfbom^-1 * (P("%% ") + P("% ")) * Cs((1-lpegpatterns.newline)^1)
@@ -572,6 +589,7 @@ function scripts.context.run(ctxdata,filename)
     local a_jithash     = getargument("jithash")
     local a_texformat   = getargument("texformat")
     local a_keeptuc     = getargument("keeptuc")
+    local a_keeplog     = getargument("keeplog")
     --
     a_batchmode = (a_batchmode and "batchmode") or (a_nonstopmode and "nonstopmode") or (a_scrollmode and "scrollmode") or nil
     a_synctex   = check_synctex(a_synctex)
@@ -739,6 +757,7 @@ function scripts.context.run(ctxdata,filename)
                         break
                     elseif returncode == 0 then
                         multipass_copyluafile(jobname,a_keeptuc and currentrun)
+                        multipass_copylogfile(jobname,a_keeplog and currentrun)
                         if not multipass_forcedruns then
                             newhash = multipass_hashfiles(jobname)
                             if multipass_changed(oldhash,newhash) then
diff --git a/scripts/context/lua/mtx-context.xml b/scripts/context/lua/mtx-context.xml
index 009a87740..4525908d6 100644
--- a/scripts/context/lua/mtx-context.xml
+++ b/scripts/context/lua/mtx-context.xml
@@ -142,7 +142,7 @@
                     <short>assume given file present elsewhere</short>
                 </flag>
                     <flag name="nofile">
-                <short>use dummy file as jobname</short>
+                    <short>use dummy file as jobname</short>
                 </flag>
             </subcategory>
         </category>
@@ -155,7 +155,7 @@
                     <short>omit runtime statistics at the end of the run</short>
                 </flag>
                     <flag name="update">
-                <short>update context from website (not to be confused with contextgarden)</short>
+                    <short>update context from website (not to be confused with contextgarden)</short>
                 </flag>
                 <flag name="profile">
                     <short>profile job (use: mtxrun <ref name="script"/> profile <ref name="analyze"/>)</short>
@@ -164,7 +164,10 @@
                     <short>generate timing and statistics overview</short>
                 </flag>
                 <flag name="keeptuc">
-                    <short>keep previous tuc files (numbered by run)</short>
+                    <short>keep previous tuc files (jobname-tuc-[run].tmp)</short>
+                </flag>
+                <flag name="keeplog">
+                    <short>keep previous log files (jobname-log-[run].tmp)</short>
                 </flag>
             </subcategory>
             <subcategory>
diff --git a/scripts/context/lua/mtx-epub.lua b/scripts/context/lua/mtx-epub.lua
index e9a4140a2..2af21990d 100644
--- a/scripts/context/lua/mtx-epub.lua
+++ b/scripts/context/lua/mtx-epub.lua
@@ -38,7 +38,7 @@ local helpinfo = [[
  <metadata>
   <entry name="name">mtx-epub</entry>
   <entry name="detail">ConTeXt EPUB Helpers</entry>
-  <entry name="version">0.12</entry>
+  <entry name="version">1.00</entry>
  </metadata>
  <flags>
   <category name="basic">
@@ -60,7 +60,7 @@ local helpinfo = [[
 
 local application = logs.application {
     name     = "mtx-epub",
-    banner   = "ConTeXt EPUB Helpers 0.12",
+    banner   = "ConTeXt EPUB Helpers 1.00",
     helpinfo = helpinfo,
 }
 
@@ -277,202 +277,226 @@ function scripts.epub.make()
 
     local filename = environment.files[1]
 
-    if filename and filename ~= "" and type(filename) == "string" then
-
-        filename = file.basename(filename)
-        local specfile = file.replacesuffix(filename,"specification")
-        local specification = lfs.isfile(specfile) and dofile(specfile) or { }
-
-        local name       = specification.name       or file.removesuffix(filename)
-        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 "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
-         -- firstpage = "firstpage.jpg" -- dummy
-        else
-            images[firstpage] = firstpage
-        end
-        if lastpage == "" then
-         -- lastpage = "lastpage.jpg" -- dummy
-        else
-            images[lastpage] = lastpage
-        end
+    if not filename or filename == "" or type(filename) ~= "string" then
+        application.report("provide filename")
+        return
+    end
 
-        local uuid = format("urn:uuid:%s",os.uuid(true)) -- os.uuid()
-
-        identifier = "bookid" -- for now
-
-        local epubname   = name
-        local epubpath   = file.replacesuffix(name,"tree")
-        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)
-        lfs.mkdir(file.join(epubpath,"META-INF"))
-        lfs.mkdir(file.join(epubpath,"OEBPS"))
-
-        local used = { }
-
-        local function registerone(filename)
-            local suffix = file.suffix(filename)
-            local mime = mimetypes[suffix]
-            if mime then
-                local idmaker = idmakers[suffix] or idmakers.default
-                used[#used+1] = replace(t_item, {
-                    id         = idmaker(filename),
-                    filename   = filename,
-                    mime       = mime,
-                } )
-                return true
-            end
-        end
+    filename = file.basename(filename)
 
-        local function copyone(filename,alternative)
-            if registerone(filename) then
-                local target = file.join(epubpath,"OEBPS",file.basename(filename))
-                local source = alternative or filename
-                file.copy(source,target)
-                application.report("copying %s to %s",source,target)
-            end
+    local specfile = file.replacesuffix(filename,"specification")
+
+    if not lfs.isfile(specfile) then
+        application.report("unknown specificaton file %a",specfile)
+        return
+    end
+
+    local specification = dofile(specfile)
+
+    if not specification or not next(specification) then
+        application.report("invalid specificaton file %a",specfile)
+        return
+    end
+
+    local name       = specification.name       or file.removesuffix(filename)
+    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 "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
+     -- firstpage = "firstpage.jpg" -- dummy
+    else
+        images[firstpage] = firstpage
+    end
+    if lastpage == "" then
+     -- lastpage = "lastpage.jpg" -- dummy
+    else
+        images[lastpage] = lastpage
+    end
+
+    local uuid = format("urn:uuid:%s",os.uuid(true)) -- os.uuid()
+
+    identifier = "bookid" -- for now
+
+    local epubname   = name
+    local epubpath   = file.replacesuffix(name,"tree")
+    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 %a",epubpath)
+    lfs.mkdir(epubpath)
+    lfs.mkdir(file.join(epubpath,"META-INF"))
+    lfs.mkdir(file.join(epubpath,"OEBPS"))
+
+    local used = { }
+
+    local function registerone(filename)
+        local suffix = file.suffix(filename)
+        local mime = mimetypes[suffix]
+        if mime then
+            local idmaker = idmakers[suffix] or idmakers.default
+            used[#used+1] = replace(t_item, {
+                id         = idmaker(filename),
+                filename   = filename,
+                mime       = mime,
+            } )
+            return true
         end
+    end
 
-        if lfs.isfile(epubcover) then
-            copyone(epubcover)
-            epubcover = false
-        else
-            registerone(epubcover)
+    local function copyone(filename,alternative)
+        if registerone(filename) then
+            local target = file.join(epubpath,"OEBPS",file.basename(filename))
+            local source = alternative or filename
+            file.copy(source,target)
+            application.report("copying %a to %a",source,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
-                    local suffix = file.suffix(filename)
-                    if suffix == "xhtml" then
-                        local alternative = file.replacesuffix(filename,"html")
-                        if lfs.isfile(alternative) then
-                            copyone(filename,alternative)
+    copyone("toc.ncx")
+
+    local function copythem(files)
+        for i=1,#files do
+            local filename = files[i]
+            if type(filename) == "string" then
+                local suffix = file.suffix(filename)
+                if suffix == "xhtml" then
+                    local alternative = file.replacesuffix(filename,"html")
+                    if lfs.isfile(alternative) then
+                        copyone(filename,alternative)
+                    else
+                        copyone(filename)
+                    end
+                elseif suffix == "css" then
+                    if filename == "export-example.css" then
+                        if lfs.isfile(filename) then
+                            os.remove(filename)
+                            local original = resolvers.findfile(filename)
+                            application.report("updating local copy of %a from %a",filename,original)
+                            file.copy(original,filename)
                         else
-                            copyone(filename)
-                        end
-                    elseif suffix == "css" then
-                        if not lfs.isfile(filename) then
                             filename = resolvers.findfile(filename)
                         end
-                        copyone(filename)
+                    elseif not lfs.isfile(filename) then
+                        filename = resolvers.findfile(filename)
                     else
-                        copyone(filename)
+                        -- use specific local one
                     end
+                    copyone(filename)
+                else
+                    copyone(filename)
                 end
             end
         end
+    end
 
-        copythem(files)
+    copythem(files)
 
-        local theimages = { }
+    local theimages = { }
 
-        for k, v in table.sortedpairs(images) do
-            theimages[#theimages+1] = k
-            if not lfs.isfile(k) and file.suffix(k) == "svg" and file.suffix(v) == "pdf" then
-                local command = format("inkscape --export-plain-svg=%s %s",k,v)
-                application.report("running command '%s'\n\n",command)
-                os.execute(command)
-            end
+    for k, v in table.sortedpairs(images) do
+        theimages[#theimages+1] = k
+        if not lfs.isfile(k) and file.suffix(k) == "svg" and file.suffix(v) == "pdf" then
+            local command = format("inkscape --export-plain-svg=%s %s",k,v)
+            application.report("running command %a\n\n",command)
+            os.execute(command)
         end
+    end
 
-        used[#used+1] = replace(t_nav, {
-            id         = "nav",
-            filename   = "nav.xhtml",
-            properties = "nav",
-            mime       = "application/xhtml+xml",
-        })
+    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,
-        } ) )
+    io.savedata(file.join(epubpath,"OEBPS","nav.xhtml"),replace(t_navtoc, { -- version 3.0
+        root = root,
+    } ) )
 
-        copythem(theimages)
+    copythem(theimages)
 
-        local idmaker = idmakers[file.suffix(root)] or idmakers.default
+    local idmaker = idmakers[file.suffix(root)] or idmakers.default
 
-        io.savedata(file.join(epubpath,"mimetype"),mimetype)
+    io.savedata(file.join(epubpath,"mimetype"),mimetype)
 
-        io.savedata(file.join(epubpath,"META-INF","container.xml"),replace(t_container, { -- version 2.0
-            rootfile = epubroot
-        } ) )
+    io.savedata(file.join(epubpath,"META-INF","container.xml"),replace(t_container, { -- version 2.0
+        rootfile = epubroot
+    } ) )
 
-        io.savedata(file.join(epubpath,"OEBPS",epubroot),replace(t_package, {
-            identifier = identifier,
-            title      = title,
-            language   = language,
-            uuid       = uuid,
-            creator    = creator,
-            date       = os.date("!%Y-%m-%dT%H:%M:%SZ"),
-            firstpage  = idmaker(firstpage),
-            manifest   = concat(used,"\n"),
-            rootfile   = idmaker(root)
-        } ) )
+    io.savedata(file.join(epubpath,"OEBPS",epubroot),replace(t_package, {
+        identifier = identifier,
+        title      = title,
+        language   = language,
+        uuid       = uuid,
+        creator    = creator,
+        date       = os.date("!%Y-%m-%dT%H:%M:%SZ"),
+        firstpage  = idmaker(firstpage),
+        manifest   = concat(used,"\n"),
+        rootfile   = idmaker(root)
+    } ) )
 
-        -- t_toc is replaced by t_navtoc in >= 3
+    -- 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,
-        } ) )
+    io.savedata(file.join(epubpath,"OEBPS",epubtoc), replace(t_toc, {
+        identifier = uuid, -- identifier,
+        title      = title,
+        author     = author,
+        root       = root,
+    } ) )
 
-        if epubcover then
+    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",
-            } ) )
+        io.savedata(file.join(epubpath,"OEBPS",epubcover), replace(t_coverxhtml, {
+            content = firstpage ~= "" and replace(t_coverimg, { image = firstpage }) or "no cover page defined",
+        } ) )
 
-        end
+    end
 
-        application.report("creating archive\n\n")
+    application.report("creating archive\n\n")
 
-        lfs.chdir(epubpath)
-        os.remove(epubfile)
+    lfs.chdir(epubpath)
+    os.remove(epubfile)
 
-        local done = false
+    local done = false
 
-        for i=1,#zippers do
-            local zipper = zippers[i]
-            if os.execute(format(zipper.uncompressed,epubfile,"mimetype")) then
-                os.execute(format(zipper.compressed,epubfile,"META-INF"))
-                os.execute(format(zipper.compressed,epubfile,"OEBPS"))
-                done = zipper.name
-                break
-            end
+    for i=1,#zippers do
+        local zipper = zippers[i]
+        if os.execute(format(zipper.uncompressed,epubfile,"mimetype")) then
+            os.execute(format(zipper.compressed,epubfile,"META-INF"))
+            os.execute(format(zipper.compressed,epubfile,"OEBPS"))
+            done = zipper.name
+            break
         end
+    end
 
-        lfs.chdir("..")
+    lfs.chdir("..")
 
-        if done then
-            application.report("epub archive made using %s: %s",done,file.join(epubpath,epubfile))
-        else
-            local list = { }
-            for i=1,#zippers do
-                list[#list+1] = zipper.name
-            end
-            application.report("no epub archive made, install one of: %s",concat(list," "))
+    if done then
+        application.report("epub archive made using %s: %s",done,file.join(epubpath,epubfile))
+    else
+        local list = { }
+        for i=1,#zippers do
+            list[#list+1] = zipper.name
         end
-
+        application.report("no epub archive made, install one of: %s",concat(list," "))
     end
 
 end
-- 
cgit v1.2.3