sitenav.rb 8.03 KB
Newer Older
1
# Copyright (C) 2012-2015 Scott Zeid
2
# https://code.s.zeid.me/site-design
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 
# Except as contained in this notice, the name(s) of the above copyright holders
# shall not be used in advertising or otherwise to promote the sale, use or
# other dealings in this Software without prior written authorization.

S. Zeid's avatar
S. Zeid committed
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
module Jekyll
 class SiteNavigationLevelTag < Liquid::Tag
  include Liquid::StandardFilters
  
  def initialize(tag_name, url, tokens)
   super
   @url = url.strip
  end
  
  def render(context)
   tree = SiteNavigationTag.tree(context.registers[:site])
   self.class.page_level((@url=="") ? context["page"]["url"] : @url, tree)
  end
  
  def self.page_level(url, tree, level=0)
   tree.each do |i|
    if i["page"]["url"] == url
     return i["page"]["nav"]["level"].to_i if not i["page"]["nav"]["level"].nil?
     return level
    end
    children = self.page_level(url, i["children"], level + 1)
    return children if not children.nil?
   end
   if level > 0
    return nil
   else
    return SiteNavigationTag.to_slug(url).gsub(/^\/?(.*?)\/?$/, '\1').count("/")
   end
  end
 end
 
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 class SiteNavigationPageInfoTag < Liquid::Tag
  include Liquid::StandardFilters
  extend Jekyll::Filters
  
  def initialize(tag_name, url, tokens)
   super
   @url = url.strip
  end
  
  def render(context)
   url_map = SiteNavigationTag.url_map(context.registers[:site])
   self.class.page_info((@url=="") ? context["page"]["url"] : @url, url_map,
                        context)
  end
  
  def self.page_info(url, url_map, ctx)
   if url_map.include?(url)
74
    info = nil
75
76
    p = url_map[url]
    if not p["page_basic"].nil?
77
     info = p["page_basic"].call(ctx)
78
79
    end
   end
80
81
82
83
84
   info = Jekyll::Utils::deep_merge_hashes((info or {}), {
    "nav" => {
     "original" => {}
    }
   })
85
   return jsonify(info).gsub(/(^|[^\\])\//, "\\1\\/")
86
87
88
  end
 end
 
S. Zeid's avatar
S. Zeid committed
89
90
91
92
93
 class SiteNavigationTag < Liquid::Block
  include Liquid::StandardFilters
  
  @@tree = []
  @@child_list = {}
94
  @@url_map = {}
S. Zeid's avatar
S. Zeid committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  
  def initialize(tag_name, parent, tokens)
   super
   @parent = (not parent.empty?) ? parent.strip : ""
  end
  
  def render(context)
   parent = if /^"[^"]"|'[^']'$/.match(@parent)
    @parent[1..-2]
   elsif not context[@parent].nil?
    context[@parent]
   else
    @parent
   end
   parent = (not parent.empty?) ? self.class.to_slug(parent) : ""
110
   parent = (parent != "/") ? parent : ""
S. Zeid's avatar
S. Zeid committed
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
   generate_output(self.class.tree(context.registers[:site], parent), context, super)
  end
  
  def self.tree(site, parent="", node=nil)
   if node.nil?
    if not @@tree.empty?
     node = @@tree
    else
     @@tree = self.make_tree(site)
     node = @@tree
     #@@child_list.keys.each { |k| puts "#{k} (#{@@child_list[k].size})" }
    end
   end
   
   if parent == ""
    return node
   else
    return @@child_list[parent]
   end
  end
  
  def self.to_slug(url)
   slug = url.gsub(/\/index\.([^\/.]+)$/, "/")
   return (not slug.empty?) ? slug : "/"
  end
  
137
138
139
140
141
142
143
  def self.url_map(site)
   if @@tree.empty?
    @@tree = self.make_tree(site)
   end
   return @@url_map
  end
  
S. Zeid's avatar
S. Zeid committed
144
145
146
147
  private
  
  def generate_output(tree, context, template, level=0)
   return if tree == nil or tree.empty?
148
   pages = tree.collect {|i|
S. Zeid's avatar
S. Zeid committed
149
    nav = i["page"]["nav"]
150
151
    original_nav = nav["original"]
    next if original_nav["show"] == false or original_nav["hide"] == true
S. Zeid's avatar
S. Zeid committed
152
153
154
155
    i["page"].merge({ "nav" => nav.merge(nav) { |k, v, unused|
     if k == "children"
      generate_output(v, context, template, level + 1)
     else
156
      (v.is_a? Proc) ? v.call(context, @@url_map[context["page"]["url"]]) : v
S. Zeid's avatar
S. Zeid committed
157
158
     end
    }})
159
   }.delete_if {|p| p.nil?}
