Class: JekyllThemeGuidesMbland::GeneratedNodes

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-theme-guides-mbland/generated_nodes.rb

Class Method Summary collapse

Class Method Details

.assign_orphan_to_home(nav, immediate_parent, url_to_nav) ⇒ Object


50
51
52
53
54
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 50

def self.assign_orphan_to_home(nav, immediate_parent, url_to_nav)
  nav_copy = {}.merge(nav)
  url_to_nav[nav_copy.delete(:orphan_url)] = nav_copy
  (immediate_parent['children'] ||= []) << nav_copy
end

.create_home_for_orphan(nav, nav_data, url_to_nav) ⇒ Object


20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 20

def self.create_home_for_orphan(nav, nav_data, url_to_nav)
  parents = nav[:orphan_url].split('/')[1..-1]
  nav['url'] = parents.pop + '/'
  child_url = '/'
  immediate_parent = parents.reduce(nil) do |parent, child|
    child_url = child_url + child + '/'
    find_or_create_node(nav_data, child_url, parent, child, url_to_nav)
  end
  assign_orphan_to_home(nav, immediate_parent, url_to_nav)
end

.create_homes_for_orphans(url_to_nav, nav_data) ⇒ Object

Params:

url_to_nav: Mapping from original document URL to "nav item" objects,
  i.e. { 'text' => '...', 'url' => '...', 'internal' => true }
nav_data: Array of nav item objects contained in `url_to_nav` after
  applying updates, possibly containing "orphan" items marked with an
  `:orphan_url` property

Returns:

nav_data with orphans properly nested within automatically-generated
  parent nodes marked with `'generated' => true`

13
14
15
16
17
18
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 13

def self.create_homes_for_orphans(url_to_nav, nav_data)
  orphans = nav_data.select { |nav| nav[:orphan_url] }
  orphans.each { |nav| create_home_for_orphan(nav, nav_data, url_to_nav) }
  nav_data.reject! { |nav| nav[:orphan_url] }
  prune_childless_parents(nav_data)
end

.find_or_create_node(nav_data, child_url, parent, child, url_to_nav) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 31

def self.find_or_create_node(nav_data, child_url, parent, child, url_to_nav)
  child_nav = url_to_nav[child_url]
  if child_nav.nil?
    child_nav = generated_node(child)
    url_to_nav[child_url] = child_nav
    (parent.nil? ? nav_data : (parent['children'] ||= [])) << child_nav
  end
  child_nav
end

.generated_node(parent_slug) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 41

def self.generated_node(parent_slug)
  {
    'text' => parent_slug.split('-').join(' ').capitalize,
    'url' => parent_slug + '/',
    'internal' => true,
    'generated' => true,
  }
end

.prune_childless_parents(nav_data) ⇒ Object


56
57
58
59
60
61
62
# File 'lib/jekyll-theme-guides-mbland/generated_nodes.rb', line 56

def self.prune_childless_parents(nav_data)
  (nav_data || []).reject! do |nav|
    children = (nav['children'] || [])
    prune_childless_parents(children)
    nav['generated'] && children.empty?
  end
end