forked from sinatra/sinatra-recipes
/
app.rb
196 lines (172 loc) · 4.27 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
require 'sinatra'
require 'rdiscount'
require 'erb'
require 'sass'
configure :production do
sha1, date = `git log HEAD~1..HEAD --pretty=format:%h^%ci`.strip.split('^')
require 'rack/cache'
use Rack::Cache
before do
cache_control :public, :must_revalidate, :max_age=>300
etag sha1
last_modified date
end
end
set :markdown, :layout_engine => :erb
set :views, File.dirname(__FILE__)
before do
@menu = Dir.glob("./*/").map do |file|
next if file =~ /tmp/ or file =~ /log/
file.split('/')[1]
end.compact.sort
end
get '/' do
markdown :README
end
get '/p/:topic' do
pass if params[:topic] == '..'
@readme = true
@children = Dir.glob("./#{params[:topic]}/*.md").map do |file|
next if file =~ /README/
next if file.empty? or file.nil?
file.split('/')[-1].sub(/\.md$/, '')
end.compact.sort
markdown :"#{params[:topic]}/README"
end
get '/p/:topic/:article' do
pass if params[:topic] == '..'
markdown :"#{params[:topic]}/#{params[:article]}"
end
get '/style.css' do
sass :style
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Sinatra Recipes</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
<link rel="shortcut icon" href="https://github.com/sinatra/resources/raw/master/logo/favicon.ico">
<script
type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'>
</script>
</head>
<body>
<a name='top' />
<div id="header">
<h2>Community contributed recipes and techniques</h2>
<h1><a href="/">
<img src="https://github.com/sinatra/resources/raw/master/logo/sinatra-classic-156.png" />
Sinatra Recipes
</a></h1>
</div>
<div id="menu">
<ul>
<% @menu.each do |me| %>
<li>
<a href='/p/<%= "#{me}" %>'>
<%= me.capitalize.sub('_', ' ') %>
</a>
</li>
<% end %>
</ul>
</div>
<div id="content">
<div id="post">
<%= yield %>
<% if @children %>
<div id="children">
<ul>
<% @children.each do |child| %>
<li>
<a href='/p/<%= "#{params[:topic]}/#{child}" %>'>
<%= child.capitalize.sub('_', ' ') %>
</a>
</li>
<% end %>
</ul>
</div>
<% end %>
<% if @readme %>
<div id="footer">
<h2>Did we miss something?</h2>
<p>It's very possible we've left something out, thats why we need your help! This
is a community driven project after all. Feel free to fork the project and send
us a pull request to get your recipe or tutorial included in the book.</p>
<p>See the <a href="http://github.com/sinatra/recipes/blob/master/README.md">README</a> for more details.</p>
</div>
<% end %>
</div>
</div>
<a href="http://github.com/sinatra/sinatra-recipes">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub" />
</a>
</body>
</html>
@@ style
body
font-family: 'Lucida Grande', Verdana, sans-serif
margin: 0 auto
max-width: 800px
h1, h2, h3, h4, h5
font-family: georgia, 'bitstream vera serif', serif
font-weight: normal
font-size: 2em
line-height: 160%
a:link, a:visited
color: #3F3F3F
a:hover, a:active
color: #8F8F8F
.small
font-size: .7em
#header
margin: 10px 0px
a
text-decoration: none
h1
float: left
width: 250px
font-size: 2.65em
line-height: .75em
h2
text-align: right
font-style: oblique
font-size: 1em
float: right
width: 450px
img
float: left
width: 100px
margin-right: 15px
border: 0
#menu
float: left
max-width: 200px
word-wrap: break-word
font-size: .9em
clear: left
#children
clear: both
ul li
float: left
width: 275px
height: 40px
#content
float: right
max-width: 600px
pre
padding: 10px
max-width: 600px
overflow: auto
overflow-Y: hidden
background: #EEE
line-height: 100%
#post
line-height: 110%
#footer
clear: both
margin-top: 20px