summaryrefslogtreecommitdiff
path: root/support/tex4ebook/tex4ebook-exec_mobi.lua
diff options
context:
space:
mode:
Diffstat (limited to 'support/tex4ebook/tex4ebook-exec_mobi.lua')
-rw-r--r--support/tex4ebook/tex4ebook-exec_mobi.lua38
1 files changed, 31 insertions, 7 deletions
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
+