Module: Middleman::Features::Slickmap

Defined in:
lib/middleman-slickmap.rb

Class Method Summary collapse

Class Method Details

.build_sitemap(&block) ⇒ Object



66
67
68
69
# File 'lib/middleman-slickmap.rb', line 66

def self.build_sitemap(&block)    
  @@utility = []
  [recurse_sitemap(Middleman::Server.views, &block), @@utility]
end

.recurse_sitemap(path, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/middleman-slickmap.rb', line 71

def self.recurse_sitemap(path, &block)
  bad_ext = path.split('.html')[1]
  path = path.gsub(bad_ext, '') if bad_ext
  entry = Entry.new(path, [])

  #no "." or ".." dirs
  Dir[File.join(path, "*")].each do |e|
    next if !File.directory?(e) && !e.include?(".html")
    if File.directory?(e)
      entry.children << recurse_sitemap(e, &block)
    elsif block_given?
      how_to_handle = block.call(e)
      if how_to_handle == :valid
        entry.children << recurse_sitemap(e, &block)
      elsif how_to_handle == :utility
        bad_ext = e.split('.html')[1]
        e = e.gsub(bad_ext, '') if bad_ext
        @@utility << e.gsub(Middleman::Server.views + "/", '')
      end
    end
  end

  entry
end

.registered(app) ⇒ Object Also known as: included



11
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/middleman-slickmap.rb', line 11

def registered(app)
  sitemap_url = "sitemap.html"
  sitemap_url = app.sitemap_url if app.respond_to?(:slickmap_url) && !app.sitemap_url.nil?

  Middleman::Builder.after_run "smush_pngs" do
    source_paths << File.expand_path(File.join(File.dirname(__FILE__), "middleman-slickmap", "templates"))
    tilt_template "slickmap.html.haml", File.join(Middleman::Server.build_dir, sitemap_url), { :force => true }
  end

  app.helpers do
    def sitemap_node(n, first=false)
      if n.children.length < 1
        if !first && File.extname(n.dir).length > 0
          haml_tag :li do
            path = n.dir.gsub(self.class.views, '')
            haml_concat link_to(File.basename(path), path)
          end
        end
      else  
        haml_tag(:li, :id => first ? "home" : nil) do
          if first
            haml_concat link_to("Homepage", "/" + self.class.index_file)
          else
            # we are a dir
            index = n.children.find { |c| c.dir.include?(self.class.index_file) }
            haml_concat link_to(index.dir.gsub(self.class.views + "/", '').gsub("/" + File.basename(index.dir), '').capitalize, index.dir.gsub(self.class.views, ''))
          end

          other_children = n.children.select { |c| !c.dir.include?(self.class.index_file) }
          if other_children.length > 0
            if first
              other_children.each { |i| sitemap_node(i) }
            else
              haml_tag :ul do
                other_children.each { |i| sitemap_node(i) }
              end
            end
          end
        end  
      end
    end
  end

  app.get "/#{sitemap_url}" do
    # Return :utility to put it util top menu. False to ignore
    @tree, @utility = Middleman::Features::Slickmap.build_sitemap do |file_name|
      :valid
    end

    haml "slickmap.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "middleman-slickmap", "templates"))
  end
end