Class: EPUB::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/parser.rb,
lib/epub/parser/ocf.rb,
lib/epub/parser/utils.rb,
lib/epub/parser/version.rb,
lib/epub/parser/publication.rb,
lib/epub/parser/content_document.rb

Defined Under Namespace

Modules: Utils Classes: ContentDocument, OCF, Publication

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, **options) ⇒ Parser

Returns a new instance of Parser.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/epub/parser.rb', line 44

def initialize(filepath, **options)
  path_is_uri = (options[:container_adapter] == EPUB::OCF::PhysicalContainer::UnpackedURI or
                 options[:container_adapter] == :UnpackedURI or
                 EPUB::OCF::PhysicalContainer.adapter == EPUB::OCF::PhysicalContainer::UnpackedURI)

  raise "File #{filepath} not readable" if
    !path_is_uri and !File.readable_real?(filepath)

  @filepath = path_is_uri ? filepath : File.realpath(filepath)
  @book = create_book(options)
  @book.epub_file = @filepath
  if options[:container_adapter]
    adapter = options[:container_adapter]
    @book.container_adapter = adapter
  end
end

Class Method Details

.parse(filepath, **options) ⇒ EPUB

Parse an EPUB file

Examples:

EPUB::Parser.parse('path/to/book.epub') # => EPUB::Book object
class MyBook
  include EPUB
end
book = MyBook.new
parsed_book = EPUB::Parser.parse('path/to/book.epub', :book => book) # => #<MyBook:0x000000019760e8 @epub_file=..>
parsed_book.equal? book # => true
book = EPUB::Parser.parse('path/to/book.epub', :class => MyBook) # => #<MyBook:0x000000019b0568 @epub_file=...>
book.instance_of? MyBook # => true

Parameters:

  • filepath (String)
  • options (Hash)

    the type of return is specified by this argument. If no options, returns Book object. For details of options, see below.

Options Hash (**options):

Returns:

  • (EPUB)

    object which is an instance of class including EPUB module. When option :book passed, returns the same object whose attributes about EPUB are set. When option :class passed, returns the instance of the class. Otherwise returns Book object.


39
40
41
# File 'lib/epub/parser.rb', line 39

def parse(filepath, **options)
  new(filepath, options).parse
end

Instance Method Details

#parseObject


61
62
63
64
65
66
67
68
# File 'lib/epub/parser.rb', line 61

def parse
  @book.container_adapter.open @filepath do |container|
    @book.ocf = OCF.parse(container)
    @book.package = Publication.parse(container, @book.rootfile_path)
  end

  @book
end