Class: Ataru::Traverser
- Inherits:
-
Object
- Object
- Ataru::Traverser
- Defined in:
- lib/ataru/traverser.rb
Constant Summary collapse
- CODE_ELEMENTS =
[:codeblock, :codespan]
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#language ⇒ Object
Returns the value of attribute language.
Instance Method Summary collapse
- #code_samples ⇒ Object
-
#initialize(document, file_name, language = "language-ruby") ⇒ Traverser
constructor
A new instance of Traverser.
- #walk_tree(element) ⇒ Object
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
#document ⇒ Object
Returns the value of attribute document.
8 9 10 |
# File 'lib/ataru/traverser.rb', line 8 def document @document end |
#file_name ⇒ Object
Returns the value of attribute file_name.
8 9 10 |
# File 'lib/ataru/traverser.rb', line 8 def file_name @file_name end |
#language ⇒ Object
Returns the value of attribute language.
8 9 10 |
# File 'lib/ataru/traverser.rb', line 8 def language @language end |
Instance Method Details
#code_samples ⇒ Object
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.[: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 |