Class: DocuBot::LinkTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/docubot/link_tree.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, link = nil, page = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
# File 'lib/docubot/link_tree.rb', line 7

def initialize( title=nil, link=nil, page=nil )
  @title,@link,@page = title,link,page
  @children = []
end

Instance Attribute Details

Returns the value of attribute link.



5
6
7
# File 'lib/docubot/link_tree.rb', line 5

def link
  @link
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/docubot/link_tree.rb', line 5

def page
  @page
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/docubot/link_tree.rb', line 5

def parent
  @parent
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/docubot/link_tree.rb', line 5

def title
  @title
end

Instance Method Details

#<<(node) ⇒ Object



39
40
41
42
# File 'lib/docubot/link_tree.rb', line 39

def <<( node )
  node.parent = self
  @children << node
end

#[](child_index) ⇒ Object



44
45
46
# File 'lib/docubot/link_tree.rb', line 44

def []( child_index )
  @children[child_index]
end

Add a new link underneath a link to its logical parent



25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

#ancestorsObject



71
72
73
74
75
76
77
78
# 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

#anchorObject



12
13
14
# File 'lib/docubot/link_tree.rb', line 12

def anchor
  @link[/#(.+)/,1]
end

#children(parent_link = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
# 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

#depthObject



66
67
68
69
# 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

#descendantsObject



57
58
59
# File 'lib/docubot/link_tree.rb', line 57

def descendants
  ( @children + @children.map{ |child| child.descendants } ).flatten
end

#fileObject



16
17
18
# File 'lib/docubot/link_tree.rb', line 16

def file
  @link.sub(/#.+/,'')
end

#find(link) ⇒ Object



61
62
63
64
# File 'lib/docubot/link_tree.rb', line 61

def find( link )
  # TODO: this is eminently cachable

  descendants.find{ |node| node.link==link }
end

#leaf?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/docubot/link_tree.rb', line 20

def leaf?
  !@children.any?{ |node| node.page != @page }
end

#to_sObject



80
81
82
# File 'lib/docubot/link_tree.rb', line 80

def to_s
  "#{@title} (#{@link}) - #{@page && @page.title}"
end

#to_txt(depth = 0) ⇒ Object



84
85
86
87
88
89
90
# 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