start honoring baseurl again; crazy hackery to get page-dependent computed (pre)titles
[web.git] / personal / _plugins / menu.rb
1 module Jekyll
2   module MenuFilter
3     def menu(input, displayurl)
4       def create_menu(pages, base, displayurl)
5         result = Array.new
6         for page in pages
7           cururl = page['url'].sub('/index.html', '')
8           if cururl.start_with?(base) and cururl.count('/') == base.count('/') and (not page['hide'])
9             # figure out CSS class
10             if cururl == displayurl
11               css_class = 'current'
12             elsif cururl.start_with?(displayurl)
13               css_class = 'child'
14             elsif displayurl.start_with?(cururl)
15               css_class = 'parent'
16             else
17               css_class = 'sibling'
18             end
19             # create menu node
20             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 }
21             # potentially recurse
22             if (css_class == 'parent' or css_class == 'current')
23               sub_nodes = create_menu(pages, cururl + "/", displayurl)
24               if sub_nodes.size > 0
25                 menu_node['sub'] = sub_nodes
26               end
27               if css_class == 'parent' and sub_nodes.all? { |page| page['class'] == 'sibling' }
28                 # we won't get closer to this URL, it doesn't exist in our page list
29                 menu_node['class'] = 'current'
30                 sub_nodes.each { |page| page['class'] = 'child' }
31               end
32             end
33             # remember the root
34             if base == ""
35               menu_node['class'] += ' root'
36             end
37             # store menu node
38             result.push(menu_node)
39           end
40         end
41         # sort the result before returning it
42         result.sort_by { |page| [page['sort'], page['url']] }
43       end
44       
45       create_menu(input, "", displayurl.sub('/index.html', ''))
46     end
47   end
48 end
49
50 Liquid::Template.register_filter(Jekyll::MenuFilter)