tuning, tuning, tuning the spaces...
[web.git] / _plugins / menu.rb
index 98614b76616cca1f285c00df29ab1666a958a5b6..48befa744554a401219d35667b016724eae50881 100644 (file)
@@ -1,41 +1,50 @@
 module Jekyll
 module Jekyll
-    module MenuFilter
-        def menu(input, displayurl)
-            def create_menu(pages, base, displayurl)
-                result = Array.new
-                for page in pages
-                    cururl = page['url'].sub('/index.html', '')
-                    if cururl.start_with?(base) and cururl.count('/') == base.count('/')
-                        # figure out CSS class
-                        if cururl == displayurl
-                            css_class = 'current'
-                        elsif cururl.start_with?(displayurl)
-                            css_class = 'child'
-                        elsif displayurl.start_with?(cururl)
-                            css_class = 'parent'
-                        else
-                            css_class = 'sibling'
-                        end
-                        # create menu node
-                        menu_node = { 'url' => page['url'], 'title' => page['slug'] ? page['slug'] : (page['title'] ? page['title'] : page['url']), 'class' => css_class, 'sort' => page['sort'] }
-                        # potentially recurse
-                        if (css_class == 'parent' or css_class == 'current')
-                            sub_nodes = create_menu(pages, cururl + "/", displayurl)
-                            if sub_nodes.size > 0
-                                menu_node['sub'] = sub_nodes
-                            end
-                        end
-                        # store menu node
-                        result.push(menu_node)
-                    end
-                end
-                # sort the result before returning it
-                result.sort_by { |a| a['url'] }
+  module MenuFilter
+    def menu(input, displayurl)
+      def create_menu(pages, base, displayurl)
+        result = Array.new
+        for page in pages
+          cururl = page['url'].sub('/index.html', '')
+          if cururl.start_with?(base) and cururl.count('/') == base.count('/') and (not page['hide'])
+            # figure out CSS class
+            if cururl == displayurl
+              css_class = 'current'
+            elsif cururl.start_with?(displayurl)
+              css_class = 'child'
+            elsif displayurl.start_with?(cururl)
+              css_class = 'parent'
+            else
+              css_class = 'sibling'
             end
             end
-            
-            create_menu(input, "", displayurl.sub('/index.html', ''))
+            # create menu node
+            menu_node = { 'url' => page['url'], 'title' => page['slug'] ? page['slug'] : (page['title'] ? page['title'] : page['url']), 'class' => css_class, 'sort' => page['sort'] ? page['sort'] : 0 }
+            # potentially recurse
+            if (css_class == 'parent' or css_class == 'current')
+              sub_nodes = create_menu(pages, cururl + "/", displayurl)
+              if sub_nodes.size > 0
+                menu_node['sub'] = sub_nodes
+              end
+              if css_class == 'parent' and sub_nodes.all? { |page| page['class'] == 'sibling' }
+                # we won't get closer to this URL, it doesn't exist in our page list
+                menu_node['class'] = 'current'
+                sub_nodes.each { |page| page['class'] = 'child' }
+              end
+            end
+            # remember the root
+            if base == ""
+              menu_node['class'] += ' root'
+            end
+            # store menu node
+            result.push(menu_node)
+          end
         end
         end
+        # sort the result before returning it
+        result.sort_by { |page| [page['sort'], page['url']] }
+      end
+      
+      create_menu(input, "", displayurl.sub('/index.html', ''))
     end
     end
+  end
 end
 
 Liquid::Template.register_filter(Jekyll::MenuFilter)
 end
 
 Liquid::Template.register_filter(Jekyll::MenuFilter)