class DocuBot::LinkTree::Node
Attributes
link[RW]
page[RW]
parent[RW]
title[RW]
Public Class Methods
new( title=nil, link=nil, page=nil )
click to toggle source
# File lib/docubot/link_tree.rb, line 7 def initialize( title=nil, link=nil, page=nil ) @title,@link,@page = title,link,page @children = [] end
Public Instance Methods
<<( node )
click to toggle source
# File lib/docubot/link_tree.rb, line 39 def <<( node ) node.parent = self @children << node end
[]( child_index )
click to toggle source
# File lib/docubot/link_tree.rb, line 44 def []( child_index ) @children[child_index] end
add_to_link_hierarchy( title, link, page=nil )
click to toggle source
Add a new link underneath a link to its logical parent
# File lib/docubot/link_tree.rb, line 25 def add_to_link_hierarchy( title, link, page=nil ) node = DocuBot::LinkTree::Node.new( title, link, page ) parent_link = if node.anchor node.file elsif File.basename(link)=='index.html' File.dirname(File.dirname(link))/'index.html' else (File.dirname(link) / 'index.html') end #puts "Adding #{title.inspect} (#{link}) to hierarchy under #{parent_link}" parent = descendants.find{ |n| n.link==parent_link } || self parent << node end
ancestors()
click to toggle source
# File lib/docubot/link_tree.rb, line 71 def ancestors # Cached assuming no one is going to shuffle the nodes after placement return @ancestors if @ancestors @ancestors = [] node = self @ancestors << node while node = node.parent @ancestors.reverse! end
anchor()
click to toggle source
# File lib/docubot/link_tree.rb, line 12 def anchor @link[/#(.+)/,1] end
children( parent_link=nil, &block )
click to toggle source
# File lib/docubot/link_tree.rb, line 48 def children( parent_link=nil, &block ) if parent_link root = find( parent_link ) root ? root.children( &block ) : [] else @children end end
depth()
click to toggle source
# File lib/docubot/link_tree.rb, line 66 def depth # Cached assuming no one is going to shuffle the nodes after placement @depth ||= ancestors.length end
descendants()
click to toggle source
# File lib/docubot/link_tree.rb, line 57 def descendants ( @children + @children.map{ |child| child.descendants } ).flatten end
file()
click to toggle source
# File lib/docubot/link_tree.rb, line 16 def file @link.sub(/#.+/,'') end
find( link )
click to toggle source
# File lib/docubot/link_tree.rb, line 61 def find( link ) # TODO: this is eminently cachable descendants.find{ |node| node.link==link } end
leaf?()
click to toggle source
# File lib/docubot/link_tree.rb, line 20 def leaf? !@children.any?{ |node| node.page != @page } end
to_s()
click to toggle source
# File lib/docubot/link_tree.rb, line 80 def to_s "#{@title} (#{@link}) - #{@page && @page.title}" end
to_txt( depth=0 )
click to toggle source
# File lib/docubot/link_tree.rb, line 84 def to_txt( depth=0 ) indent = " "*depth [ indent+to_s, children.map{|kid|kid.to_txt(depth+1)} ].flatten.join("\n") end