Class: Treeline::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/treeline/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(lines) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/treeline/parser.rb', line 6

def parse(lines)
  tree = {}
  @path = []
  
  lines.each do | line |
    # line.chomp!
    line.rstrip!
    line = yield(line) if block_given?
    
    # first, catch root elements (with no indent)
    if (line =~ /^\S/)
      @path = [ [ line, 0 ] ]
    else
      # next, catch indented elements
      matched = /^(\s+)/.match(line)
      unless (matched.nil?)
        indent = matched[1].length
        while (indent <= @path[0][1])
          @path.shift
        end
        line.lstrip!
        @path.unshift [ line, indent ]
      end
    end

    # puts self.to_s
    make_path_to_node(tree, @path.reverse)
    
  end
  
  tree
end