Skip to content

Commit 0bc7c89

Browse files
committed
introduce locale (as sites subdir and optional param), and...
* use Sinatra 1.3.6 * add short stack trace to error log * various refactorings * put Deck file server ahead of Sinatra in middleware chain (using Rack::Cascade)
1 parent bcefe11 commit 0bc7c89

File tree

217 files changed

+182
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+182
-123
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ GEM
4242
rack-codehighlighter (0.5.0)
4343
nokogiri (>= 1.4.1)
4444
rack (>= 1.0.0)
45-
rack-protection (1.5.1)
45+
rack-protection (1.5.2)
4646
rack
4747
rack-test (0.6.2)
4848
rack (>= 1.0)
@@ -70,7 +70,7 @@ GEM
7070
sexp_processor (~> 4.1)
7171
sass (3.2.12)
7272
sexp_processor (4.4.1)
73-
sinatra (1.3.4)
73+
sinatra (1.3.6)
7474
rack (~> 1.4)
7575
rack-protection (~> 1.3)
7676
tilt (~> 1.3, >= 1.3.3)

app.rb

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717
require "deck"
1818
require "deck/rack_app"
1919
require "titleizer"
20+
require "site"
2021

21-
class InstallFest < Sinatra::Application
22+
class InstallFest < Sinatra::Application # todo: use Sinatra::Base instead, with more explicit config
2223
include Erector::Mixin
2324

2425
def initialize
2526
super
2627
@here = File.expand_path(File.dirname(__FILE__))
2728
@default_site = "docs"
28-
set_downstream_app # todo: test
29+
@default_locale = "en" # nil for English # todo: make a cleaner way to switch default locales
2930
end
3031

3132
attr_reader :here
32-
attr_writer :default_site
33+
attr_writer :default_site, :default_locale
34+
35+
# todo: test
36+
# returns the most-specific hostname component, e.g. "foo" for "foo.example.com"
37+
def subdomain
38+
host.split(".").first
39+
end
3340

3441
def default_site
35-
if host && sites.include?(site = host.split(".").first)
42+
if host && sites.include?(site = subdomain)
3643
site
3744
else
3845
@default_site
@@ -47,16 +54,8 @@ def site_dir
4754
"#{sites_dir}/#{params[:site]}"
4855
end
4956

50-
def sites_dir= dir
51-
@sites_dir = dir.tap { set_downstream_app }
52-
end
53-
54-
def set_downstream_app
55-
@app = ::Deck::RackApp.public_file_server
56-
end
57-
5857
def sites_dir
59-
@sites_dir || "#{@here}/sites"
58+
Site.sites_dir(locale)
6059
end
6160

6261
def sites
@@ -68,6 +67,12 @@ def redirect_sites
6867
'curriculum' => 'intro-to-rails'
6968
}
7069
end
70+
71+
def locale
72+
(params && params[:locale]) or
73+
(host && subdomain =~ /^..$/ && subdomain) or # note: only allows 2-char locales for now -- should check against a list of locales
74+
@default_locale
75+
end
7176

7277
def src
7378
File.read(doc_path)
@@ -88,12 +93,6 @@ def doc_path
8893
end
8994
end
9095

