Sha256: 04d440c213ee0a105e0afc88807a68438bb957e5e939d9a0b3a06d5b61b277e9
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require "rexml/document" begin require "nokogiri" rescue LoadError end module EPUB class Parser class XMLDocument class << self attr_accessor :backend def new(xml) if backend == :Nokogiri Nokogiri.XML(xml) else REXML::Document.new(xml) end end end module Refinements [REXML::Element, REXML::Text].each do |klass| refine klass do %i[document element text].each do |type| define_method "#{type}?" do node_type == type end end end end refine REXML::Element do def each_element_by_xpath(xpath, namespaces = nil, &block) REXML::XPath.each self, xpath, namespaces, &block end def attribute_with_prefix(name, prefix = nil) attribute(name, EPUB::NAMESPACES[prefix])&.value end alias namespace_uri namespace def content texts.join end end refine REXML::Text do alias content value end if const_defined? :Nokogiri refine Nokogiri::XML::Node do def each_element_by_xpath(xpath, namespaces = nil, &block) xpath(xpath, namespaces).each &block end def attribute_with_prefix(name, prefix = nil) attribute_with_ns(name, EPUB::NAMESPACES[prefix])&.value end def each_element(xpath = nil, &block) element_children.each(&block) end alias elements element_children def namespace_uri namespace.href end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
epub-parser-0.3.8 | lib/epub/parser/xml_document.rb |