Class: Middleman::Navigation::Tree

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

Class Method Summary collapse

Class Method Details

.build(sitemap) ⇒ Object



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

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

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

.traverse(resource, navigation) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/middleman-navigation/tree.rb', line 16

def self.traverse(resource, navigation)
  children = visible_children(resource)

  unless children.blank?
    navigation.items do |level|
      children.each do |child|
        title = child.data.navigation[:title] || child.data.title
        url = child.data.navigation[:destination] || child.url

        level.item child.destination_path, title, url, :highlights_on => %r(#{url}(#{@app.index_file})?)
        traverse child, level
      end
    end
  end
end

.visible_children(resource) ⇒ Object



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

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