-
Notifications
You must be signed in to change notification settings - Fork 1
/
docs.rb
114 lines (96 loc) · 2.46 KB
/
docs.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
require 'rubygems'
require 'sinatra'
require 'rdiscount'
set :app_file, __FILE__
get '/README' do
render_topic './README'
end
get '/' do
cache_long
render_topic 'index'
end
get '/:topic' do
cache_long
render_topic params[:topic]
end
get '/css/docs.css' do
cache_long
content_type 'text/css'
erb :css, :layout => false
end
before do
@asset_host = ENV['ASSET_HOST']
end
helpers do
def render_topic(topic)
source = File.read(topic_file(topic))
@content = markdown(source)
@title, @content = title(@content)
@toc, @content = toc(@content)
@topic = topic
erb :topic
end
def cache_long
response['Cache-Control'] = "public, max-age=#{60 * 60}" unless development?
end
def notes(source)
source.gsub(/NOTE: (.*)/, '<table class="note"><td class="icon"></td><td class="content">\\1</td></table>')
end
def markdown(source)
RDiscount.new(notes(source), :smart).to_html
end
def topic_file(topic)
if topic.include?('/')
topic
else
"#{options.root}/docs/#{topic}.txt"
end
end
def title(content)
title = content.match(/<h1>(.*)<\/h1>/)[1]
content_minus_title = content.gsub(/<h1>.*<\/h1>/, '')
return title, content_minus_title
end
def slugify(title)
title.downcase.gsub(/[^a-z0-9 -]/, '').gsub(/ /, '-')
end
def toc(content)
toc = content.scan(/<h2>([^<]+)<\/h2>/m).to_a.map { |m| m.first }
content_with_anchors = content.gsub(/(<h2>[^<]+<\/h2>)/m) do |m|
"<a name=\"#{slugify(m.gsub(/<[^>]+>/, ''))}\"></a>#{m}"
end
return toc, content_with_anchors
end
def sections
[
[ 'quickstart', 'Quickstart' ],
[ 'heroku-command', 'Heroku command-line tool' ],
[ 'git', 'Using Git' ],
[ 'sharing', 'Sharing' ],
[ 'console-rake', 'Console and rake' ],
[ 'rack', 'Deploying Rack-based apps' ],
[ 'logs-exceptions', 'Logs and exceptions' ],
[ 'errors', 'Errors' ],
[ 'custom-domains', 'Custom domain names' ],
[ 'gems', 'Installing gems' ],
[ 'taps', 'Database import/export' ],
[ 'renaming-apps', 'Renaming apps' ],
[ 'cron', 'Cron jobs' ],
[ 'config-vars', 'Config vars' ],
[ 'http-caching', 'HTTP caching' ],
[ 'full-text-indexing', 'Full text indexing' ],
[ 'constraints', 'Constraints' ],
[ 'technologies', 'Technologies' ],
]
end
def next_section(current_slug)
return sections.first if current_slug.nil?
sections.each_with_index do |(slug, title), i|
if current_slug == slug and i < sections.length-1
return sections[i+1]
end
end
nil
end
alias_method :h, :escape_html
end