From 88c1c6302c4b8c7ffdd25abe49c0ae76ead645c7 Mon Sep 17 00:00:00 2001 From: Marat Radchenko Date: Mon, 27 Jan 2020 11:19:01 +0300 Subject: [PATCH] resolves #276 try to use KindleGen/EPUBCheck binary from `$PATH` --- CHANGELOG.adoc | 1 + lib/asciidoctor-epub3/packager.rb | 67 +++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 17ce9fda..83a18c03 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -11,6 +11,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ * make `KINDLEGEN` env var work again (#269) * enable Pygments on non-Windows JRuby platforms (#264) * provide a human-readable error message when we fail to find KindleGen (#268) +* try to use KindleGen/EPUBCheck binary from `$PATH` (#276) == 1.5.0.alpha.11 (2020-01-26) - @slonopotamus diff --git a/lib/asciidoctor-epub3/packager.rb b/lib/asciidoctor-epub3/packager.rb index 8763afcb..518d108a 100644 --- a/lib/asciidoctor-epub3/packager.rb +++ b/lib/asciidoctor-epub3/packager.rb @@ -615,24 +615,35 @@ def package options = {} end end - def distill_epub_to_mobi epub_file, target, compress - if !(kindlegen_cmd = ENV['KINDLEGEN']).nil? - argv = [kindlegen_cmd] - else - begin - require 'kindlegen' unless defined? ::Kindlegen - rescue LoadError => e - raise 'Unable to find KindleGen. Either install the kindlegen gem or set KINDLEGEN environment variable with path to KindleGen executable', cause: e - end - argv = [::Kindlegen.command.to_s] + def get_kindlegen_command + unless (result = ENV['KINDLEGEN']).nil? + logger.debug %(Using KINDLEGEN env variable: #{result}) + return [result] + end + + begin + require 'kindlegen' unless defined? ::Kindlegen + result = ::Kindlegen.command.to_s + logger.debug %(Using KindleGen from gem: #{result}) + [result] + rescue LoadError => e + logger.debug %(#{e}; Using KindleGen from PATH) + ['kindlegen'] end + end + + def distill_epub_to_mobi epub_file, target, compress mobi_file = ::File.basename target.sub(EpubExtensionRx, '.mobi') compress_flag = KindlegenCompression[compress ? (compress.empty? ? '1' : compress.to_s) : '0'] - argv += ['-dont_append_source', compress_flag, '-o', mobi_file, epub_file].compact - # This duplicates Kindlegen.run, but we want to override executable - out, err, res = Open3.capture3(*argv) do |r| - r.force_encoding 'UTF-8' if windows? && r.respond_to?(:force_encoding) + argv = get_kindlegen_command + ['-dont_append_source', compress_flag, '-o', mobi_file, epub_file].compact + begin + # This duplicates Kindlegen.run, but we want to override executable + out, err, res = Open3.capture3(*argv) do |r| + r.force_encoding 'UTF-8' if windows? && r.respond_to?(:force_encoding) + end + rescue Errno::ENOENT => e + raise 'Unable to run KindleGen. Either install the kindlegen gem or set KINDLEGEN environment variable with path to KindleGen executable', cause: e end out.each_line do |line| @@ -650,15 +661,29 @@ def distill_epub_to_mobi epub_file, target, compress end end - def validate_epub epub_file - if !(epubcheck = ENV['EPUBCHECK']).nil? - argv = [epubcheck] - else - argv = [::Gem.ruby, ::Gem.bin_path('epubcheck-ruby', 'epubcheck')] + def get_epubcheck_command + unless (result = ENV['EPUBCHECK']).nil? + logger.debug %(Using EPUBCHECK env variable: #{result}) + return [result] + end + + begin + result = ::Gem.bin_path 'epubcheck-ruby', 'epubcheck' + logger.debug %(Using EPUBCheck from gem: #{result}) + [::Gem.ruby, result] + rescue ::Gem::Exception => e + logger.debug %(#{e}; Using EPUBCheck from PATH) + ['epubcheck'] end + end - argv += ['-w', epub_file] - out, err, res = Open3.capture3(*argv) + def validate_epub epub_file + argv = get_epubcheck_command + ['-w', epub_file] + begin + out, err, res = Open3.capture3(*argv) + rescue Errno::ENOENT => e + raise 'Unable to run EPUBCheck. Either install epubcheck-ruby gem or set EPUBCHECK environment variable with path to EPUBCheck executable', cause: e + end out.each_line do |line| logger.info line