forked from railsbridge/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·330 lines (278 loc) · 7.81 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
require 'sinatra'
require 'sinatra/cookies'
require 'digest/md5'
require 'erector'
require 'i18n'
require 'i18n/backend/fallbacks'
require 'font-awesome-sass'
require 'bootstrap-sass'
require 'zip'
require 'tmpdir'
here = File.expand_path File.dirname(__FILE__)
lib = File.expand_path "#{here}/lib"
$: << lib
require "doc_page"
require "step_page"
require "markdown_page"
require "media_wiki_page"
require "raw_page"
require "deck"
require "deck/rack_app"
require "titleizer"
require "site"
require 'sprockets'
require 'jquery-cdn'
class InstallFest < Sinatra::Application # todo: use Sinatra::Base instead, with more explicit config
include Erector::Mixin
# Set available locales in Array of Strings; this is also used when
# checking availability in dynamic locale assignment, they must be strings.
AVAILABLE_LOCALES = %w[en es zh-tw].freeze
DEFAULT_SITE = "docs"
set :assets, Sprockets::Environment.new
settings.assets.append_path "assets/stylesheets"
settings.assets.append_path "assets/javascripts"
settings.assets.append_path "public/fonts"
settings.assets.append_path Bootstrap.javascripts_path
JqueryCdn.install(settings.assets)
if settings.environment == :development
set :cookie_options, domain: nil
end
configure do
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.load_path = Dir[File.join(settings.root, 'locales', '**/*.yml')]
I18n.backend.load_translations
I18n.available_locales = AVAILABLE_LOCALES
I18n.enforce_available_locales = true
I18n.default_locale = :en
end
def initialize
super
@here = File.expand_path(File.dirname(__FILE__))
end
attr_reader :here
attr_writer :default_site
# todo: test
# returns the most-specific hostname component, e.g. "foo" for "foo.example.com"
def subdomain
host.split(".").first
end
def default_site
if host && sites.include?(site = subdomain)
site
else
DEFAULT_SITE
end
end
def host
request && request.host
end
def site_dir
"#{sites_dir}/#{params[:site]}"
end
def sites_dir
Site.sites_dir
end
def sites
Dir["#{sites_dir}/*"].map { |path| File.basename(path) }
end
def redirect_sites
{
'curriculum' => 'intro-to-rails',
'intermediate-rails' => 'message-board'
}
end
def redirect_pages
{
'installfest/macintosh' => 'installfest/macOS'
}
end
before do
begin
I18n.locale = dynamic_locale
rescue I18n::InvalidLocale
I18n.locale = I18n.default_locale
end
end
after '/:site/*' do
# Any real page (starts with a site and doesn't end with an extension)
# gets saved as the 'back' for the next pageload.
if sites.include?(params[:site]) && !request.fullpath.match(/\..+\z/)
cookies[:docs_back_path] = request.fullpath
end
end
def dynamic_locale
(params && (params[:locale] || params[:l])) ||
(host && AVAILABLE_LOCALES.include?(subdomain) && subdomain) ||
(ENV['SITE_LOCALE'])
end
def src
File.read(doc_path)
end
def ext
$1 if doc_path.match(/\.(.*)/)
end
def doc_path
@doc_path ||= begin
base = "#{site_dir}/#{params[:name]}"
Site::DOC_TYPES.each do |ext|
path = "#{base}.#{ext}"
return path if File.exist?(path)
end
raise Errno::ENOENT, base
end
end
def back_path
path_parts = cookies[:docs_back_path]&.split('/')
return unless path_parts && path_parts.length > 2
current_path_parts = request.fullpath.split('/')
prev_site, prev_page = path_parts[1..2]
this_site, this_page = current_path_parts[1..2]
return unless prev_site == this_site
return if prev_page == this_page
prev_page
end
def render_page
begin
options = {
site: Site.named(params[:site]),
page_name: params[:name],
doc_title: Titleizer.title_for_page(params[:name]),
doc_path: doc_path,
back: back_path,
src: src,
locale: I18n.locale,
}
case ext
when "deck.md", "deck"
render_deck
when "md"
MarkdownPage.new(options).to_html
when "mw"
MediaWikiPage.new(options).to_html
when "step"
StepPage.new(options).to_html
else
raise "unknown file type #{doc_path}"
end
rescue Errno::ENOENT => e
p e
e.backtrace.each do |line|
break if line =~ /sinatra\/base.rb/
puts "\t"+line
end
halt 404
end
end
def render_deck
slides = Deck::Slide.split(src)
Deck::SlideDeck.new(:slides => slides).to_pretty
end
before do
expires 3600, :public
end
get '/favicon.ico' do
halt 404
end
get "/assets/:file.:ext" do
mime_type = {
'js' => 'application/javascript',
'css' => 'text/css',
'ttf' => 'application/font-ttf',
'woff' => 'application/font-woff'
}[params[:ext]]
content_type mime_type if mime_type
settings.assets["#{params[:file]}.#{params[:ext]}"]
end
get '/fonts/font-awesome/:file' do
font_path = File.join(FontAwesome::Sass.gem_path, 'assets', 'fonts', 'font-awesome', params[:file])
send_file font_path
end
get "/" do
redirect "/#{default_site}/"
end
get "/:site/:name/src" do
begin
RawPage.new(
site: Site.named(params[:site], I18n.locale),
page_name: params[:name],
doc_title: File.basename(doc_path),
doc_path: doc_path,
src: src,
locale: I18n.locale,
).to_html
rescue Errno::ENOENT => e
p e
halt 404
end
end
get "/:site/:name.zip" do
manifest_path = "#{site_dir}/#{params[:name]}.zip-manifest"
if File.exist?(manifest_path)
manifest_files = File.read(manifest_path).split("\n")
zip_path = File.join(Dir.tmpdir, "#{params[:name]}.zip")
FileUtils.rm_rf(zip_path)
Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
manifest_files.each do |filename|
filename_without_first_directory = filename.split(File::SEPARATOR)[1..-1].join(File::SEPARATOR)
location_in_zip = File.join(params[:name], filename_without_first_directory)
location_on_disk = File.join(site_dir, filename)
zipfile.add(location_in_zip, location_on_disk)
end
end
send_file zip_path
end
end
get "/:site/:name.:ext" do
if sites.include?(params[:site])
if params[:ext] == "deck" # to show a markdown page as slides, change the ".md" to ".deck"
render_deck
else
send_file "#{site_dir}/#{params[:name]}.#{params[:ext]}"
end
end
end
get "/:site/:subdir/:name.:ext" do
if sites.include?(params[:site])
send_file "#{site_dir}/#{params[:subdir]}/#{params[:name]}.#{params[:ext]}"
end
end
get "/:site/:name/" do
# remove any extraneous slash from otherwise well-formed page URLs
redirect request.fullpath.chomp('/')
end
get "/:site/:name" do
site_name = params[:site]
if redirect_sites[site_name]
redirect "#{redirect_sites[site_name]}/#{params[:name]}"
return
end
page_url = "#{params[:site]}/#{params[:name]}"
if redirect_pages[page_url]
redirect redirect_pages[page_url]
else
render_page
end
end
get "/:site/:name/:section/" do
# remove any extraneous slash from otherwise well-formed page URLs
redirect request.fullpath.chomp('/')
end
get "/:site/:name/:section" do
render_page
end
get "/:site" do
# add a slash to any URLs that contain only a site
# (otherwise paths in that site's pages would resolve relative to the root)
redirect "#{request.fullpath}/"
end
get "/:site/" do
site_name = params[:site]
if redirect_sites[site_name]
redirect "#{redirect_sites[site_name]}/"
elsif sites.include? site_name
# render the site's index page
params[:name] = site_name
render_page
end
end
end