Class: ContentfulConverter::TreeCloner

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful_converter/tree_cloner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nokogiri_fragment, noko_stack, rich_stack) ⇒ TreeCloner

Returns a new instance of TreeCloner.



16
17
18
19
20
# File 'lib/contentful_converter/tree_cloner.rb', line 16

def initialize(nokogiri_fragment, noko_stack, rich_stack)
  @nokogiri_fragment = nokogiri_fragment
  @noko_stack = noko_stack
  @rich_stack = rich_stack
end

Class Method Details

.nokogiri_to_rich_text(nokogiri_fragment) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/contentful_converter/tree_cloner.rb', line 8

def self.nokogiri_to_rich_text(nokogiri_fragment)
  if nokogiri_fragment.children.empty?
    return NodeBuilder.build(nokogiri_fragment).to_h
  end

  new(nokogiri_fragment, Stack.new, Stack.new).traverse_and_clone
end

Instance Method Details

#traverse_and_cloneObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/contentful_converter/tree_cloner.rb', line 22

def traverse_and_clone
  initialize_stacks

  while noko_stack.any?
    noko_node = noko_stack.pop
    rich_node = rich_stack.pop

    next unless noko_node.children.any?

    children_traversal(noko_node, rich_node)
  end

  rich_root_node.to_h
end