Sha256: 117d8a29321d074948990901c419ac11959082907a8bd57de0b1baec77d0724e

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require File.dirname(__FILE__) + '/node'
require File.dirname(__FILE__) + '/nodes'
require File.dirname(__FILE__) + '/parents_list'

module TodoNext
  class Tree
    class Factory

      def self.build(lines)
        mark_the_leaves_and_the_branches(lines)

        tree    = Tree.new
        parents = ParentsList.new

        lines.each do |line|
          curr_line_col = line.col_offset
          parent        = parents.get_for_item_at_column(curr_line_col) || tree

          new_node = if     line.example? then Tree::EX.new(line.text, parent)
                     elsif  line.branch?  then Tree::OL.new(line.text, parent)
                     else                      Tree::LI.new(line.text, parent)
                     end
          parent.children << new_node

          if line.branch?
            parents.register_parent(new_node, :for_col => curr_line_col)
          end
        end
        tree
      end

    private

      def self.mark_the_leaves_and_the_branches(lines)
        lines.each_with_index do |curr_line, idx|
          next_line          = lines[1+idx]
          next_line_is_child = next_line && curr_line.col_offset < next_line.col_offset
          curr_line.leaf     = !next_line_is_child
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
todo_next-0.0.2 lib/todo_next/tree/factory.rb