3 def menu(input, displayurl)
4 def create_menu(pages, base, displayurl)
5 # URLs in `base`, `displayurl`, `cururl` are normalized to remove trailing slashes. This is to be able to distinguish the root of a folder from its children by the number of slashes in the URL.
6 # We need this to treat the root of a folder as a direct child of the parent folder, whereas pages within a subfolder are of course grandchildren.
9 cururl = page['url'].chomp('/')
10 # Test if this page is a non-hidden *direct* child of `base`
11 if cururl.start_with?(base) and cururl.count('/') == base.count('/') and (not page['hide'])
12 # figure out CSS class
13 if cururl == displayurl
15 elsif cururl.start_with?(displayurl)
17 elsif displayurl.start_with?(cururl)
23 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 }
25 if (css_class == 'parent' or css_class == 'current')
26 sub_nodes = create_menu(pages, cururl + "/", displayurl)
28 menu_node['sub'] = sub_nodes
30 if css_class == 'parent' and sub_nodes.all? { |page| page['class'] == 'sibling' }
31 # we won't get closer to this URL, it doesn't exist in our page list
32 menu_node['class'] = 'current'
33 sub_nodes.each { |page| page['class'] = 'child' }
38 menu_node['class'] += ' root'
41 result.push(menu_node)
43 #print "Not adding: cur=", cururl, ", base=", base, ", hide=", page['hide'], "\n"
46 # sort the result before returning it
47 result.sort_by { |page| [page['sort'], page['url']] }
50 create_menu(input, "", displayurl.chomp('/'))
55 Liquid::Template.register_filter(Jekyll::MenuFilter)