Class: EPUB::Publication::Package::Manifest::Item

Inherits:
Object
  • Object
show all
Includes:
Inspector
Defined in:
lib/epub/publication/package/manifest.rb

Constant Summary

Constants included from Inspector

Inspector::INSTANCE_VARIABLES_OPTION, Inspector::SIMPLE_TEMPLATE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspector

#inspect_instance_variables, #inspect_object_id, #inspect_simply

Constructor Details

#initializeItem

Returns a new instance of Item.



74
75
76
# File 'lib/epub/publication/package/manifest.rb', line 74

def initialize
  @properties = Set.new
end

Instance Attribute Details

#fallbackItem

Returns the value of attribute fallback

Returns:

  • (Item)

    Returns the value of attribute fallback



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

#hrefAddressable::URI

Returns the value of href, which is relative IRI from rootfile(OPF file)

Returns:

  • (Addressable::URI)

    Returns the value of href, which is relative IRI from rootfile(OPF file)



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

#idString

Returns the value of id

Returns:

  • (String)

    Returns the value of id



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

#manifestManifest

Returns the value of manifest

Returns:

  • (Manifest)

    Returns the value of manifest



70
71
72
# File 'lib/epub/publication/package/manifest.rb', line 70

def manifest
  @manifest
end

#media_overlayString

Returns the value of media_overlay

Returns:

  • (String)

    Returns the value of media_overlay



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

#media_typeString

Returns the value of media_type

Returns:

  • (String)

    Returns the value of media_type



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

#propertiesSet<String>

Returns the value of properties

Returns:

  • (Set<String>)

    Returns the value of properties



70
71
# File 'lib/epub/publication/package/manifest.rb', line 70

attr_accessor :manifest,
:id, :href, :media_type, :fallback, :media_overlay

Instance Method Details

#content_documentObject



129
130
131
132
# File 'lib/epub/publication/package/manifest.rb', line 129

def content_document
  return nil unless %w[application/xhtml+xml image/svg+xml].include? media_type
  @content_document ||= Parser::ContentDocument.new(self).parse
end

#cover_image?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/epub/publication/package/manifest.rb', line 107

def cover_image?
  properties.include? 'cover-image'
end

#entry_nameObject

full path in archive



88
89
90
91
# File 'lib/epub/publication/package/manifest.rb', line 88

def entry_name
  rootfile = manifest.package.book.ocf.container.rootfile.full_path
  Addressable::URI.unescape(rootfile + href.normalize.request_uri)
end

#fallback_chainObject



83
84
85
# File 'lib/epub/publication/package/manifest.rb', line 83

def fallback_chain
  @fallback_chain ||= traverse_fallback_chain([])
end

#find_item_by_relative_iri(iri) ⇒ Item?

Note:

Algorithm stolen form Rack::Utils#clean_path_info

Parameters:

  • iri (Addressable::URI)

    relative iri

Returns:

  • (Item)
  • (nil)

    when item not found

Raises:

  • ArgumentError when iri is not relative

  • ArgumentError when iri starts with “/”(slash)



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/epub/publication/package/manifest.rb', line 146

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
  target_href = href + iri
  segments = target_href.to_s.split(Addressable::URI::SLASH)
  clean_segments = []
  segments.each do |segment|
    next if segment.empty? || segment == '.'
    segment == '..' ? clean_segments.pop : clean_segments << segment
  end
  target_iri = Addressable::URI.parse(clean_segments.join(Addressable::URI::SLASH))
  manifest.items.find { |item| item.href == target_iri}
end

#inspectObject



160
161
162
163
164
165
166
167
# File 'lib/epub/publication/package/manifest.rb', line 160

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

#itemrefPackage::Spine::Itemref

Returns:



136
137
138
# File 'lib/epub/publication/package/manifest.rb', line 136

def itemref
  manifest.package.spine.itemrefs.find {|itemref| itemref.idref == id}
end

Returns:

  • (Boolean)


103
104
105
# File 'lib/epub/publication/package/manifest.rb', line 103

def nav?
  properties.include? 'nav'
end

#readObject



93
94
95
96
97
# File 'lib/epub/publication/package/manifest.rb', line 93

def read
  Zip::Archive.open(manifest.package.book.epub_file) {|zip|
    zip.fopen(entry_name).read
  }
end

#use_fallback_chain(options = {}) ⇒ Object

TODO:

Handle circular fallback chain



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/epub/publication/package/manifest.rb', line 112

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[:unsupported]
    supported = supported - (del.respond_to?(:to_ary) ? del : [del])
  end

  return yield self if supported.include? media_type
  if (bindings = manifest.package.bindings) && (binding_media_type = bindings[media_type])
    return yield binding_media_type.handler
  end
  return fallback.use_fallback_chain(options) {|fb| yield fb} if fallback
  raise EPUB::MediaType::UnsupportedMediaType
end

#xhtml?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/epub/publication/package/manifest.rb', line 99

def xhtml?
  media_type == 'application/xhtml+xml'
end