Class: Hexp::Nokogiri::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/hexp/nokogiri/reader.rb

Overview

Read Nokogiri, turning it into Hexp

Instance Method Summary collapse

Instance Method Details

#call(node) ⇒ Hexp::Node

Take a Nokogiri root node and convert it to Hexp

Parameters:

  • node (Nokogiri::XML::Element)

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hexp/nokogiri/reader.rb', line 12

def call(node)
  return node.text if node.text? || node.cdata?

  unless node.attributes.empty?
    attrs = node.attributes.map do |key, value|
      [key.to_sym, value.value]
    end
    attrs = Hash[attrs]
  end

  recurse = ->(next_node) { call(next_node) }
  H[node.name.to_sym, attrs || {}, node.children.map(&recurse)]
end