forked from railsbridge/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
executable file
·204 lines (169 loc) · 4.16 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
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"
class InstallFest < Sinatra::Application # should this be Sinatra::Base instead?
include Erector::Mixin
def initialize
super
@here = File.expand_path(File.dirname(__FILE__))
@default_site = "installfest"
set_downstream_app # todo: test
end
attr_reader :here
attr_writer :default_site
def default_site
if host && sites.include?(site = host.split(".").first)
site
else
@default_site
end
end
def host
request && request.host
end
def site_dir
"#{sites_dir}/#{params[:site]}"
end
def sites_dir= dir
@sites_dir = dir.tap { set_downstream_app }
end
def set_downstream_app
@app = ::Deck::RackApp.public_file_server
end
def sites_dir
@sites_dir || "#{@here}/sites"
end
def sites
Dir["#{sites_dir}/*"].map{|path| path.split('/').last}
end
def src
File.read(doc_path)
end
def ext
doc_path.split('.').last
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 title_for_page page_name
page_name.split(/[-_]/).map do |w|
w == "osx" ? "OS X" : w.capitalize
end.join(' ')
end
def render_page
begin
options = {
site_name: params[:site],
page_name: params[:name],
doc_title: title_for_page(params[:name]),
doc_path: doc_path,
back: params[:back],
src: src,
}
case ext
when "md"
if doc_path =~ /\.deck\.md$/ # todo: refactor
# todo: render with page nav elements too
slides = Deck::Slide.split(src)
Deck::SlideDeck.new(:slides => slides).to_pretty
else
MarkdownPage.new(options).to_html
end
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
halt 404
end
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
).to_html
rescue Errno::ENOENT => e
p e
halt 404
end
end
get "/:site/:name.:ext" do
if sites.include?(params[:site])
send_file "#{site_dir}/#{params[:name]}.#{params[:ext]}"
else
forward # send it on to the downstream file server
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]}"
else
forward # send it on to the downstream file server
end
end
get "/:site/:name/" do
# remove any extraneous slash from otherwise well-formed page URLs
redirect "#{params[:site]}/#{params[:name]}"
end
get "/:site/:name" do
render_page
end
get "/:file.:ext" do
# treat root URLs with dots in them like static assets and serve them
# from the downstream file server (coderay.css, jquery-1.7.2.js)
forward
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 "#{params[:site]}/"
end
get "/:site/" do
site_name = params[:site]
if sites.include? site_name
# render the site's index page
params[:name] = site_name
render_page
else
forward # send it on to the downstream file server
end
end
end