lib/epub/publication/package/manifest.rb in epub-parser-0.1.4 vs lib/epub/publication/package/manifest.rb in epub-parser-0.1.5
- old
+ new
@@ -4,16 +4,21 @@
module EPUB
module Publication
class Package
class Manifest
+ include Inspector::PublicationModel
+
attr_accessor :package,
:id
+ def initialize
+ @items = {}
+ end
+
# @return self
def <<(item)
- @items ||= {}
item.manifest = self
@items[item.id] = item
self
end
@@ -27,25 +32,34 @@
def cover_image
items.selector {|i| i.properties.include? 'cover-image'}.first
end
+ def each_item
+ @items.each_value do |item|
+ yield item
+ end
+ end
+
def items
@items.values
end
def [](item_id)
@items[item_id]
end
class Item
+ include Inspector
+
# @!attribute [rw] manifest
# @return [Manifest] Returns the value of manifest
# @!attribute [rw] id
# @return [String] Returns the value of id
# @!attribute [rw] href
- # @return [Addressable::URI] Returns the value of href
+ # @return [Addressable::URI] Returns the value of href,
+ # which is relative path from rootfile(OPF file)
# @!attribute [rw] media_type
# @return [String] Returns the value of media_type
# @!attribute [rw] properties
# @return [Array<String>] Returns the value of properties
# @!attribute [rw] media_overlay
@@ -53,23 +67,35 @@
# @!attribute [rw] fallback
# @return [Item] Returns the value of attribute fallback
attr_accessor :manifest,
:id, :href, :media_type, :fallback, :properties, :media_overlay
+ def initialize
+ @properties = []
+ end
+
# @todo Handle circular fallback chain
def fallback_chain
@fallback_chain ||= traverse_fallback_chain([])
end
+ # full path in archive
+ def entry_name
+ rootfile = manifest.package.book.ocf.container.rootfile.full_path
+ Addressable::URI.unescape(rootfile + href.normalize.request_uri)
+ end
+
def read
- rootfile = Addressable::URI.parse(manifest.package.book.ocf.container.rootfile.full_path)
Zip::Archive.open(manifest.package.book.epub_file) {|zip|
- path = Addressable::URI.unescape(rootfile + href.normalize.request_uri)
- zip.fopen(path).read
+ zip.fopen(entry_name).read
}
end
+ def xhtml?
+ media_type == 'application/xhtml+xml'
+ end
+
def nav?
properties.include? 'nav'
end
# @todo Handle circular fallback chain
@@ -97,9 +123,18 @@
# @return [Package::Spine::Itemref]
# @return nil when no Itemref refers this Item
def itemref
manifest.package.spine.itemrefs.find {|itemref| itemref.idref == id}
+ end
+
+ def inspect
+ "#<%{class}:%{object_id} %{manifest} %{attributes}>" % {
+ :class => self.class,
+ :object_id => inspect_object_id,
+ :manifest => "@manifest=#{@manifest.inspect_simply}",
+ :attributes => inspect_instance_variables(exclude: [:@manifest])
+ }
end
protected
def traverse_fallback_chain(chain)