Module: EpubWorm::Extractors::Base
- Included in:
- Content, CoverReference, File, Manifest, Metadata, NcxNavigation, Spine, Version, XhtmlNavigation
- Defined in:
- lib/epub_worm/extractors/base.rb
Constant Summary collapse
- DEFAULT_NS =
{ "container" => "urn:oasis:names:tc:opendocument:xmlns:container" }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #as_xml(file) ⇒ Object
- #element_at(doc, path) ⇒ Object
- #elements_at(doc, path) ⇒ Object
- #ns ⇒ Object
- #ns_entry(prefix, uri) ⇒ Object
- #open_opf(path) ⇒ Object
- #text_at(doc, path) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
13 14 15 |
# File 'lib/epub_worm/extractors/base.rb', line 13 def self.extended(base) base.instance_variable_set(:@ns, DEFAULT_NS.dup) end |
Instance Method Details
#as_xml(file) ⇒ Object
45 46 47 |
# File 'lib/epub_worm/extractors/base.rb', line 45 def as_xml(file) Nokogiri::XML(file.get_input_stream.read) end |
#element_at(doc, path) ⇒ Object
49 50 51 |
# File 'lib/epub_worm/extractors/base.rb', line 49 def element_at(doc, path) doc.at_xpath(path, ns) end |
#elements_at(doc, path) ⇒ Object
53 54 55 |
# File 'lib/epub_worm/extractors/base.rb', line 53 def elements_at(doc, path) doc.xpath(path, ns) end |
#ns ⇒ Object
22 23 24 |
# File 'lib/epub_worm/extractors/base.rb', line 22 def ns @ns || {} end |
#ns_entry(prefix, uri) ⇒ Object
17 18 19 20 |
# File 'lib/epub_worm/extractors/base.rb', line 17 def ns_entry(prefix, uri) @ns ||= {} @ns[prefix.to_s] = uri end |
#open_opf(path) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/epub_worm/extractors/base.rb', line 26 def open_opf(path) Zip::File.open(path) do |zip_file| # Get the container.xml file to get the opf file path. container_file = zip_file.find_entry "META-INF/container.xml" raise ::EpubWorm::Error, "container.xml not found" unless container_file # Read the container file to get opf file path. container_doc = Nokogiri::XML(container_file.get_input_stream.read) opf_file_path = element_at(container_doc, "//container:rootfile")["full-path"] # Read the opf file to get metadata. opf_file = zip_file.find_entry opf_file_path raise ::EpubWorm::Error, "opf file not found" unless opf_file opf_doc = Nokogiri::XML(opf_file.get_input_stream.read) yield(opf_doc, opf_file_path, zip_file) if block_given? end end |
#text_at(doc, path) ⇒ Object
57 58 59 |
# File 'lib/epub_worm/extractors/base.rb', line 57 def text_at(doc, path) element_at(doc, path)&.text&.strip end |