91-
def title_for_page page_name
92-
page_name.split(/[-_]/).map do |w|
93-
w == "osx" ? "OS X" : w.capitalize
94-
end.join(' ')
95-
end
96-
9796
def render_page
9897
begin
9998
options = {
@@ -126,12 +125,23 @@ def render_page
126125

127126
rescue Errno::ENOENT => e
128127
p e
128+
e.backtrace.each do |line|
129+
break if line =~ /sinatra\/base.rb/
130+
puts "\t"+line
131+
end
129132
halt 404
130133
end
131134
end
132135

133136
before do
134137
expires 3600, :public
138+
139+
if request.path =~ /^\/es(.*)/
140+
params[:locale] = "es"
141+
request.env["PATH_INFO"] = $1
142+
# p request.env["PATH_INFO"]
143+
# p request.params[:locale]
144+
end
135145
end
136146

137147
get '/favicon.ico' do
@@ -141,7 +151,7 @@ def render_page
141151
get "/" do
142152
redirect "/#{default_site}/"
143153
end
144-
154+
145155
get "/:site/:name/src" do
146156
begin
147157
RawPage.new(
@@ -160,25 +170,21 @@ def render_page
160170
get "/:site/:name.:ext" do
161171
if sites.include?(params[:site])
162172
send_file "#{site_dir}/#{params[:name]}.#{params[:ext]}"
163-
else
164-
forward # send it on to the downstream file server
165173
end
166174
end
167175

168176
# todo: make this work in a general way, without hardcoded 'img'
169177
get "/:site/img/:name.:ext" do
170178
if sites.include?(params[:site])
171179
send_file "#{site_dir}/img/#{params[:name]}.#{params[:ext]}"
172-
else
173-
forward # send it on to the downstream file server
174180
end
175181
end
176182

177183
get "/:site/:name/" do
178184
# remove any extraneous slash from otherwise well-formed page URLs
179185
redirect request.fullpath.chomp('/')
180186
end
181-
187+
182188
get "/:site/:name" do
183189
site_name = params[:site]
184190
if redirect_sites[site_name]
@@ -188,10 +194,13 @@ def render_page
188194
end
189195
end
190196

191-
get "/:file.:ext" do
192-
# treat root URLs with dots in them like static assets and serve them
193-
# from the downstream file server (coderay.css, jquery-1.7.2.js)
194-
forward
197+
get "/:site/:name/:section/" do
198+
# remove any extraneous slash from otherwise well-formed page URLs
199+
redirect "#{params[:site]}/#{params[:name]}/#{params[:section]}"
200+
end
201+
202+
get "/:site/:name/:section" do
203+
render_page
195204
end
196205

197206
get "/:site" do
@@ -208,8 +217,6 @@ def render_page
208217
# render the site's index page
209218
params[:name] = site_name
210219
render_page
211-
else
212-
forward # send it on to the downstream file server
213220
end
214221
end
215222
end

config.ru

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ use Rack::Codehighlighter, :coderay,
1414
# Thin::Logging.debug = true
1515

1616
require './app'
17-
run InstallFest
17+
run Rack::Cascade.new([
18+
Deck::RackApp.public_file_server,
19+
InstallFest
20+
])

lib/site.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class Site
22
@@here = File.expand_path(File.dirname(__FILE__))
33
@@project_root = File.dirname(@@here)
4-
@@sites_dir = File.expand_path("sites", @@project_root)
4+
5+
def self.sites_dir locale = "en"
6+
sites_dir = File.join(["sites", locale].compact)
7+
File.expand_path(sites_dir, @@project_root)
8+
end
59

6-
def self.all
7-
Dir["#{@@sites_dir}/*"].map{|dir| Site.new(dir)}
10+
def self.all locale = "en"
11+
Dir["#{sites_dir(locale)}/*"].map{|dir| Site.new(dir)}
812
end
913

1014
def self.named name

lib/site_index.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
class SiteIndex < Erector::Widget
2-
@@here = File.expand_path(File.dirname(__FILE__))
3-
@@project_root = File.dirname(@@here)
4-
@@sites_dir = File.expand_path("sites", @@project_root)
5-
62
needs :site_name
73
attr_accessor :site_name
84

@@ -12,7 +8,7 @@ def initialize(options)
128

139
def sites
1410
return @sites if @sites
15-
@sites = Dir.glob("#{@@sites_dir}/**").map { |filename| File.basename(filename) }.sort
11+
@sites = Dir.glob("#{Site.sites_dir}/**").map { |filename| File.basename(filename) }.sort
1612
end
1713

1814
def site_link site
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)