Class: Asciidoctor::Html::TreeWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/html/tree_walker.rb

Overview

Walks the document tree

Instance Method Summary collapse

Constructor Details

#initialize(document, max_levels = 20) ⇒ TreeWalker



7
8
9
10
11
12
# File 'lib/asciidoctor/html/tree_walker.rb', line 7

def initialize(document, max_levels = 20)
  @max_levels = max_levels
  @idx = [0] * (max_levels + 1) # index of next unexplored block at each level
  @path = [document]
  @level = 0 # the current level
end

Instance Method Details

#next_blockObject



14
15
16
17
18
# File 'lib/asciidoctor/html/tree_walker.rb', line 14

def next_block
  return nil if @path.empty?

  @path.last
end

#walk(&callback) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/asciidoctor/html/tree_walker.rb', line 20

def walk(&callback)
  block = next_block
  return nil unless block

  if block.blocks? && @level < @max_levels && @idx[@level + 1] < block.blocks.size
    @level += 1
    el = block.blocks[@idx[@level]]
    el = el.last if el.is_a?(Array) # Get the <dd> from an array of <dt>'s followed by <dd>
    @path.push(el)
    callback.call(:explore)
  else
    @idx[@level + 1] = 0 if @level < @max_levels
    @idx[@level] += 1
    @level -= 1
    @path.pop
    callback.call(:retreat)
  end
end