Sha256: c0ab560529183fad3ae32e31b9ce350b9f8bf77dae8e00c8c05c345c99f0d69b

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'open3'

module Bookshelf
  module Parser
    autoload :HTML  , "bookshelf/parser/html"
    autoload :PDF   , "bookshelf/parser/pdf"
    autoload :Epub  , "bookshelf/parser/epub"
    autoload :Mobi  , "bookshelf/parser/mobi"
    autoload :Txt   , "bookshelf/parser/txt"

    class Base
      # The e-book directory.
      #
      attr_accessor :book_dir
      
      def self.parse(book_dir)
        new(book_dir).parse
      end

      def initialize(book_dir)
        @book_dir = Pathname.new(book_dir)
      end

      # Return directory's basename.
      #
      def name
        File.basename(book_dir)
      end

      # Return the configuration file.
      #
      def config
        Bookshelf.config
      end

      # Render a eRb template using +locals+ as data seed.
      #
      def render_template(file, locals = {})
        ERB.new(File.read(file)).result OpenStruct.new(locals).instance_eval{ binding }
      end

      def spawn_command(cmd)
        begin
          stdout_and_stderr, status = Open3.capture2e(*cmd)
        rescue Errno::ENOENT => e
          puts e.message
        else
          puts stdout_and_stderr unless status.success?
          status.success?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bookshelf-1.2.0 lib/bookshelf/parser.rb
bookshelf-1.1.0 lib/bookshelf/parser.rb