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

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

Constant Summary collapse

DUMMY_ROOT_IRI =
Addressable::URI.parse('http://example.net/').freeze

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.


91
92
93
# File 'lib/epub/publication/package/manifest.rb', line 91

def initialize
  @properties = Set.new
end

Instance Attribute Details

#fallbackItem

Returns the value of attribute fallback

Returns:

  • (Item)

    Returns the value of attribute fallback


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

attr_accessor :manifest,
:id, :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)


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

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

#idString

Returns the value of id

Returns:

  • (String)

    Returns the value of id


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

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

#manifestManifest

Returns the value of manifest

Returns:

  • (Manifest)

    Returns the value of manifest


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

def manifest
  @manifest
end

#media_overlayString

Returns the value of media_overlay

Returns:

  • (String)

    Returns the value of media_overlay


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

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

#media_typeString

Returns the value of media_type

Returns:

  • (String)

    Returns the value of media_type


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

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

#propertiesSet<String>

Returns the value of properties

Returns:

  • (Set<String>)

    Returns the value of properties


87
88
# File 'lib/epub/publication/package/manifest.rb', line 87

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

Instance Method Details

#content_documentObject


178
179
180
181
# File 'lib/epub/publication/package/manifest.rb', line 178

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)

156
157
158
# File 'lib/epub/publication/package/manifest.rb', line 156

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

#entry_nameString

full path in archive

Returns:

  • (String)

122
123
124
# File 'lib/epub/publication/package/manifest.rb', line 122

def entry_name
  Addressable::URI.unencode(full_path)
end

#fallback_chainObject

TODO:

Handle circular fallback chain


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

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)


195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/epub/publication/package/manifest.rb', line 195

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.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 == '.'
    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

#full_pathAddressable::URI

full path in archive

Returns:

  • (Addressable::URI)

110
111
112
113
114
115
116
117
118
# File 'lib/epub/publication/package/manifest.rb', line 110

def full_path
  return @full_path if @full_path
  rootfile = manifest.package.book.ocf.container.rootfile.full_path
  path = DUMMY_ROOT_IRI + rootfile + href
  path.scheme = nil
  path.host = nil
  path.path = path.path[1..-1]
  @full_path = path
end

#inspectObject


209
210
211
212
213
214
215
216
# File 'lib/epub/publication/package/manifest.rb', line 209

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:


185
186
187
# File 'lib/epub/publication/package/manifest.rb', line 185

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

Returns:

  • (Boolean)

152
153
154
# File 'lib/epub/publication/package/manifest.rb', line 152

def nav?
  properties.include? 'nav'
end

#readObject


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/epub/publication/package/manifest.rb', line 126

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
      ['application/json', 'application/javascript', 'application/ecmascript', 'application/xml-dtd'].include?(media_type)
    return raw_content
  end
  # CharDet.detect doesn't raise Encoding::CompatibilityError
  # that is caused when trying compare CharDet's internal
  # ASCII-8BIT RegExp with a String with other encoding
  # because Zip::File#read returns a String with encoding ASCII-8BIT.
  # So, no need to rescue the error here.
  encoding = CharDet.detect(raw_content)['encoding']
  if encoding
    raw_content.force_encoding(encoding)
  else
    warn "No encoding detected for #{entry_name}. Set to ASCII-8BIT" if $DEBUG || $VERBOSE
    raw_content
  end
end

#use_fallback_chain(options = {}) ⇒ Object

TODO:

Handle circular fallback chain


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/epub/publication/package/manifest.rb', line 161

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)

148
149
150
# File 'lib/epub/publication/package/manifest.rb', line 148

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