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.to_h { |k, v| [v, k] }
.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ OpfFile

Returns a new instance of OpfFile.

Parameters:

  • document (String)


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

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

  @package = @document.at_css('package')
   = @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) }
                             .to_h { |item| [item.id, item] }
  @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)


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

def document
  @document
end

#guide_itemsArray<GuideItem> (readonly)

Returns:



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

def guide_items
  @guide_items
end

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

Returns:



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

def manifest
  @manifest
end

#manifest_itemsHash<String, ManifestItem> (readonly)

Returns:



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

def manifest_items
  @manifest_items
end

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

Returns:



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

def 
  
end

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

Returns:



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

def package
  @package
end

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

Returns:



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

def spine
  @spine
end

#spine_itemsArray<SpineItem> (readonly)

Returns:



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

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:



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

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)


184
185
186
# File 'lib/epuber/from_file/opf_file.rb', line 184

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

#identifiersArray<Nokogiri::XML::Node>

Return all identifiers from EPUB metadata

Returns:



173
174
175
# File 'lib/epuber/from_file/opf_file.rb', line 173

def identifiers
  .css('identifier')
end

#manifest_file_by_href(href) ⇒ ManifestItem

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

Parameters:

  • href (String)

Returns:



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

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:



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

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)


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

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

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