Class: Middleman::MetaPages::SitemapTree

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-core/meta_pages/sitemap_tree.rb

Overview

View class for a sitemap tree

Instance Method Summary collapse

Constructor Details

#initializeSitemapTree

Returns a new instance of SitemapTree.



7
8
9
# File 'lib/middleman-core/meta_pages/sitemap_tree.rb', line 7

def initialize
  @children = {}
end

Instance Method Details

#add_resource(resource) ⇒ Object



11
12
13
# File 'lib/middleman-core/meta_pages/sitemap_tree.rb', line 11

def add_resource(resource)
  add_path(resource.path.split('/'), resource)
end

#css_classesObject



47
48
49
# File 'lib/middleman-core/meta_pages/sitemap_tree.rb', line 47

def css_classes
  ['tree'].concat(ignored? ? ['ignored'] : [])
end

#ignored?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/middleman-core/meta_pages/sitemap_tree.rb', line 51

def ignored?
  @children.values.all?(&:ignored?)
end

#renderObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/middleman-core/meta_pages/sitemap_tree.rb', line 15

def render
  sorted_children_keys = @children.keys.sort do |a, b|
    a_subtree = @children[a]
    b_subtree = @children[b]
    if a_subtree.is_a? SitemapResource
      if b_subtree.is_a? SitemapResource
        a.downcase <=> b.downcase
      else
        1
      end
    elsif b_subtree.is_a? SitemapResource
      if a_subtree.is_a? SitemapResource
        b.downcase <=> a.downcase
      else
        -1
      end
    else
      a.downcase <=> b.downcase
    end
  end

  sorted_children_keys.reduce('') do |content, path_part|
    subtree = @children[path_part]
    content << "<details class='#{subtree.css_classes.join(' ')}'>"
    content << '<summary>'
    content << "<i class='icon-folder-open'></i>" unless subtree.is_a? SitemapResource
    content << "#{path_part}</summary>"
    content << subtree.render
    content << '</details>'
  end
end