Sha256: b8672b0c064cc194501a84da2532756061a110fef5932df53165eba6a7b9e207

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module Softcover
  module Builders
    class Preview < Builder

      def build!
        # Recall that MOBI generation makes an EPUB as a side-effect.
        Softcover::Builders::Mobi.new.build!(preview: true)
        Softcover::Builders::Pdf.new.build!(preview: true)
        extract_pdf_pages
      end

      private

        # Extracts pages from the PDF using GhostScript.
        # The page range is set by the `pdf_preview_page_range` parameter
        # in book.yml so that authors can override the default range.
        def extract_pdf_pages
          input  = File.join('ebooks', manifest.filename + '.pdf')
          output = input.sub('.pdf', '-preview.pdf')
          range  = manifest.pdf_preview_page_range.split('..').map(&:to_i)
          cmd  = %(yes | #{ghostscript} -dBATCH -sOutputFile="#{output}")
          cmd += %( -dFirstPage=#{range.first} -dLastPage=#{range.last})
          cmd += %( -sDEVICE=pdfwrite "#{input}" > /dev/null)
          execute cmd
        end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
softcover-0.9.5 lib/softcover/builders/preview.rb
softcover-0.9.4 lib/softcover/builders/preview.rb
softcover-0.9.3 lib/softcover/builders/preview.rb
softcover-0.9.2 lib/softcover/builders/preview.rb
softcover-0.9.1 lib/softcover/builders/preview.rb
softcover-0.9.0 lib/softcover/builders/preview.rb