From 569e4d488d3398f8ed12807dd67e2b301b7447d5 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Fri, 1 Oct 2021 03:01:47 +0000 Subject: CTAN sync 202110010301 --- support/tex4ebook/tex4ebook-exec_mobi.lua | 38 +++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'support/tex4ebook/tex4ebook-exec_mobi.lua') diff --git a/support/tex4ebook/tex4ebook-exec_mobi.lua b/support/tex4ebook/tex4ebook-exec_mobi.lua index 92b70af71c..828baeba4a 100644 --- a/support/tex4ebook/tex4ebook-exec_mobi.lua +++ b/support/tex4ebook/tex4ebook-exec_mobi.lua @@ -11,22 +11,46 @@ function run(out,params) return eb.run(out, params) end +function kindlegen(source, outputfile) + -- try to run kindlegen first + local command = "kindlegen " .. source .. " -o " .. outputfile + local status, output = ebookutils.execute(command) + log:debug("running kindlegen: " .. command, status) + -- if we cannot find kindlegen, try ebook-convert + if not output:match("Amazon") then + log:debug("kindlegen failed, trying epub-convert") + local ebookcmd = "ebook-convert " .. source .. " " .. outputfile + status, output = ebookutils.execute(ebookcmd) + if status > 0 then + log:error("Conversion to the output format failed") + log:error("Do you have either kindlegen or ebook-convert installed?") + return false + end + end + return true +end + function writeContainer() local ret = eb.writeContainer() -- convert the epub file to mobi local epubpath = eb.basedir .. "/" .. eb.outputfile - log:info("Pack mobi "..os.execute("kindlegen " .. epubpath)) -- find the mobi filename - local mobifile = epubpath:gsub("epub$", "mobi") + local mobifile = eb.outputfile:gsub("epub$", "mobi") local mobidist = eb.destdir .. eb.outputfile:gsub("epub$", "mobi") - -- copy the mobi file to the destination directory - -- the destination directory will be created by the epub writer, so it is possible to use - -- the cp function which doesn't try to create directory - ebookutils.cp(mobifile, mobidist) - + log:info("Convert Epub to mobi") + local status = kindlegen(epubpath, mobifile) + if status then + -- copy the mobi file to the destination directory + -- the destination directory will be created by the epub writer, so it is possible to use + -- the cp function which doesn't try to create directory + if mobifile ~= mobidist then + ebookutils.cp(mobifile, mobidist) + end + end return ret end function clean() return eb.clean() end + -- cgit v1.2.3