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.1.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datasource, options = {}) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(datasource, options = {})
  if options[:io]
    raise "IO source not readable" unless datasource.respond_to?(:read)

    @io_stream = datasource
    @book = create_book options
    file = Tempfile.new('epub_string')
    file.write(@io_stream)
    @filepath = file.path
    @book.epub_file = @filepath
  else
    raise "File #{datasource} not readable" unless File.readable_real? datasource

    @filepath = File.realpath datasource
    @book = create_book options
    @book.epub_file = @filepath
  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) (defaults to: {})

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

Options Hash (options):

  • :book (EPUB)

    instance of class which includes EPUB module

  • :class (Class)

    class which includes EPUB module

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.



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

def parse(filepath, options = {})
  new(filepath, options).parse
end

.parse_io(io_stream, options = {}) ⇒ Object



41
42
43
# File 'lib/epub/parser.rb', line 41

def parse_io(io_stream, options = {})
  new(io_stream, options.merge(io: true)).parse
end

Instance Method Details

#parseObject



65
66
67
68
69
70
71
72
# File 'lib/epub/parser.rb', line 65

def parse
  Zip::Archive.open @filepath do |zip|
    @book.ocf = OCF.parse(zip)
    @book.package = Publication.parse(zip, @book.ocf.container.rootfile.full_path.to_s)
  end

  @book
end

#parse_ioObject

unnecessary, but desirable maybe?



74
75
76
77
78
79
80
81
# File 'lib/epub/parser.rb', line 74

def parse_io # unnecessary, but desirable maybe?
  Zip::Archive.open_buffer @io_stream do |zip|
    @book.ocf = OCF.parse(zip)
    @book.package = Publication.parse(zip, @book.ocf.container.rootfile.full_path.to_s)
  end

  @book
end