forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadcrumbs.rb
69 lines (58 loc) · 2.52 KB
/
breadcrumbs.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
require 'open-uri'
module Jekyll
# Add accessor for directory
class Page
attr_reader :dir
end
class BreadCrumbs < Liquid::Tag
def render(context)
site = context.registers[:site]
page = context.registers[:page]
@page_url = context.environments.first["page"]["url"]
@dirs = {}
site.pages.each do |site_page|
path = site_page.url
path = path.index('/')==0 ? path[1..-1] : path
@dirs[path] = site_page.data
end
output='<div class="breadcrumb-pane clearfix"><ul class="breadcrumb">'
output+='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/index.html" itemprop="url"><span itemprop="title">Documentation</span></a></li>'
levels = @page_url.split('/') #break up url into different levels
levels.each_with_index do |level, index|
unless level.empty?
if index == levels.size-1 ||
(level == levels[levels.size-2] && levels[levels.size-1].to_i > 0)
path = @page_url[1..-1]
if @dirs[path]["navigation"]["show"] == true
path = @dirs[path]["title"] || path
output += "<li itemscope itemtype=\"http://data-vocabulary.org/Breadcrumb\"><span itemprop=\"title\">#{path}</span><meta itemprop=\"url\" content=\"#{site.config['url']}#{@page_url}\" /></li>" unless level.to_i > 0
end
else
link = "/"
i = 1
while i <= index
link += "#{URI::encode levels[i]}/"
i+=1
end
if !link.index('.')
path = Pathname.new(site.source + link + 'index.html')
if path.exist?
link = link + 'index.html'
else
link = ""
end
end
inner = link.length > 0 ? "<a href=\"#{link}\" itemprop=\"url\"><span itemprop=\"title\">#{level.gsub(/_/, ' ')}</span></a>" : "#{level.gsub(/_/, ' ')}"
output += "<li itemscope itemtype=\"http://data-vocabulary.org/Breadcrumb\">#{inner}</li>"
end
end
end
output += "</ul>"
output += "<a class=\"edit-link\" href=\"https://github.com/sendgrid/docs/blob/develop/source/#{page['path']}\"><span class=\"icon-edit\"></span> Edit</a>"
output += "</div>"
puts "generating breadcrumbs for #{@page_url}"
output
end
end
end
Liquid::Template.register_tag("breadcrumbs", Jekyll::BreadCrumbs)