S. Zeid's avatar
S. Zeid committed
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
   output = ""
   context.stack do
    context["nav"] = {
     "level"  => level,
     "pages"  => pages
    }
    partial = Liquid::Template.parse(template)
    output = partial.render(context)
   end
   return output
  end
  
  def self.sort_key(page)
   if page["page"]["nav"]["original"]["sort-key"]
    return page["page"]["nav"]["original"]["sort-key"]
   else
    return page["slug"]
   end
  end
  
  def self.make_tree(site, parent="")
   previous = ""
   site.pages.collect {|p| p.to_liquid}.sort {|a,b|
    self.to_slug(a["url"]) <=> self.to_slug(b["url"])
   }.collect {|page|
    slug = self.to_slug(page["url"])
    slug_no_end_slash = (slug != "/") ? slug.gsub(/(\/)$/, "") : slug
    next if not page["layout"] and (not page["nav"] or not page["nav"]["show"])
    next if previous != "" and slug_no_end_slash.start_with? (previous + "/")
    next if not slug_no_end_slash.start_with?(parent + "/")
    previous = slug_no_end_slash
    original_nav = (page["nav"].nil?) ? {} : page["nav"]
192
193
194
195
196
197
198
    title = if not original_nav["title"].nil?
     original_nav["title"]
    elsif not page["title"].nil?
     page["title"]
    else
     slug
    end
S. Zeid's avatar
S. Zeid committed
199
    @@child_list[slug] = children = self.make_tree(site, slug_no_end_slash)
S. Zeid's avatar
S. Zeid committed
200
    page = Jekyll::Utils::deep_merge_hashes(page, {"nav" => {
201
202
     "current"  => lambda {|ctx, p|
      p = (p.nil?) ? {} : p
S. Zeid's avatar
S. Zeid committed
203
204
      cnav = (ctx["page"]["nav"].nil?) ? {} : ctx["page"]["nav"]
      (ctx["page"]["url"] == page["url"] or
205
206
       (([page["url"], slug].include? cnav["highlight"]) and
        cnav["highlight-as-current"]))
S. Zeid's avatar
S. Zeid committed
207
     },
208
209
210
211
212
213
214
215
216
217
218
219
     "parent"   => lambda{|ctx, p|
      p = (p.nil?) ? {} : p
      if p["page"] and p["page"]["nav"] and p["page"]["nav"]["original"] then
       p_original_nav = p["page"]["nav"]["original"]
      end
      if p_original_nav and p_original_nav["highlight"]
       nav_highlight = p_original_nav["highlight"]
       nav_highlight_no_end_slash = nav_highlight.gsub(/(\/)$/, "")
       (nav_highlight_no_end_slash+"/").start_with?(slug_no_end_slash+"/")
      else
       ctx["page"]["url"].start_with?(slug_no_end_slash+"/")
      end
S. Zeid's avatar
S. Zeid committed
220
221
     },
     "slug"     => slug,
222
     "title"    => title,
S. Zeid's avatar
S. Zeid committed
223
224
225
226
227
     "dir"      => File.dirname(page["url"]),
     "original" => original_nav,
     "children" => children,
     "has_children" => children.length > 0
    }})
228
229
230
231
232
233
    page["nav"]["has_children"] = children.collect {|p|
     if p["page"] and p["page"]["nav"] and p["page"]["nav"]["original"] then
      p_original_nav = p["page"]["nav"]["original"]
      next if p_original_nav["show"] == false or p_original_nav["hide"] == true
     end
     p
234
    }.delete_if {|p| p.nil?}.length > 0
235
236
237
238
239
    {
     "slug" => slug, "page" => page, "children" => children,
     "page_basic" => lambda {|ctx|
      page_basic = Jekyll::Utils::deep_merge_hashes(page, {})
      page_basic.delete("content")
240
      page_basic["nav"] = Jekyll::Utils::deep_merge_hashes(page_basic["nav"], {})
241
242
243
244
245
246
247
      pbnav = page_basic["nav"]
      pbnav.delete("children")
      page_basic = page_basic.merge({ "nav" => pbnav.merge(pbnav) { |k, v, unused|
       (v.is_a? Proc) ? v.call(ctx, @@url_map[ctx["page"]["url"]]) : v
      }})
     }
    }
S. Zeid's avatar
S. Zeid committed
248
249
   }.delete_if {|p| p.nil?}.sort {|a, b|
    self.sort_key(a) <=> self.sort_key(b)
250
251
   }.each {|p|
    @@url_map[p["page"]["url"]] = p
S. Zeid's avatar
S. Zeid committed
252
253
254
255
256
257
258
   }
  end
 end
end

Liquid::Template.register_tag("sitenav", Jekyll::SiteNavigationTag)
Liquid::Template.register_tag("sitenavlevel", Jekyll::SiteNavigationLevelTag)
259
Liquid::Template.register_tag("sitenavpageinfo", Jekyll::SiteNavigationPageInfoTag)