Sha256: c3cf57a39b7fac5df757966c15a9c363d4ba1cfd18760367393eb4ec07cbd781

Contents?: true

Size: 1.42 KB

Versions: 44

Compression:

Stored size: 1.42 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')
          unless manifest.respond_to?(:pdf_preview_page_range)
            $stderr.puts("Error: Preview not built")
            $stderr.puts("Define pdf_preview_page_range in config/book.yml")
            $stderr.puts("See http://manual.softcover.io/book/getting_started#sec-build_preview")
            exit(1)
          end
          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 2> /dev/null)
          execute cmd
        end

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

Version data entries

44 entries across 44 versions & 2 rubygems

Version Path
softcover-1.1.6 lib/softcover/builders/preview.rb
softcover-1.1.4 lib/softcover/builders/preview.rb
softcover-1.1.3 lib/softcover/builders/preview.rb
softcover-1.1.2 lib/softcover/builders/preview.rb
softcover-1.1.1 lib/softcover/builders/preview.rb
softcover-1.1.0 lib/softcover/builders/preview.rb
softcover-1.1.beta2 lib/softcover/builders/preview.rb
softcover-1.0.5 lib/softcover/builders/preview.rb
softcover-1.1.beta1 lib/softcover/builders/preview.rb
softcover-1.0.4 lib/softcover/builders/preview.rb
softcover-1.0.3 lib/softcover/builders/preview.rb
softcover-1.0.2 lib/softcover/builders/preview.rb
softcover-1.0.1 lib/softcover/builders/preview.rb
softcover-1.0.0 lib/softcover/builders/preview.rb
softcover-1.0.beta17 lib/softcover/builders/preview.rb
softcover-1.0.beta16 lib/softcover/builders/preview.rb
softcover-1.0.beta15 lib/softcover/builders/preview.rb
softcover-1.0.beta14 lib/softcover/builders/preview.rb
softcover-1.0.beta13 lib/softcover/builders/preview.rb
softcover-1.0.beta12 lib/softcover/builders/preview.rb