Class: Epuber::OpfFile

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/from_file/opf_file.rb

Defined Under Namespace

Classes: GuideItem, ManifestItem, SpineItem

Constant Summary collapse

LANDMARKS_MAP =

reversed map of generator’s map

Compiler::OPFGenerator::LANDMARKS_MAP.map { |k, v| [v, k] }
.to_h
.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ OpfFile

Returns a new instance of OpfFile.

Parameters:

  • document (String)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/epuber/from_file/opf_file.rb', line 126

def initialize(xml)
  @document = Nokogiri::XML(xml)
  @document.remove_namespaces!

  @package = @document.at_css('package')
  @metadata = @document.at_css('package metadata')
  @manifest = @document.at_css('package manifest')
  @spine = @document.at_css('package spine')

  @manifest_items = @document.css('package manifest item')
                             .map { |node| ManifestItem.from_node(node) }
                             .map { |item| [item.id, item] }
                             .to_h
  @spine_items = @document.css('package spine itemref')
                          .map { |node| SpineItem.from_node(node) }
  @guide_items = @document.css('package guide reference')
                          .map { |node| GuideItem.from_node(node) }
end

Instance Attribute Details

#documentNokogiri::XML::Document (readonly)

Returns:

  • (Nokogiri::XML::Document)


107
108
109
# File 'lib/epuber/from_file/opf_file.rb', line 107

def document
  @document
end

#guide_itemsArray<GuideItem> (readonly)

Returns:



123
124
125
# File 'lib/epuber/from_file/opf_file.rb', line 123

def guide_items
  @guide_items
end

#manifestNokogiri::XML::Node? (readonly)

Returns:



111
112
113
# File 'lib/epuber/from_file/opf_file.rb', line 111

def manifest
  @manifest
end

#manifest_itemsHash<String, ManifestItem> (readonly)

Returns:



115
116
117
# File 'lib/epuber/from_file/opf_file.rb', line 115

def manifest_items
  @manifest_items
end

#metadataNokogiri::XML::Node? (readonly)

Returns:



111
112
113
# File 'lib/epuber/from_file/opf_file.rb', line 111

def 
  @metadata
end

#packageNokogiri::XML::Node? (readonly)

Returns:



111
112
113
# File 'lib/epuber/from_file/opf_file.rb', line 111

def package
  @package
end

#spineNokogiri::XML::Node? (readonly)

Returns:



111
112
113
# File 'lib/epuber/from_file/opf_file.rb', line 111

def spine
  @spine
end

#spine_itemsArray<SpineItem> (readonly)

Returns:



119
120
121
# File 'lib/epuber/from_file/opf_file.rb', line 119

def spine_items
  @spine_items
end

Instance Method Details

#find_navArray<ManifestItem, [:ncx, :xhtml]>?

Find nav file in EPUB (both EPUB 2 and EPUB 3). Returns array with nav and type of nav (:xhtml or :ncx).

Returns:



149
150
151
152
153
154
155
156
157
158
# File 'lib/epuber/from_file/opf_file.rb', line 149

def find_nav
  nav = @manifest_items.find { |_, item| item.properties == 'nav' }&.last
  return [nav, NavFile::MODE_XHTML] if nav

  ncx_id = @spine['toc'] if @spine
  ncx = manifest_file_by_id(ncx_id) if ncx_id
  return [ncx, NavFile::MODE_NCX] if ncx

  nil
end

#find_refines(id, property) ⇒ String?

Find meta refines in EPUB 3 metadata

Parameters:

  • id (String)
  • property (String)

Returns:

  • (String, nil)


186
187
188
# File 'lib/epuber/from_file/opf_file.rb', line 186

def find_refines(id, property)
  @metadata.at_css(%(meta[refines="##{id}"][property="#{property}"]))&.text
end

#identifiersArray<Nokogiri::XML::Node>

Return all identifiers from EPUB metadata

Returns:



175
176
177
# File 'lib/epuber/from_file/opf_file.rb', line 175

def identifiers
  @metadata.css('identifier')
end

#manifest_file_by_href(href) ⇒ ManifestItem

Find file in <manifest> by href. Throws exception when not found.

Parameters:

  • href (String)

Returns:



209
210
211
212
213
214
215
216
217
# File 'lib/epuber/from_file/opf_file.rb', line 209

def manifest_file_by_href(href)
  # remove anchor
  href = href.sub(/#.*$/, '')

  item = @manifest_items.find { |_, i| i.href == href }&.last
  raise "Manifest item with href #{href.inspect} not found" unless item

  item
end

#manifest_file_by_id(id) ⇒ ManifestItem

Find file in <manifest> by id. Throws exception when not found.

Parameters:

  • id (String)

Returns:



196
197
198
199
200
201
# File 'lib/epuber/from_file/opf_file.rb', line 196

def manifest_file_by_id(id)
  item = @manifest_items[id]
  raise "Manifest item with id #{id.inspect} not found" unless item

  item
end

#raw_unique_identifierString?

Returns main unique identifier of this EPUB

Returns:

  • (String, nil)


164
165
166
167
168
169
# File 'lib/epuber/from_file/opf_file.rb', line 164

def raw_unique_identifier
  id = @package['unique-identifier']
  return unless id

  @metadata.at_css(%(identifier[id="#{id}"]))&.text
end