forked from phpbridge/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
executable file
·226 lines (188 loc) · 4.76 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
require 'sinatra'
require 'digest/md5'
require 'erector'
#require 'wrong'
#include Wrong::D
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"
class InstallFest < Sinatra::Application # todo: use Sinatra::Base instead, with more explicit config
include Erector::Mixin
def initialize
super
@here = File.expand_path(File.dirname(__FILE__))
@default_sites = {en: "docs", es: "hola"}
@default_locale = "en"
end
attr_reader :here, :default_locale
attr_writer :default_site, :default_locale
# 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_sites[locale.to_sym]
end
end
def host
request && request.host
end
def site_dir
"#{sites_dir}/#{params[:site]}"
end
def sites_dir
Site.sites_dir(locale)
end
def sites
Dir["#{sites_dir}/*"].map { |path| path.split('/').last }
end
def redirect_sites
{
'curriculum' => 'intro-to-php'
}
end
def locale
(params && (params[:locale] or params[:l])) or
(host && subdomain =~ /^..$/ && subdomain) or # note: only allows 2-char locales for now -- should check against a list of locales
(ENV['SITE_LOCALE']) or
default_locale
end
def src
File.read(doc_path)
end
def ext
ext = $1 if doc_path.match(/\.(.*)/)
end
def doc_path
@doc_path ||= begin
base = "#{site_dir}/#{params[:name]}"
%w{step md deck.md mw}.each do |ext|
path = "#{base}.#{ext}"
return path if File.exist?(path)
end
raise Errno::ENOENT, base
end
end
def render_page
begin
options = {
site_name: params[:site],
page_name: params[:name],
doc_title: Titleizer.title_for_page(params[:name]),
doc_path: doc_path,
back: params[:back],
src: src,
locale: 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 "/" do
redirect "/#{default_site}/"
end
get "/:site/:name/src" do
begin
RawPage.new(
site_name: params[:site],
page_name: params[:name],
doc_title: doc_path.split('/').last,
doc_path: doc_path,
src: src,
locale: locale,
).to_html
rescue Errno::ENOENT => e
p e
halt 404
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
# todo: make this work in a general way, without hardcoded 'img'
get "/:site/img/:name.:ext" do
if sites.include?(params[:site])
send_file "#{site_dir}/img/#{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]}"
else
render_page
end
end
get "/:site/:name/:section/" do
# remove any extraneous slash from otherwise well-formed page URLs
redirect "#{params[:site]}/#{params[:name]}/#{params[:section]}"
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