Class: Middleman::Navigation::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-navigation/tree.rb

Class Method Summary collapse

Class Method Details

.add_navigation_node(node, level) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/middleman-navigation/tree.rb', line 41

def self.add_navigation_node(node, level)
  options = with_defaults(node.data.navigation)
  title = options[:title] || node.data.title
  url = options[:destination] || node.url
  unless options[:hidden]
    level.item node.destination_path, title, url, :highlights_on => %r(^#{url}(#{@app.index_file})?$) do |subnav|
      yield(subnav) if block_given?
    end
  end
end

.build(sitemap) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/middleman-navigation/tree.rb', line 4

def self.build(sitemap)
  @app = sitemap.app
  @root = sitemap.find_resource_by_destination_path @app.http_prefix+@app.index_file

  unless @root.blank?
    SimpleNavigation::Configuration.run do |navigation|
      navigation.items do |level|
        traverse(@root, level)
      end
    end
  end
end

.traverse(resource, navigation) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/middleman-navigation/tree.rb', line 22

def self.traverse(resource, navigation)
  if resource == @root
    add_navigation_node resource, navigation
  end

  visible_children(resource).each do |child|
    add_navigation_node child, navigation do |subnav|
      traverse child, subnav
    end
  end
end

.visible_children(resource) ⇒ Object



34
35
36
37
38
39
# File 'lib/middleman-navigation/tree.rb', line 34

def self.visible_children(resource)
  visible = resource.children
  visible.select!{|child| !with_defaults(child.data.navigation)[:hidden]}
  visible.sort! {|a, b| with_defaults(a.data.navigation)[:weight] <=> with_defaults(b.data.navigation)[:weight]}
  visible
end

.with_defaults(navigation) ⇒ Object



17
18
19
20
# File 'lib/middleman-navigation/tree.rb', line 17

def self.with_defaults(navigation)
  navigation = {} if navigation.blank?
  Middleman::Navigation::FRONTMATTER_DEFAULTS.merge navigation
end