317db0c102160a7453466de632cf96e30f08bb9b
[web.git] / _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('/')
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['title'], 'class' => css_class }
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                         end
28                         # store menu node
29                         result.push(menu_node)
30                     end
31                 end
32                 result
33             end
34             
35             create_menu(input, "", displayurl.sub('/index.html', ''))
36         end
37     end
38 end
39
40 Liquid::Template.register_filter(Jekyll::MenuFilter)