Class: Epuber::OpfFile
- Inherits:
-
Object
- Object
- Epuber::OpfFile
- 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
- #document ⇒ Nokogiri::XML::Document readonly
- #guide_items ⇒ Array<GuideItem> readonly
- #manifest ⇒ Nokogiri::XML::Node? readonly
- #manifest_items ⇒ Hash<String, ManifestItem> readonly
- #metadata ⇒ Nokogiri::XML::Node? readonly
- #package ⇒ Nokogiri::XML::Node? readonly
- #spine ⇒ Nokogiri::XML::Node? readonly
- #spine_items ⇒ Array<SpineItem> readonly
Instance Method Summary collapse
-
#find_nav ⇒ Array<ManifestItem, [:ncx, :xhtml]>?
Find nav file in EPUB (both EPUB 2 and EPUB 3).
-
#find_refines(id, property) ⇒ String?
Find meta refines in EPUB 3 metadata.
-
#identifiers ⇒ Array<Nokogiri::XML::Node>
Return all identifiers from EPUB metadata.
-
#initialize(xml) ⇒ OpfFile
constructor
A new instance of OpfFile.
-
#manifest_file_by_href(href) ⇒ ManifestItem
Find file in <manifest> by href.
-
#manifest_file_by_id(id) ⇒ ManifestItem
Find file in <manifest> by id.
-
#raw_unique_identifier ⇒ String?
Returns main unique identifier of this EPUB.
Constructor Details
#initialize(xml) ⇒ OpfFile
Returns a new instance of OpfFile.
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
#document ⇒ Nokogiri::XML::Document (readonly)
106 107 108 |
# File 'lib/epuber/from_file/opf_file.rb', line 106 def document @document end |
#guide_items ⇒ Array<GuideItem> (readonly)
122 123 124 |
# File 'lib/epuber/from_file/opf_file.rb', line 122 def guide_items @guide_items end |
#manifest ⇒ Nokogiri::XML::Node? (readonly)
110 111 112 |
# File 'lib/epuber/from_file/opf_file.rb', line 110 def manifest @manifest end |
#manifest_items ⇒ Hash<String, ManifestItem> (readonly)
114 115 116 |
# File 'lib/epuber/from_file/opf_file.rb', line 114 def manifest_items @manifest_items end |
#metadata ⇒ Nokogiri::XML::Node? (readonly)
110 111 112 |
# File 'lib/epuber/from_file/opf_file.rb', line 110 def end |
#package ⇒ Nokogiri::XML::Node? (readonly)
110 111 112 |
# File 'lib/epuber/from_file/opf_file.rb', line 110 def package @package end |
#spine ⇒ Nokogiri::XML::Node? (readonly)
110 111 112 |
# File 'lib/epuber/from_file/opf_file.rb', line 110 def spine @spine end |
#spine_items ⇒ Array<SpineItem> (readonly)
118 119 120 |
# File 'lib/epuber/from_file/opf_file.rb', line 118 def spine_items @spine_items end |
Instance Method Details
#find_nav ⇒ Array<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).
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
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 |
#identifiers ⇒ Array<Nokogiri::XML::Node>
Return all identifiers from EPUB metadata
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.
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.
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_identifier ⇒ String?
Returns main unique identifier of this EPUB
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 |