lib/epub/parser.rb in epub-parser-0.1.9 vs lib/epub/parser.rb in epub-parser-0.2.0
- old
+ new
@@ -1,8 +1,8 @@
require 'epub'
require 'epub/constants'
-require 'zipruby'
+require 'epub/book'
require 'nokogiri'
module EPUB
class Parser
class << self
@@ -27,31 +27,38 @@
# @param [Hash] options the type of return is specified by this argument.
# If no options, returns {EPUB::Book} object.
# For details of options, see below.
# @option options [EPUB] :book instance of class which includes {EPUB} module
# @option options [Class] :class class which includes {EPUB} module
+ # @option options [EPUB::OCF::PhysicalContainer, Symbol] :container_adapter OCF physical container adapter to use when parsing EPUB container
+ # When class passed, it is used. When symbol passed, it is considered as subclass name of {EPUB::OCF::PhysicalContainer}.
+ # If omitted, {EPUB::OCF::PhysicalContainer.adapter} is used.
# @return [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 {EPUB::Book} object.
- def parse(filepath, options = {})
+ def parse(filepath, **options)
new(filepath, options).parse
end
end
- def initialize(filepath, options = {})
+ def initialize(filepath, **options)
raise "File #{filepath} not readable" unless File.readable_real? filepath
@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
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)
+ @book.container_adapter.open @filepath do |container|
+ @book.ocf = OCF.parse(container)
+ @book.package = Publication.parse(container, @book.rootfile_path)
end
@book
end
@@ -62,10 +69,9 @@
when params[:book]
params[:book]
when params[:class]
params[:class].new
else
- require 'epub/book'
Book.new
end
end
end
end