Sha256: 3a6852c432a3b546e917c614ecf39e8e11bc44a353d600dc9f9995dd204228dd

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require File.dirname(__FILE__) + '/node'
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

          if line.branch?
            new_node = Tree::OL.new(line.text, parent)
            parents.register_parent(new_node, :for_col => curr_line_col)
          else
            new_node = Tree::LI.new(line.text, parent)
          end
          parent.children << new_node
        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.1 lib/todo_next/tree/factory.rb