Sha256: 078a45fc67ce4d0367d9f6c959586d59bee6a8037fd0b3211d07fd0a676dc05e

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'open3'

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

    class Base
      # The e-book directory.
      #
      attr_accessor :root_dir

      # Where the text files are stored.
      #
      attr_accessor :source

      def self.parse(root_dir)
        new(root_dir).parse
      end

      def initialize(root_dir)
        @root_dir = Pathname.new(root_dir)
        @source = root_dir.join("text")
      end

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

      # Return the configuration file.
      #
      def config
        Kitabu.config(root_dir)
      end

      # Render a eRb template using +locals+ as data seed.
      #
      def render_template(file, locals = {})
        context = OpenStruct.new(locals).extend(Helpers)
        ERB.new(File.read(file), 0, "%<>", "@_output").result context.instance_eval { binding }
      end

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

      def ui
        @ui ||= Thor::Base.shell.new
      end

      def handle_error(error)
        ui.say "#{error.class}: #{error.message}", :red
        ui.say error.backtrace.join("\n"), :white
      end

      def copy_directory(source, target)
        source = root_dir.join("#{source}/.")
        target = root_dir.join(target)

        FileUtils.mkdir_p target
        FileUtils.cp_r source, target, remove_destination: true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kitabu-2.0.0 lib/kitabu/parser.rb