Class: Jekyll::Tree
- Inherits:
-
Object
- Object
- Jekyll::Tree
- Defined in:
- lib/jekyll-namespaces/tree.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#add_path(doc, node = nil, depth = 1) ⇒ Object
add unique path for the given doc to tree (node-class).
-
#find_doc_ancestors_and_children_metadata(target_doc, node = nil, ancestors = []) ⇒ Object
find the parent and children of the ‘target_doc’.
- #get_all_lineage_ids(target_node_id, node = @root, ancestors = [], descendents = [], found = false) ⇒ Object
- #get_sibling_ids(target_node_id, node = @root, parent = nil) ⇒ Object
-
#initialize(root_doc, md_docs) ⇒ Tree
constructor
A new instance of Tree.
-
#print_tree(node, ancestors = []) ⇒ Object
…for debugging.
Constructor Details
#initialize(root_doc, md_docs) ⇒ Tree
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jekyll-namespaces/tree.rb', line 9 def initialize(root_doc, md_docs) @root = Node.new('root', root_doc.data['id'], root_doc.data['title'], root_doc.url, root_doc) md_docs.each do |doc| if doc != @root.doc # jekyll pages don't have the slug attribute: https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb#L8 if doc.type == :pages page_basename = File.basename(doc.name, File.extname(doc.name)) doc.data['slug'] = Jekyll::Utils.slugify(page_basename) end self.add_path(doc) end end # print_tree(root) end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/jekyll-namespaces/tree.rb', line 7 def root @root end |
Instance Method Details
#add_path(doc, node = nil, depth = 1) ⇒ Object
add unique path for the given doc to tree (node-class).
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll-namespaces/tree.rb', line 27 def add_path(doc, node=nil, depth=1) node = @root if depth == 1 Jekyll.logger.error("Jekyll-Namespaces: Incorrect node in tree.add_path") if node == nil levels = doc.data['slug'].split(/\s|\./) # handle doc if the given node was not root and we are at depth if depth == levels.length cur_nd_namespace = 'root' + '.' + doc.data['slug'] cur_nd_id = doc.data['id'] cur_nd_title = doc.data['title'] cur_nd_url = doc.url cur_node = node.children.detect {|c| c.namespace == cur_nd_namespace } # create node if one does not exist if cur_node.nil? new_node = Node.new(cur_nd_namespace, cur_nd_id, cur_nd_title, cur_nd_url, doc) node.children << new_node # fill-in node if one already exists else cur_node.fill(cur_nd_id, cur_nd_title, cur_nd_url, doc) end return # create temp node and recurse else cur_namespace = 'root' + '.' + levels[0..(depth - 1)].join('.') unless node.children.any? { |c| c.namespace == cur_namespace } new_node = Node.new(cur_namespace) node.children << new_node else new_node = node.children.detect { |c| c.namespace == cur_namespace } end end self.add_path(doc, new_node, depth + 1) end |
#find_doc_ancestors_and_children_metadata(target_doc, node = nil, ancestors = []) ⇒ Object
find the parent and children of the ‘target_doc’. (‘node’ as in the current node, which first is root.)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/jekyll-namespaces/tree.rb', line 106 def (target_doc, node=nil, ancestors=[]) node = @root if ancestors == [] Jekyll.logger.error("Jekyll-Namespaces: Incorrect node in tree.find_doc_immediate_lineages") if node == nil if target_doc == node.doc children = [] node.children.each do |child| # if the child document is an empty string, it is a missing node if child.missing children << child.namespace else children << child.doc.url end end return ancestors, children else # if the node document is an empty string, it is a missing node if node.missing ancestors << node.namespace else ancestors << node.doc.url end results = [] node.children.each do |child_node| results.concat(self.(target_doc, child_node, ancestors.clone)) end return results.select { |r| !r.nil? } end end |
#get_all_lineage_ids(target_node_id, node = @root, ancestors = [], descendents = [], found = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-namespaces/tree.rb', line 61 def get_all_lineage_ids(target_node_id, node=@root, ancestors=[], descendents=[], found=false) # found target node, stop adding ancestors and build descendents if target_node_id == node.url || target_node_id == node.namespace || found node.children.each do |child_node| # if the child document is an empty string, it is a missing node if child_node.missing descendents << child_node.namespace else descendents << child_node.doc.url end self.get_all_lineage_ids(target_node_id, child_node, ancestors.clone, descendents, found=true) end return ancestors, descendents # target node not yet found, build ancestors else # if the node document is an empty string, it is a missing node if node.missing ancestors << node.namespace else ancestors << node.doc.url end results = [] node.children.each do |child_node| results.concat(self.get_all_lineage_ids(target_node_id, child_node, ancestors.clone)) end return results.select { |r| !r.nil? } end end |
#get_sibling_ids(target_node_id, node = @root, parent = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jekyll-namespaces/tree.rb', line 91 def get_sibling_ids(target_node_id, node=@root, parent=nil) return [] if target_node_id === @root.url # found target node if target_node_id == node.url || target_node_id == node.namespace return parent.children.select { |c| c.id } # target node not yet found else node.children.each do |child_node| self.get_sibling_ids(target_node_id, child_node, node) end end end |
#print_tree(node, ancestors = []) ⇒ Object
…for debugging
136 137 138 139 140 141 142 143 |
# File 'lib/jekyll-namespaces/tree.rb', line 136 def print_tree(node, ancestors=[]) Jekyll.logger.warn("Jekyll-Namespaces: ") Jekyll.logger.warn("Ancestors: #{ancestors.length}; #{node}; Children: #{node.children}") ancestors.append(node.id) node.children.each do |child_node| self.print_tree(child_node, ancestors.clone) end end |