Module: MultiXml::Parsers::Oga

Extended by:
Oga
Includes:
Libxml2Parser
Included in:
Oga
Defined in:
lib/multi_xml/parsers/oga.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#node_to_hash(node, hash = {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/multi_xml/parsers/oga.rb', line 19

def node_to_hash(node, hash = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  node_hash = {MultiXml::CONTENT_ROOT => ""}

  name = node_name(node)

  # Insert node hash into parent hash correctly.
  case hash[name]
  when Array
    hash[name] << node_hash
  when Hash
    hash[name] = [hash[name], node_hash]
  when NilClass
    hash[name] = node_hash
  end

  # Handle child elements
  each_child(node) do |c|
    if c.is_a?(::Oga::XML::Element)
      node_to_hash(c, node_hash)
    elsif c.is_a?(::Oga::XML::Text) || c.is_a?(::Oga::XML::Cdata)
      node_hash[MultiXml::CONTENT_ROOT] += c.text
    end
  end

  # Remove content node if it is empty
  node_hash.delete(MultiXml::CONTENT_ROOT) if node_hash[MultiXml::CONTENT_ROOT].strip.empty?

  # Handle attributes
  each_attr(node) do |a|
    key = node_name(a)
    v = node_hash[key]
    node_hash[key] = ((v) ? [a.value, v] : a.value)
  end

  hash
end

#parse(io) ⇒ Object



14
15
16
17
# File 'lib/multi_xml/parsers/oga.rb', line 14

def parse(io)
  document = ::Oga.parse_xml(io)
  node_to_hash(document.children[0])
end

#parse_errorObject



10
11
12
# File 'lib/multi_xml/parsers/oga.rb', line 10

def parse_error
  LL::ParserError
end