Sha256: 94436b50572a148d295dadd2f2a6b2e80e1f51b09ae0ab6d99d8d764d817523e

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Gollum
  module DescendantTree
    class TreeBuilder

      def initialize (wiki, name)
        @wiki = wiki
        @name = name
        @current_nest_index = 0
      end

      def tree
        current_page_name = get_current_page_name
        page_descendants = []
        previous_page = ''
        @wiki.pages.each do |page|
          if (page.url_path.include?(current_page_name))
            page_descendants << {
              title: page.title,
              url_path: page.url_path,
              nest_index: get_current_index(page, previous_page)
            }
            previous_page = page
          end
          
        end
        page_descendants
      end

      private
      
      def get_current_index(current, previous)
        return 0 if previous == ''
        current_path_array = get_path_array(current)
        previous_path_array = get_path_array(previous)
        i = -1
        previous_path_array.reverse_each do |node|
          next if current_path_array.include?(node)
          i += 1
        end
        @current_nest_index -= i
      end

      def get_current_page_name
        @name.split('.').first
      end

      def get_path_array(page)
        return page.url_path.split('/')
      end

    end  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gollum-descendant_tree-0.0.1 lib/gollum/descendant_tree/tree_builder.rb