Class: Ataru::Traverser

Inherits:
Object
  • Object
show all
Defined in:
lib/ataru/traverser.rb

Constant Summary collapse

CODE_ELEMENTS =
[:codeblock, :codespan]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, file_name, language = "language-ruby") ⇒ Traverser

Returns a new instance of Traverser.



10
11
12
13
14
# File 'lib/ataru/traverser.rb', line 10

def initialize(document, file_name, language = "language-ruby")
  self.document = document
  self.language = language
  self.file_name = file_name
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



8
9
10
# File 'lib/ataru/traverser.rb', line 8

def document
  @document
end

#file_nameObject

Returns the value of attribute file_name.



8
9
10
# File 'lib/ataru/traverser.rb', line 8

def file_name
  @file_name
end

#languageObject

Returns the value of attribute language.



8
9
10
# File 'lib/ataru/traverser.rb', line 8

def language
  @language
end

Instance Method Details

#code_samplesObject



16
17
18
19
# File 'lib/ataru/traverser.rb', line 16

def code_samples
  tree = document.root
  self.walk_tree(tree)
end

#walk_tree(element) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ataru/traverser.rb', line 21

def walk_tree(element)
  code_samples = []
  element.children.each do |child|
    if CODE_ELEMENTS.include?(child.type)
      if child.attr["class"] == language
        line_number = child.options[:location] + 1
        code_samples << CodeSample.new(child.value.strip, file_name, line_number)
      end
    else
      code_samples += walk_tree(child)
    end
  end
  code_samples
end