forked from railsbridge/curriculum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
railsbridge.rb
executable file
·86 lines (70 loc) · 1.87 KB
/
railsbridge.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
#!/usr/bin/ruby -w
ENV['GEM_HOME'] = "tmp/isolate/ruby-1.8"
ENV.delete 'GEM_PATH'
require 'rubygems'
require 'sinatra'
require 'erb'
require 'yaml'
require 'sinatra/base'
class RailsBridge < Sinatra::Base
helpers do
def page locals
if locals["content"] then
erb(:page, :locals => locals).gsub(/<pre>/, '<pre class="term">')
else
erb(:goal, :locals => locals).gsub(/<pre>/, '<pre class="term">')
end
end
def lesson_path n
path = "lessons/%02d.yaml" % n
path = nil unless File.file? path
path
end
def load_lesson n
lesson = YAML.load_file lesson_path n
lesson["n"] = n
lesson["section_title"] ||= nil
lesson["section_content"] ||= ""
lesson["skip_nav"] ||= false
lesson
rescue TypeError => e
raise "Error loading lesson #{n}"
end
def nav n
befor = n - 1
after = n + 1
r = []
r << "<a href=\"/#{befor}\">Prev Lesson</a>" if lesson_path befor
r << "<a href=\"/toc\">TOC</a>"
r << "<a href=\"/#{after}\">Next Lesson</a>" if lesson_path after
links = r.join(" | ")
"<div class='nav'>#{links}</div>"
end
end
get '/print' do
lessons = Dir["lessons/*.yaml"].sort.map { |f| File.basename f, ".yaml" }
lessons.map! { |n| load_lesson n.to_i(10) }
erb :index, :locals => { :lessons => lessons, :subtitle => "All Lessons" }
end
get '/toc' do
lessons = Dir["lessons/*.yaml"].sort.map { |f| File.basename f, ".yaml" }
lessons.map! { |n| load_lesson n.to_i(10) }
erb :toc, :locals => {
:lessons => lessons,
:subtitle => "Table of Contents"
}
end
get '/favicon.ico' do
halt 404
end
get '/:id' do
n = params[:id].to_i
lesson = load_lesson n
lesson["subtitle"] = "Lesson #{n}"
page lesson
end
get '/' do
redirect "/1"
end
run! if app_file == $0
end