Class: EpubWorm::Extractors::Content

Inherits:
Object
  • Object
show all
Extended by:
Base
Defined in:
lib/epub_worm/extractors/content.rb

Constant Summary

Constants included from Base

Base::DEFAULT_NS

Class Method Summary collapse

Methods included from Base

as_xml, element_at, elements_at, extended, ns, ns_entry, open_opf, text_at

Class Method Details

.extract(path, reference) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/epub_worm/extractors/content.rb', line 8

def self.extract(path, reference)
  open_opf(path) do |_opf_doc, opf_file_path, zip_file|
    content_file_name, content_fragment = reference.split("#", 2)
    content_file_path = ::File.join(::File.dirname(opf_file_path), content_file_name)
    content_file = zip_file.find_entry(content_file_path)
    raise ::EpubWorm::Error, "#{content_file_name} not found" unless content_file

    content_doc = as_xml(content_file)
    return content_doc unless content_fragment

    fragment_content_doc = element_at(content_doc, "//*[@id='#{content_fragment}']")
    raise ::EpubWorm::Error, "#{content_file_name}#{content_fragment} not found" unless fragment_content_doc

    fragment_content_doc
  end
end