class Epuber::OpfFile

Constants

LANDMARKS_MAP

reversed map of generator’s map

Attributes

document[R]

@return [Nokogiri::XML::Document]

guide_items[R]

@return [Array<GuideItem>]

manifest[R]

@return [Nokogiri::XML::Node, nil]

manifest_items[R]

@return [Hash<String, ManifestItem>]

metadata[R]

@return [Nokogiri::XML::Node, nil]

package[R]

@return [Nokogiri::XML::Node, nil]

spine[R]

@return [Nokogiri::XML::Node, nil]

spine_items[R]

@return [Array<SpineItem>]

Public Class Methods

new(xml) click to toggle source

@param [String] document

# 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

Public Instance Methods

find_nav() click to toggle source

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

@return [Array<ManifestItem, [:ncx, :xhtml]>, nil] nav, ncx

# 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) click to toggle source

Find meta refines in EPUB 3 metadata

@param [String] id @param [String] property

@return [String, nil]

# 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
identifiers() click to toggle source

Return all identifiers from EPUB metadata

@return [Array<Nokogiri::XML::Node>]

# File lib/epuber/from_file/opf_file.rb, line 175
def identifiers
  @metadata.css('identifier')
end
manifest_file_by_href(href) click to toggle source

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

@param [String] href

@return [ManifestItem]

# 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) click to toggle source

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

@param [String] id

@return [ManifestItem]

# 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_identifier() click to toggle source

Returns main unique identifier of this EPUB

@return [String, nil]

# 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