Module: NanocConrefFS::Ancestry

Included in:
ConrefFS
Defined in:
lib/nanoc-conref-fs/ancestry.rb

Class Method Summary collapse

Class Method Details

.create_children(toc, meta) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/nanoc-conref-fs/ancestry.rb', line 12

def create_children(toc, meta)
  if toc.is_a?(Array)
    find_array_children(toc, meta[:title])
  elsif toc.is_a?(Hash)
    find_hash_children(toc, meta[:title])
  end
end

.create_parents(toc, meta) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/nanoc-conref-fs/ancestry.rb', line 3

def create_parents(toc, meta)
  if toc.is_a?(Array)
    find_array_parents(toc, meta[:title])
  elsif toc.is_a?(Hash)
    find_hash_parents(toc, meta[:title])
  end
end

.find_array_children(toc, title) ⇒ Object

Given a category file that’s an array, this method finds the children of an item, probably a map topic

toc - the array containing the table of contents title - the text title to return the children of

Returns a flattened array of all descendants which could be empty.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nanoc-conref-fs/ancestry.rb', line 70

def find_array_children(toc, title)
  toc.each do |item|
    next unless item.is_a?(Hash)
    item.each_pair do |key, values|
      if key == title
        children = values.flatten
        return children
      end
    end
  end
  # Found no children
  Array.new
end

.find_array_parents(toc, title) ⇒ Object

Given a category file that’s an array, this method finds the parent of an item



23
24
25
26
27
28
29
30
31
32
# File 'lib/nanoc-conref-fs/ancestry.rb', line 23

def find_array_parents(toc, title)
  parents = ''
  toc.each do |item|
    if item.is_a?(Hash)
      parents = find_hash_parents(item, title)
      break unless parents.empty?
    end
  end
  parents
end

.find_hash_children(toc, title) ⇒ Object

Given a category file that’s a hash, this method finds the children of an item, probably a map topic

toc - the hash containing the table of contents title - the text title to return the children of

Returns a flattened array of all descendants which could be empty.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nanoc-conref-fs/ancestry.rb', line 92

def find_hash_children(toc, title)
  toc.each_key do |key|
    next if toc[key].nil?
    toc[key].each do |item|
      next unless item.is_a?(Hash)
      if item[title]
        children = item.values.flatten
        return children
      end
    end
  end
  # Found no children
  Array.new
end

.find_hash_parents(toc, title) ⇒ Object

Given a category file that’s a hash, this method finds the parent of an item



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nanoc-conref-fs/ancestry.rb', line 37

def find_hash_parents(toc, title)
  parents = ''
  toc.each_key do |key|
    next if toc[key].nil?
    toc[key].each do |item|
      if item.is_a?(Hash)
        if item.keys.include?(title)
          parents = key
          break
        else
          if item[item.keys.first].include?(title)
            parents = key
            break
          end
        end
      elsif title == item
        parents = key
        break
      end
    end
    break unless parents.empty?
  end
  parents
end