Sha256: f346c785a2a77250fa5531077250524f33680fe05cbcaf563504feafaf55ac38

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

module Softcover
  module Builders
    class Mobi < Builder
      include Softcover::Utils
      include Softcover::EpubUtils

      def build!(options={})
        Softcover::Builders::Epub.new.build!(options)
        filename = mobi_filename(options)
        command  = mobi_command(filename, options)
        silent   = options[:silent] || Softcover.test?
        if options[:quiet] || silent
          silence { system(command) }
        else
          system(command)
        end
      end

      # Returns the filename of the MOBI (preview if necessary).
      def mobi_filename(options={})
        options[:preview] ? manifest.filename + '-preview' : manifest.filename
      end

      # Returns the command for making a MOBI, based on the options.
      def mobi_command(filename, options={})
        if options[:kindlegen]
          "#{kindlegen} ebooks/#{filename}.epub"
        else
          "#{calibre} ebooks/#{filename}.epub ebooks/#{filename}.mobi" +
          " #{calibre_options}"
        end
      end

      private

        def calibre
          @calibre ||= executable(dependency_filename(:calibre))
        end

        # Returns the options for the Calibre `ebook-convert` CLI.
        def calibre_options
          # Include both Mobipocket & KF8 formats.
          # Figuring this out took around two years. It really should be
          # the Calibre default.
          opts = ["--mobi-file-type both"]
          # Don't put pagebreaks in the detailed table of contents.
          opts << "--chapter /"
          if cover?
            # Add an explicit path to the cover image.
            # Figuring this out took several days.
            opts << "--cover #{cover_img_path}"
            # Get covers to work in Kindle desktop app.
            opts << "--share-not-sync"
          end
          opts.join(" ")
        end

        def kindlegen
          @kindlegen ||= executable(dependency_filename(:kindlegen))
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
softcover-1.0.beta15 lib/softcover/builders/mobi.rb
softcover-1.0.beta14 lib/softcover/builders/mobi.rb
softcover-1.0.beta13 lib/softcover/builders/mobi.rb
softcover-1.0.beta12 lib/softcover/builders/mobi.rb
softcover-1.0.beta11 lib/softcover/builders/mobi.rb