/
app.rb
243 lines (186 loc) · 5.99 KB
/
app.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
ROOT_PATH = File.expand_path(File.dirname(__FILE__))
require "cuba"
require "haml"
require "rdiscount"
require "json"
require "compass"
require "open-uri"
require "digest/md5"
require "redis"
require "ohm"
require "rack/static"
require "nokogiri"
require File.expand_path("lib/reference", ROOT_PATH)
require File.expand_path("lib/template", ROOT_PATH)
require File.expand_path("lib/interactive/namespace", ROOT_PATH)
require File.expand_path("lib/interactive/session", ROOT_PATH)
Encoding.default_external = Encoding::UTF_8
module Kernel
private
def documentation_path
@documentation_path ||= File.expand_path(ENV["REDIS_DOC"] || "redis-doc")
end
def commands
@commands ||= Reference.new(JSON.parse(File.read(documentation_path + "/commands.json")))
end
def new_redis_connection
Redis.connect(url: ENV["REDISTOGO_URL"])
end
def redis
@redis ||= new_redis_connection
end
def redis_versions
@redis_versions ||= redis.hgetall("versions")
end
def user
@user ||= User[session[:user]]
end
def related_topics_for(command)
# For now this is a quick and dirty way of figuring out related topics.
path = "#{documentation_path}/topics/#{command.group}.md"
return [] unless File.exist?(path)
_, title = topic(path)
[[title, "/topics/#{command.group}"]]
end
def related_commands_for(group)
commands.select do |command|
command.group == group
end.sort_by(&:name)
end
def update_redis_versions
tags = `git ls-remote -t https://github.com/antirez/redis.git`
versions = tags.scan(%r{refs/tags/(v?(?:\d\.?)*\-(?:stable|rc\w+|alpha\w+))}).flatten.uniq
stable, development = versions.partition { |v| v =~ /^v/ }
redis.hmset(
"versions",
"stable", stable.sort.last,
"development", development.sort.last
)
end
def clean_version(version)
version[/((?:\d\.?)+)/, 1]
end
def version_name(tag)
tag[/v?(.*)/, 1].sub(/\-stable$/, "")
end
end
Ohm.redis = redis
Cuba.use Rack::Static, urls: ["/images"], root: File.join(ROOT_PATH, "public")
Cuba.define do
def render(path, locals = {})
expanded = File.expand_path(path)
if expanded.start_with?(documentation_path)
data = super(path, locals)
filter_interactive_examples(data)
elsif expanded.start_with?(ROOT_PATH)
super(path, locals)
end
end
# Setup a new interactive session for every <pre><code> with @cli
def filter_interactive_examples(data)
session = create_interactive_session
data.gsub %r{<pre><code>(.*?)</code></pre>}m do |match|
lines = $1.split(/\n+/m).map(&:strip)
if lines.shift == "@cli"
render("views/interactive.haml", session: session, lines: lines)
else
match
end
end
end
def create_interactive_session
namespace = Digest::MD5.hexdigest([rand(2**32), Time.now.usec, Process.pid].join("-"))
::Interactive::Session.create(namespace)
end
def haml(template, locals = {})
layout(partial(template, locals))
end
def partial(template, locals = {})
render("views/#{template}.haml", locals)
end
def layout(content)
partial("layout", content: content)
end
def topic(template)
body = render(template)
title = body[%r{<h1>(.+?)</h1>}, 1] # Nokogiri may be overkill
return body, title
end
def gravatar_hash(email)
Digest::MD5.hexdigest(email)
end
def not_found(locals = {path: nil})
res.status = 404
res.write haml("404", locals)
end
on get, path("") do
res.write haml("home")
end
on get, path("buzz") do
res.write haml("buzz")
end
on get, path("tutorial") do
session = create_interactive_session
res.write haml("tutorial", session: session)
end
%w[download community documentation].each do |topic|
on get, path(topic) do
@body, @title = topic("views/#{topic}.md")
res.write haml("topics/name")
end
end
on get, path("commands") do
on segment do |name|
@name = @title = name.upcase.gsub("-", " ")
@command = commands[@name]
break not_found unless @command
@related_commands = related_commands_for(@command.group)
@related_topics = related_topics_for(@command)
res.write haml("commands/name")
end
on default do
@commands = commands
@title = "Command reference"
res.write haml("commands")
end
end
on post, path("session"), path(/[0-9a-f]{32}/i) do |_, _, id|
if session = ::Interactive::Session.find(id)
res.write session.run(req.params["command"].to_s)
else
res.status = 404
res.write "ERR Session does not exist or has timed out."
end
end
on get, path("clients") do
@clients = JSON.parse(File.read(documentation_path + "/clients.json"))
@clients_by_language = @clients.group_by { |info| info["language"] }.sort_by { |name, _| name.downcase }
res.write haml("clients")
end
on get, path("topics"), segment do |_, _, name|
path = "/topics/#{name}.md"
break not_found(path: path) unless File.exist?(File.join(documentation_path, path))
@css = [:topics, name]
@body, @title = topic(File.join(documentation_path, path))
@related_commands = related_commands_for(name)
res.write haml("topics/name")
end
on get, path(/\w+\.json/) do |_, file|
res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
res.headers["Content-Type"] = "application/json;charset=UTF-8"
res.write File.read(documentation_path + "/#{file}")
end
on get, path("styles.css") do
res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
res.headers["Content-Type"] = "text/css; charset=utf-8"
res.write render("views/styles.sass")
end
on get, path("app.js") do |_, file|
res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
res.headers["Content-Type"] = "text/javascript; charset=utf-8"
res.write File.read("views/app.js")
end
on post, path("commits"), param("payload") do
update_redis_versions
end
end