sinatra-pages 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +60 -13
- data/spec/pages_spec.rb +206 -146
- metadata +4 -4
data/README.markdown
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Sinatra Pages
|
2
|
-
This is a [Sinatra
|
2
|
+
This is a [Sinatra][1] extension that renders any static page (including sub-pages) located under your *views/* directory using [HAML][2] as the rendering engine. By combining the main driving principles behind these two libraries, this extension allows you to build static websites for [Sinatra][2] or directly on [Rack][3] in no time.
|
3
3
|
|
4
4
|
### Installation
|
5
5
|
In order to install this gem, you just need to install the gem from your command line like this:
|
@@ -8,8 +8,8 @@ In order to install this gem, you just need to install the gem from your command
|
|
8
8
|
|
9
9
|
You should take into account that this library have the following dependencies:
|
10
10
|
|
11
|
-
* [sinatra][
|
12
|
-
* [haml][
|
11
|
+
* [sinatra][1]
|
12
|
+
* [haml][2]
|
13
13
|
|
14
14
|
### Usage
|
15
15
|
Before using this extension, you should create the following file structure inside your application.
|
@@ -17,17 +17,28 @@ Before using this extension, you should create the following file structure insi
|
|
17
17
|
app/
|
18
18
|
|- app.rb
|
19
19
|
|- config.ru
|
20
|
+
|- public/
|
21
|
+
|- images/
|
22
|
+
|- ...
|
23
|
+
|- scripts/
|
24
|
+
|- ...
|
25
|
+
|- styles/
|
26
|
+
|- ...
|
20
27
|
|- views/
|
21
28
|
|- home.haml
|
22
|
-
|- a_file.haml
|
23
|
-
|- another_file.haml
|
24
|
-
|- another_file/yet_another_file.haml
|
25
|
-
|- another_file/yet_another_file/still_another_file.haml
|
26
|
-
|- ...more files and subdirectories...
|
27
29
|
|- layout.haml
|
28
30
|
|- not_found.haml
|
31
|
+
|- a_file.haml
|
32
|
+
|- another_file.haml
|
33
|
+
|- ...
|
34
|
+
|- another_file/
|
35
|
+
|- yet_another_file.haml
|
36
|
+
|- ...
|
37
|
+
|- yet_another_file/
|
38
|
+
|- still_another_file.haml
|
39
|
+
|- ...
|
29
40
|
|
30
|
-
Please notice that this extension requires you to create the *home.haml* and the *not_found.haml* files inside
|
41
|
+
Please notice that this extension requires you to create the *home.haml* and the *not_found.haml* files inside your *views/* directory on your application. Then you're free to add a *layout.haml* (if required) and all the static pages you need under any file structure hierarchy inside this directory. Please don't forget to give the extension *.haml* to all your views.
|
31
42
|
|
32
43
|
Now, as any other existing extension, there are two possible use cases depending the kind of Sinatra application you're developing. If you follow the __Classic__ approach, then you just need to require this extension in the *app.rb* file.
|
33
44
|
|
@@ -68,7 +79,43 @@ You can try your modular application by executing the following command in your
|
|
68
79
|
$app/rackup config.ru
|
69
80
|
|
70
81
|
In order to verify if you application is working, open your web browser with the address that will appear after the execution described above.
|
71
|
-
|
82
|
+
|
83
|
+
### Customization
|
84
|
+
This extension assumes the __:views__ and __:public__ configuration are located on the *./views* and *./public* directories respectively. In any case, you are able to change these values as required.
|
85
|
+
|
86
|
+
Depending on the kind of Sinatra application you're developing, you should proceed as follows. If you follow the __Classic__ approach, then you just need to set these configuration parameters in the *app.rb* file.
|
87
|
+
|
88
|
+
require 'sinatra'
|
89
|
+
require 'sinatra/pages'
|
90
|
+
|
91
|
+
set :views, File.join(Dir.pwd, 'templates')
|
92
|
+
set :public, Proc.new {File.join(root, 'static')}
|
93
|
+
|
94
|
+
In case you would prefer to follow the __Modular__ approach on your application design, then you can set these variables within your application as a class that inherit from the *Sinatra::Base* class in the *app.rb* file.
|
95
|
+
|
96
|
+
require 'sinatra/base'
|
97
|
+
require 'sinatra/pages'
|
98
|
+
|
99
|
+
class App < Sinatra::Base
|
100
|
+
register Sinatra::Pages
|
101
|
+
|
102
|
+
set :views, File.join(Dir.pwd, 'templates')
|
103
|
+
set :public, Proc.new {File.join(root, 'static')}
|
104
|
+
end
|
105
|
+
|
106
|
+
Alternatively, you can also set these variables directly from the *config.ru* file in which your application class will be associated to a certain route.
|
107
|
+
|
108
|
+
require 'app'
|
109
|
+
|
110
|
+
map '/' do
|
111
|
+
Sinatra::App.set :views, File.join(Dir.pwd, 'templates')
|
112
|
+
Sinatra::App.set :public, Proc.new {File.join(root, 'static')}
|
113
|
+
|
114
|
+
run Sinatra::App
|
115
|
+
end
|
116
|
+
|
117
|
+
In order to verify if the customized setup is working as expected, open your web browser with the address that will appear after the execution described above.
|
118
|
+
|
72
119
|
### Contributions
|
73
120
|
Everybody is welcome to contribute to this project by commenting the source code, suggesting modifications or new ideas, reporting bugs, writing some documentation and, of course, you're also welcome to contribute with patches as well!
|
74
121
|
|
@@ -95,9 +142,9 @@ This extension have been tested on the versions 1.8.6, 1.8.7 and 1.9.1 of the [R
|
|
95
142
|
### License
|
96
143
|
This extension is licensed under the [MIT License][9].
|
97
144
|
|
98
|
-
[1]: http://www.sinatrarb.com/
|
99
|
-
[2]: http://
|
100
|
-
[3]: http://
|
145
|
+
[1]: http://www.sinatrarb.com/
|
146
|
+
[2]: http://haml-lang.com/
|
147
|
+
[3]: http://rack.rubyforge.org/
|
101
148
|
[4]: http://rspec.info/
|
102
149
|
[5]: http://eigenclass.org/hiki/rcov
|
103
150
|
[6]: http://gitrdoc.com/brynary/rack-test/tree/master
|
data/spec/pages_spec.rb
CHANGED
@@ -8,10 +8,6 @@ describe Sinatra::Pages do
|
|
8
8
|
TestApp
|
9
9
|
end
|
10
10
|
|
11
|
-
before :all do
|
12
|
-
PAGES.each{|page| create_file_for(page)}
|
13
|
-
end
|
14
|
-
|
15
11
|
context 'built-in settings' do
|
16
12
|
context 'by default' do
|
17
13
|
subject {app}
|
@@ -50,196 +46,260 @@ describe Sinatra::Pages do
|
|
50
46
|
end
|
51
47
|
end
|
52
48
|
|
53
|
-
context
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
49
|
+
context 'on HTTP GET' do
|
50
|
+
before :all do
|
51
|
+
PAGES.each{|page| create_file_for(page, app.views)}
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'in synchronous mode' do
|
55
|
+
context 'with no Layout' do
|
56
|
+
subject {File.join app.views, "#{file_of('Layout')}.haml"}
|
57
|
+
it {File.exist?(subject).should == false}
|
58
|
+
|
59
|
+
context 'and with Home static page' do
|
60
|
+
subject {File.join app.views, "#{file_of('Home')}.haml"}
|
61
|
+
it {File.exist?(subject).should == true}
|
62
|
+
it 'should render it.' do
|
63
|
+
['/', ''].each do |route|
|
64
|
+
get route
|
65
|
+
|
66
|
+
last_request.should_not be_xhr
|
67
|
+
last_response.should be_ok
|
68
|
+
last_response.body.chomp.should == file_of('Home')
|
69
|
+
end
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
73
|
+
Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
|
74
|
+
filename = File.basename(file, '.haml').capitalize
|
75
|
+
|
76
|
+
context "and with #{filename} static page" do
|
77
|
+
subject {file}
|
78
|
+
it {File.exist?(subject).should == true}
|
79
|
+
it "should render it." do
|
80
|
+
directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
|
81
|
+
path = "#{directory}/#{File.basename(subject, '.haml')}"
|
82
|
+
|
83
|
+
[path, "#{path}/"].each do |route|
|
84
|
+
get route
|
85
|
+
|
86
|
+
last_request.should_not be_xhr
|
87
|
+
last_response.should be_ok
|
88
|
+
last_response.body.chomp.should == File.basename(subject, '.haml')
|
89
|
+
end
|
90
|
+
end
|
81
91
|
end
|
82
92
|
end
|
83
93
|
|
84
|
-
|
85
|
-
File.
|
86
|
-
File.exist?(
|
87
|
-
|
88
|
-
|
94
|
+
context 'and with a non-existing static page' do
|
95
|
+
subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
|
96
|
+
it {File.exist?(subject).should == false}
|
97
|
+
it 'should render the Not Found page.' do
|
98
|
+
path = "/#{file_of('Do Not Exist')}"
|
99
|
+
|
100
|
+
[path, "#{path}/"].each do |route|
|
101
|
+
get route
|
89
102
|
|
90
|
-
|
91
|
-
|
92
|
-
|
103
|
+
last_request.should_not be_xhr
|
104
|
+
last_response.should be_not_found
|
105
|
+
last_response.body.chomp.should == file_of('Not Found')
|
106
|
+
end
|
107
|
+
end
|
93
108
|
end
|
94
109
|
end
|
95
110
|
|
96
|
-
context "with Layout
|
111
|
+
context "with Layout" do
|
97
112
|
before :all do
|
98
|
-
create_file_for('Layout',
|
113
|
+
create_file_for('Layout', app.views, ['Layout', '= yield'])
|
99
114
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
115
|
+
|
116
|
+
subject {File.join app.views, "#{file_of('Layout')}.haml"}
|
117
|
+
it {File.exist?(subject).should == true}
|
118
|
+
|
119
|
+
context 'and with Home static page' do
|
120
|
+
subject {File.join app.views, "#{file_of('Home')}.haml"}
|
121
|
+
it {File.exist?(subject).should == true}
|
122
|
+
it 'should render both.' do
|
123
|
+
['/', ''].each do |route|
|
124
|
+
get route
|
125
|
+
|
126
|
+
last_request.should_not be_xhr
|
127
|
+
last_response.should be_ok
|
128
|
+
separate(last_response.body).first.should == 'Layout'
|
129
|
+
separate(last_response.body).last.should == file_of('Home')
|
130
|
+
end
|
112
131
|
end
|
113
132
|
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
133
|
+
|
134
|
+
Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
|
135
|
+
filename = File.basename(file, '.haml').capitalize
|
136
|
+
|
137
|
+
context "and with #{filename} static page" do
|
138
|
+
subject {file}
|
139
|
+
it {File.exist?(subject).should == true}
|
140
|
+
it "should render both." do
|
141
|
+
directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
|
142
|
+
path = "#{directory}/#{File.basename(subject, '.haml')}"
|
143
|
+
|
144
|
+
[path, "#{path}/"].each do |route|
|
145
|
+
get route
|
146
|
+
|
147
|
+
last_request.should_not be_xhr
|
148
|
+
last_response.should be_ok
|
149
|
+
separate(last_response.body).first.should == 'Layout'
|
150
|
+
separate(last_response.body).last.should == File.basename(file, '.haml')
|
151
|
+
end
|
152
|
+
end
|
128
153
|
end
|
129
154
|
end
|
130
155
|
|
131
|
-
|
132
|
-
File.
|
133
|
-
File.exist?(
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
156
|
+
context 'and with a non-existing static page' do
|
157
|
+
subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
|
158
|
+
it {File.exist?(subject).should == false}
|
159
|
+
it 'should render the Layout and the Not Found page.' do
|
160
|
+
path = "/#{file_of('Do Not Exist')}"
|
161
|
+
|
162
|
+
[path, "#{path}/"].each do |route|
|
163
|
+
get route
|
164
|
+
|
165
|
+
last_request.should_not be_xhr
|
166
|
+
last_response.should be_not_found
|
167
|
+
separate(last_response.body).first.should == 'Layout'
|
168
|
+
separate(last_response.body).last.should == file_of('Not Found')
|
169
|
+
end
|
170
|
+
end
|
141
171
|
end
|
142
172
|
|
143
173
|
after :all do
|
144
|
-
FileUtils.rm
|
174
|
+
FileUtils.rm "#{app.views}/layout.haml"
|
145
175
|
end
|
146
176
|
end
|
147
177
|
end
|
148
178
|
|
149
179
|
context "in asynchronous mode" do
|
150
|
-
context
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
180
|
+
context 'with no Layout' do
|
181
|
+
subject {File.join app.views, "#{file_of('Layout')}.haml"}
|
182
|
+
it {File.exist?(subject).should == false}
|
183
|
+
|
184
|
+
context 'and with Home static page' do
|
185
|
+
subject {File.join app.views, "#{file_of('Home')}.haml"}
|
186
|
+
it {File.exist?(subject).should == true}
|
187
|
+
it 'should render it.' do
|
188
|
+
['/', ''].each do |route|
|
189
|
+
request route, :method => 'GET', :xhr => true
|
190
|
+
|
191
|
+
last_request.should be_xhr
|
192
|
+
last_response.should be_ok
|
193
|
+
last_response.body.chomp.should == file_of('Home')
|
194
|
+
end
|
161
195
|
end
|
162
196
|
end
|
163
197
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
198
|
+
Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
|
199
|
+
filename = File.basename(file, '.haml').capitalize
|
200
|
+
|
201
|
+
context "and with #{filename} static page" do
|
202
|
+
subject {file}
|
203
|
+
it {File.exist?(subject).should == true}
|
204
|
+
it "should render it." do
|
205
|
+
directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
|
206
|
+
path = "#{directory}/#{File.basename(subject, '.haml')}"
|
207
|
+
|
208
|
+
[path, "#{path}/"].each do |route|
|
209
|
+
request route, :method => 'GET', :xhr => true
|
210
|
+
|
211
|
+
last_request.should be_xhr
|
212
|
+
last_response.should be_ok
|
213
|
+
last_response.body.chomp.should == File.basename(subject, '.haml')
|
214
|
+
end
|
215
|
+
end
|
176
216
|
end
|
177
217
|
end
|
178
218
|
|
179
|
-
|
180
|
-
File.
|
181
|
-
File.exist?(
|
182
|
-
|
183
|
-
|
219
|
+
context 'and with a non-existing static page' do
|
220
|
+
subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
|
221
|
+
it {File.exist?(subject).should == false}
|
222
|
+
it 'should render the Not Found page.' do
|
223
|
+
path = "/#{file_of('Do Not Exist')}"
|
224
|
+
|
225
|
+
[path, "#{path}/"].each do |route|
|
226
|
+
request route, :method => 'GET', :xhr => true
|
184
227
|
|
185
|
-
|
186
|
-
|
187
|
-
|
228
|
+
last_request.should be_xhr
|
229
|
+
last_response.should be_not_found
|
230
|
+
last_response.body.chomp.should == file_of('Not Found')
|
231
|
+
end
|
232
|
+
end
|
188
233
|
end
|
189
234
|
end
|
190
235
|
|
191
|
-
context
|
236
|
+
context 'with a Layout' do
|
192
237
|
before :all do
|
193
|
-
create_file_for('Layout',
|
238
|
+
create_file_for('Layout', app.views, ['Layout', '= yield'])
|
194
239
|
end
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
240
|
+
|
241
|
+
subject {File.join app.views, "#{file_of('Layout')}.haml"}
|
242
|
+
it {File.exist?(subject).should == true}
|
243
|
+
|
244
|
+
context 'and with Home static page' do
|
245
|
+
subject {File.join app.views, "#{file_of('Home')}.haml"}
|
246
|
+
it {File.exist?(subject).should == true}
|
247
|
+
it 'should render it.' do
|
248
|
+
['/', ''].each do |route|
|
249
|
+
request route, :method => 'GET', :xhr => true
|
250
|
+
|
251
|
+
last_request.should be_xhr
|
252
|
+
last_response.should be_ok
|
253
|
+
last_response.body.chomp.should == file_of('Home')
|
254
|
+
end
|
206
255
|
end
|
207
256
|
end
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
257
|
+
|
258
|
+
Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
|
259
|
+
filename = File.basename(file, '.haml').capitalize
|
260
|
+
|
261
|
+
context "and with #{filename} static page" do
|
262
|
+
subject {file}
|
263
|
+
it {File.exist?(subject).should == true}
|
264
|
+
it "should render both." do
|
265
|
+
directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
|
266
|
+
path = "#{directory}/#{File.basename(subject, '.haml')}"
|
267
|
+
|
268
|
+
[path, "#{path}/"].each do |route|
|
269
|
+
request route, :method => 'GET', :xhr => true
|
270
|
+
|
271
|
+
last_request.should be_xhr
|
272
|
+
last_response.should be_ok
|
273
|
+
last_response.body.chomp.should == File.basename(file, '.haml')
|
274
|
+
end
|
275
|
+
end
|
221
276
|
end
|
222
277
|
end
|
223
278
|
|
224
|
-
|
225
|
-
File.
|
226
|
-
File.exist?(
|
227
|
-
|
228
|
-
|
279
|
+
context 'and with a non-existing static page' do
|
280
|
+
subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
|
281
|
+
it {File.exist?(subject).should == false}
|
282
|
+
it 'should render the Layout and the Not Found page.' do
|
283
|
+
path = "/#{file_of('Do Not Exist')}"
|
284
|
+
|
285
|
+
[path, "#{path}/"].each do |route|
|
286
|
+
request route, :method => 'GET', :xhr => true
|
229
287
|
|
230
|
-
|
231
|
-
|
232
|
-
|
288
|
+
last_request.should be_xhr
|
289
|
+
last_response.should be_not_found
|
290
|
+
last_response.body.chomp.should == file_of('Not Found')
|
291
|
+
end
|
292
|
+
end
|
233
293
|
end
|
234
294
|
|
235
295
|
after :all do
|
236
|
-
FileUtils.rm
|
296
|
+
FileUtils.rm "#{app.views}/layout.haml"
|
237
297
|
end
|
238
298
|
end
|
239
299
|
end
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
300
|
+
|
301
|
+
after :all do
|
302
|
+
FileUtils.rm_r app.views, :force => true
|
303
|
+
end
|
244
304
|
end
|
245
305
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-pages
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Julio Javier Cicchelli
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: 0.5.4
|
99
99
|
type: :development
|
100
100
|
version_requirements: *id005
|
101
|
-
description: " A Sinatra extension for static pages rendering.\n"
|
101
|
+
description: " A Sinatra extension for static pages rendering using the HAML rendering engine.\n"
|
102
102
|
email: javier@rock-n-code.com
|
103
103
|
executables: []
|
104
104
|
|