lib/asciidoctor-epub3/packager.rb in asciidoctor-epub3-1.5.0.alpha.4 vs lib/asciidoctor-epub3/packager.rb in asciidoctor-epub3-1.5.0.alpha.5

- old
+ new

@@ -311,12 +311,11 @@ end class Packager KINDLEGEN = ENV['KINDLEGEN'] || 'kindlegen' EPUBCHECK = ENV['EPUBCHECK'] || %(epubcheck#{::Gem.win_platform? ? '.bat' : '.sh'}) - EpubExtensionRx = /\.epub$/ - Kf8ExtensionRx = /-kf8\.epub$/ + EpubExtensionRx = /\.epub$/i def initialize spine_doc, spine, format = :epub3, options = {} @document = spine_doc @spine = spine || [] @format = format @@ -324,11 +323,12 @@ def package options = {} doc = @document spine = @spine fmt = @format - dest = @dest_dir = options[:to_dir] || (File.expand_path ::Dir.pwd) + target = options[:target] + dest = File.dirname target images = spine.map {|item| (item.find_by context: :image) || [] }.flatten usernames = spine.map {|item| item.attr 'username' }.compact.uniq # FIXME authors should be aggregated already on parent document authors = if doc.attr? 'authors' @@ -422,13 +422,13 @@ add_content doc end ::FileUtils.mkdir_p dest unless ::File.directory? dest - epub_file = ::File.expand_path %(#{doc.attr 'docname'}#{fmt == :kf8 ? '-kf8' : nil}.epub), dest + epub_file = fmt == :kf8 ? %(#{::Asciidoctor::Helpers.rootname target}-kf8.epub) : target builder.generate_epub epub_file - puts %(Wrote #{fmt.upcase} to #{epub_file}) + puts %(Wrote #{fmt.upcase} to #{epub_file}) if $VERBOSE if options[:extract] extract_dir = epub_file.sub EpubExtensionRx, '' ::FileUtils.remove_dir extract_dir if ::File.directory? extract_dir ::Dir.mkdir extract_dir ::Dir.chdir extract_dir do @@ -440,41 +440,41 @@ end entry.extract end end end - puts %(Extracted #{fmt.upcase} to #{extract_dir}) + puts %(Extracted #{fmt.upcase} to #{extract_dir}) if $VERBOSE end if fmt == :kf8 - distill_epub_to_mobi epub_file + distill_epub_to_mobi epub_file, target elsif options[:validate] validate_epub epub_file end end # QUESTION how to enable the -c2 flag? (enables ~3-5% compression) - def distill_epub_to_mobi epub_file + def distill_epub_to_mobi epub_file, target kindlegen_cmd = KINDLEGEN unless ::File.executable? kindlegen_cmd require 'kindlegen' unless defined? ::Kindlegen kindlegen_cmd = ::Kindlegen.command end - mobi_file = ::File.basename(epub_file).sub Kf8ExtensionRx, '.mobi' + mobi_file = ::File.basename(target.sub EpubExtensionRx, '.mobi') ::Open3.popen2e(::Shellwords.join [kindlegen_cmd, '-o', mobi_file, epub_file]) {|input, output, wait_thr| - output.each {|line| puts line } + output.each {|line| puts line } unless $VERBOSE.nil? } - puts %(Wrote MOBI to #{::File.join ::File.dirname(epub_file), mobi_file}) + puts %(Wrote MOBI to #{::File.join ::File.dirname(epub_file), mobi_file}) if $VERBOSE end def validate_epub epub_file epubcheck_cmd = EPUBCHECK unless ::File.executable? epubcheck_cmd epubcheck_cmd = ::Gem.bin_path 'epubcheck', 'epubcheck' end # NOTE epubcheck gem doesn't support epubcheck command options; enable -quiet once supported ::Open3.popen2e(::Shellwords.join [epubcheck_cmd, epub_file]) {|input, output, wait_thr| - output.each {|line| puts line } + output.each {|line| puts line } unless $VERBOSE.nil? } end end end end