class Spacetree::Parser
Parser
to parse a text file indented by spaces to a tree structure
Public Instance Methods
parse(s, &blk)
click to toggle source
Do parsing @s String to parse @blk if block given each line without the starting spaces is yielded
# File lib/spacetree/parser.rb, line 10 def parse s, &blk root = Node.new @indent_map = {-1 => root} s.chomp.split(/\n/).each do |line| generate_node line, &blk end root end
Protected Instance Methods
generate_node(line) { |line| ... }
click to toggle source
# File lib/spacetree/parser.rb, line 21 def generate_node line if line =~ /(^ *)([^ ].*)$/ indent = $1.size line = $2 line = yield line if block_given? parent = search_parent_node indent new_node = Node.new line @indent_map[indent] = new_node parent.children << new_node end end
search_parent_node(indent)
click to toggle source
# File lib/spacetree/parser.rb, line 33 def search_parent_node indent fail ArgumentError if indent < 0 (indent - 1).downto(-1).each do |i| res = @indent_map[i] return res if res end fail ArgumentError end