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.map { |k, v| [v, k] } .to_h .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.
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
#document ⇒ Nokogiri::XML::Document (readonly)
107 108 109 |
# File 'lib/epuber/from_file/opf_file.rb', line 107 def document @document end |
#guide_items ⇒ Array<GuideItem> (readonly)
123 124 125 |
# File 'lib/epuber/from_file/opf_file.rb', line 123 def guide_items @guide_items end |
#manifest ⇒ Nokogiri::XML::Node? (readonly)
111 112 113 |
# File 'lib/epuber/from_file/opf_file.rb', line 111 def manifest @manifest end |
#manifest_items ⇒ Hash<String, ManifestItem> (readonly)
115 116 117 |
# File 'lib/epuber/from_file/opf_file.rb', line 115 def manifest_items @manifest_items end |
#metadata ⇒ Nokogiri::XML::Node? (readonly)
111 112 113 |
# File 'lib/epuber/from_file/opf_file.rb', line 111 def @metadata end |
#package ⇒ Nokogiri::XML::Node? (readonly)
111 112 113 |
# File 'lib/epuber/from_file/opf_file.rb', line 111 def package @package end |
#spine ⇒ Nokogiri::XML::Node? (readonly)
111 112 113 |
# File 'lib/epuber/from_file/opf_file.rb', line 111 def spine @spine end |
#spine_items ⇒ Array<SpineItem> (readonly)
119 120 121 |
# File 'lib/epuber/from_file/opf_file.rb', line 119 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).
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
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 |
#identifiers ⇒ Array<Nokogiri::XML::Node>
Return all identifiers from EPUB metadata
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.
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.
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_identifier ⇒ String?
Returns main unique identifier of this EPUB
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 |