Class: Middleman::MetaPages::SitemapTree

Inherits:
Object
  • Object
show all
Defined in:
middleman-core/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.


9
10
11
# File 'middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb', line 9

def initialize
  @children = {}
end

Instance Method Details

#add_resource(resource) ⇒ Object


13
14
15
# File 'middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb', line 13

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

#css_classesObject


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

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

#ignored?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb', line 55

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

#renderObject


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
46
47
48
49
# File 'middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb', line 17

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]
    additional_content = []
    additional_content << "<details class='#{subtree.css_classes.join(' ')}'>"
    additional_content << '<summary>'
    additional_content << "<i class='icon-folder-open'></i>" unless subtree.is_a? SitemapResource
    additional_content << "#{path_part}</summary>"
    additional_content << subtree.render
    additional_content << '</details>'
    "#{content}#{additional_content.join}"
  end
end