lib/epub/publication/package/manifest.rb in epub-parser-0.0.2 vs lib/epub/publication/package/manifest.rb in epub-parser-0.0.3
- old
+ new
@@ -1,6 +1,7 @@
require 'enumerabler'
+require 'epub/constants'
module EPUB
module Publication
class Package
class Manifest
@@ -22,21 +23,57 @@
def nav
navs.first
end
+ def cover_image
+ items.selector {|i| i.properties.include? 'cover-image'}.first
+ end
+
def [](item_id)
items.selector {|item| item.id == item_id}.first
end
class Item
attr_accessor :manifest,
- :id, :href, :media_type, :fallback, :properties, :media_overlay
+ :id, :href, :media_type, :fallback, :properties, :media_overlay,
+ :iri
- alias path href
+ # To do: Handle circular fallback chain
+ def fallback_chain
+ return @fallback_chain if @fallback_chain
+ @fallback_chain = traverse_fallback_chain([])
+ end
- def to_s
- href.to_s
+ def read
+ open(iri) {|file| file.read}
+ end
+
+ # To do: Handle circular fallback chain
+ def use_fallback_chain(options = {})
+ supported = EPUB::MediaType::CORE
+ if ad = options[:supported]
+ supported = supported | (ad.respond_to?(:to_ary) ? ad : [ad])
+ end
+ if del = options[:not_supported]
+ supported = supported - (del.respond_to?(:to_ary) ? del : [del])
+ end
+
+ if supported.include? media_type
+ yield self
+ elsif fallback
+ fallback.use_fallback_chain(options) {|fallbacked| yield fallbacked}
+ else
+ raise EPUB::MediaType::NotSupportedError
+ end
+ end
+
+ protected
+
+ def traverse_fallback_chain(chain)
+ chain << self
+ return chain unless fallback
+ fallback.traverse_fallback_chain(chain)
end
end
end
end
end