forked from cloudfoundry-attic/cf-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicklinks.rb
40 lines (29 loc) · 961 Bytes
/
quicklinks.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
module QuickLinks
require 'redcarpet'
class << self
def registered(app)
app.helpers HelperMethods
end
alias :included :registered
end
module HelperMethods
def quick_links()
links = []
page_src = File.read(current_page.source_file)
sections = page_src.scan /\n\#{2,3}[^#]+\#{2,3}\n/
markdown = ''
sections.each do |s|
next if s.match(/id=['"](.+)['"]/).nil? or s.match(/<\/a>([^#.]+)\#{2,3}/).nil?
anchor_name = s.match(/id=['"](.+)['"]/)[1]
title = s.match(/<\/a>([^#.]+)\#{2,3}/)[1].strip!
indent = (s.count('#') / 2) - 2
markdown << ' ' * indent
markdown << "* [#{title}](##{anchor_name})\n"
end
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
result = md.render(markdown)
"<div class=\"quick-links\">#{result}</div>"
end
end
end
::Middleman::Extensions.register(:quicklinks, QuickLinks)