forked from ahti/jekyll-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_generator.rb
214 lines (169 loc) · 6.54 KB
/
menu_generator.rb
1
2
3
4
5
6
7
8
9
10
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
module Jekyll
class Site
attr_accessor :menu
end
class Page
def menu
self.data['menu'] ||= {}
end
def menu_parent
menu['parent']
end
def menu_name
menu['name'] ||= self.data['title']
end
def subpages
menu['subpages'] ||= []
end
end
class MenuGenerator < Generator
safe true
def menu_name(hash)
hash['menu']['name'] || hash['title']
end
def setup_config(site)
site.config['menu_generator'] ||= {}
site.config['menu_generator']['parent_match_hash'] ||= 'path'
site.config['menu_generator']['menu_root'] ||= '__root'
site.config['menu_generator']['delete_content_hash'] ||= false
site.config['menu_generator']['hash_name_in_site_config'] ||= "menu"
site.config['menu_generator']['css'] ||= {}
site.config['menu_generator']['css']['current'] ||= 'current'
site.config['menu_generator']['css']['current_parent'] ||= 'current-parent'
site.config['menu_generator']['css']['li'] ||= ''
site.config['menu_generator']['css']['ul'] ||= ''
@parent_match_hash = site.config['menu_generator']['parent_match_hash']
@menu_root = site.config['menu_generator']['menu_root']
@delete_content_hash = site.config['menu_generator']['delete_content_hash']
@hash_name_in_site_config = site.config['menu_generator']['hash_name_in_site_config']
end
def generate(site)
@pages = site.pages.dup
setup_config(site)
@main_menu = []
@lookup = { @menu_root => @main_menu }
build_tree
sort_pages
generate_suburls
site.config[@hash_name_in_site_config] = @main_menu
site.menu = @main_menu
end
def build_tree
# build the subpage tree
loop do
prev_size = @pages.size
@pages.reject! do |page|
parent = @lookup[page.menu_parent]
unless parent.nil?
# Initilize the name
page.menu_name
@lookup[page[@parent_match_hash]] = page.subpages
liq_hash = page.to_liquid
if @delete_content_hash
liq_hash.delete('content')
end
parent << liq_hash
true
else
false
end
end
break if @pages.size == prev_size
end
end
def sort_pages
# lookup contains every page's subpage array,
# so we only need to sort every value in lookup
@lookup.each do |key, val|
val.sort! do |a, b|
compare_pages(a, b)
end
end
end
def compare_pages(a, b)
if a['menu']['position'].nil? and b['menu']['position'].nil? or a['menu']['position'] == b['menu']['position']
# neither has a position, sort by name
menu_name(a) <=> menu_name(b)
elsif a['menu']['position'].nil?
# if a has no position, it goes after b
+1
elsif b['menu']['position'].nil?
# if b has no position, it goes after a
-1
else
# both have a position, compare them
a['menu']['position'] <=> b['menu']['position']
end
end
def generate_suburls
# generate the suburl lists.
# #set_suburls recurses, so we only call this
# on the pages in the main menu
@main_menu.each do |page|
set_suburls(page)
end
end
def set_suburls(page)
page['menu']['subpages'].each do |subpage|
set_suburls(subpage)
end
page['menu']['suburls'] = suburls(page)
end
def suburls(page_hash, add_self=false)
subsub = page_hash['menu']['subpages'].map do |subpage|
suburls(subpage, true)
end
subsub.flatten!
if add_self
subsub << page_hash['url']
else
subsub
end
end
end
class MenuGeneratorTag < Liquid::Tag
Syntax = /^\s*(max_depth:[0-9]+)?\s*$/
def initialize(tag_name, markup, tokens)
@attributes = {}
# Parse parameters
if markup =~ Syntax
markup.scan(Liquid::TagAttributes) do |key, value|
#p key + ":" + value
@attributes[key] = value
end
else
raise SyntaxError.new("Syntax Error in 'MenuGenerator' - Valid syntax: menu [max_depth:y]")
end
@max_depth = @attributes['max_depth'].nil? ? -1 : @attributes['max_depth'].to_i()
super
end
def render(context)
site = context.registers[:site]
page = context.registers[:page]
@css_class_current = site.config['menu_generator']['css']['current']
@css_class_current_parent = site.config['menu_generator']['css']['current_parent']
render_menu(site.menu, site, page)
end
def render_menu(menu, site, page, level=0)
output = "<ul class=\"menu-level-#{level} " + site.config['menu_generator']['css']['ul'] + "\">"
menu.each do |menu_page|
css_class = ""
if page['url'] == menu_page['url']
css_class = " class=\"" + site.config['menu_generator']['css']['li'] + ' ' + @css_class_current + "\""
elsif menu_page['menu']['suburls'].include? page['url']
css_class = " class=\"" + site.config['menu_generator']['css']['li'] + ' ' + @css_class_current_parent + "\""
end
title = menu_page['menu']['name']
url = menu_page['url']
output += "<li#{css_class}><a href=\"#{url}\">#{title}</a>"
unless menu_page['menu']['subpages'].count == 0 or level - @max_depth == 0
output += render_menu(menu_page['menu']['subpages'], site, page, level + 1)
end
output += "</li>"
end
output += "</ul>"
output
end
end
end
Liquid::Template.register_tag('menu', Jekyll::MenuGeneratorTag)