lib/epub/publication/package/manifest.rb in epub-parser-0.2.2 vs lib/epub/publication/package/manifest.rb in epub-parser-0.2.3
- old
+ new
@@ -1,6 +1,7 @@
require 'set'
+require 'addressable/uri'
require 'rchardet'
require 'epub/constants'
require 'epub/parser/content_document'
module EPUB
@@ -62,10 +63,12 @@
def [](item_id)
@items[item_id]
end
class Item
+ DUMMY_ROOT_IRI = Addressable::URI.parse('http://example.net/').freeze
+
include Inspector
# @!attribute [rw] manifest
# @return [Manifest] Returns the value of manifest
# @!attribute [rw] id
@@ -80,35 +83,48 @@
# @!attribute [rw] media_overlay
# @return [String] Returns the value of media_overlay
# @!attribute [rw] fallback
# @return [Item] Returns the value of attribute fallback
attr_accessor :manifest,
- :id, :href, :media_type, :fallback, :media_overlay
- attr_reader :properties
+ :id, :media_type, :fallback, :media_overlay
+ attr_reader :properties, :href
def initialize
@properties = Set.new
end
def properties=(props)
@properties = props.kind_of?(Set) ? props : Set.new(props)
end
+ def href=(iri)
+ @href = iri.kind_of?(Addressable::URI) ? iri : Addressable::URI.parse(iri)
+ end
+
# @todo Handle circular fallback chain
def fallback_chain
@fallback_chain ||= traverse_fallback_chain([])
end
# full path in archive
- def entry_name
- dummy_root_iri = Addressable::URI.parse('http://example.net/') # FIXME: Use constant
+ # @return [Addressable::URI]
+ def full_path
+ return @full_path if @full_path
rootfile = manifest.package.book.ocf.container.rootfile.full_path
- en = Addressable::URI.unencode((dummy_root_iri + rootfile + href).normalize.request_uri)
- en.slice!(0) if en.start_with? '/'
- en
+ path = DUMMY_ROOT_IRI + rootfile + href
+ path.scheme = nil
+ path.host = nil
+ path.path = path.path[1..-1]
+ @full_path = path
end
+ # full path in archive
+ # @return [String]
+ def entry_name
+ Addressable::URI.unencode(full_path)
+ end
+
def read
raw_content = manifest.package.book.container_adapter.read(manifest.package.book.epub_file, entry_name)
unless media_type.start_with?('text/') or
media_type.end_with?('xml') or
@@ -176,10 +192,10 @@
# @raise ArgumentError when +iri+ is not relative
# @raise ArgumentError when +iri+ starts with "/"(slash)
# @note Algorithm stolen form Rack::Utils#clean_path_info
def find_item_by_relative_iri(iri)
raise ArgumentError, "Not relative: #{iri.inspect}" unless iri.relative?
- raise ArgumentError, "Start with slash: #{iri.inspect}" if iri.to_s.start_with? Addressable::URI::SLASH
+ raise ArgumentError, "Start with slash: #{iri.inspect}" if iri.path.start_with? Addressable::URI::SLASH
target_href = href + iri
segments = target_href.to_s.split(Addressable::URI::SLASH)
clean_segments = []
segments.each do |segment|
next if segment.empty? || segment == '.'