Class: EPUB::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/parser.rb,
lib/epub/parser/ocf.rb,
lib/epub/parser/version.rb,
lib/epub/parser/metadata.rb,
lib/epub/parser/publication.rb,
lib/epub/parser/xml_document.rb,
lib/epub/parser/content_document.rb,
lib/epub/parser/xml_document/refinements/oga.rb,
lib/epub/parser/xml_document/refinements/rexml.rb,
lib/epub/parser/xml_document/refinements/nokogiri.rb

Defined Under Namespace

Modules: Metadata Classes: ContentDocument, OCF, Publication, XMLDocument

Constant Summary collapse

VERSION =
"0.4.8"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, container_adapter: nil, book: nil, initialize_with: nil, **options) ⇒ Parser

Returns a new instance of Parser.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/epub/parser.rb', line 43

def initialize(filepath, container_adapter: nil, book: nil, initialize_with: nil, **options)
  if filepath.to_s.encoding == Encoding::ASCII_8BIT
    # On Windows and macOS, encoding of file name is set by Ruby,
    # but on UNIX, always is ASCII-8BIT
    # See https://docs.ruby-lang.org/ja/2.7.0/class/IO.html
    filepath = filepath.to_s.dup
    require "nkf"
    filepath.force_encoding NKF.guess(filepath)
  end
  path_is_uri = (container_adapter == EPUB::OCF::PhysicalContainer::UnpackedURI or
                 container_adapter == :UnpackedURI or
                 EPUB::OCF::PhysicalContainer.adapter == EPUB::OCF::PhysicalContainer::UnpackedURI)

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

  @filepath = path_is_uri ? filepath : File.realpath(filepath)
  @book = create_book(book: book, initialize_with: initialize_with, **options)
  if path_is_uri
    @book.container_adapter = :UnpackedURI
  elsif File.directory? @filepath
    @book.container_adapter = :UnpackedDirectory
  end
  @book.epub_file = @filepath
  if options[:container_adapter]
    @book.container_adapter = options[:container_adapter]
  end
end

Class Method Details

.parse(filepath, container_adapter: nil, book: nil, initialize_with: nil, **options) ⇒ EPUB

Parse an EPUB file

Examples:

EPUB::Parser.parse('path/to/book.epub') # => EPUB::Book object
class MyBook
  include EPUB::Book::Feature
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.



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

def parse(filepath, container_adapter: nil, book: nil, initialize_with: nil, **options)
  new(filepath, container_adapter: container_adapter, book: book, initialize_with: initialize_with, **options).parse
end

Instance Method Details

#parseObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/epub/parser.rb', line 72

def parse
  @book.container_adapter.open @filepath do |container|
    @book.ocf = OCF.parse(container)
    @book.ocf.container.rootfiles.each {|rootfile|
      package = Publication.parse(container, rootfile.full_path.to_s)
      rootfile.package = package
      @book.packages << package
      package.book = @book
    }
  end

  @book
end