piano 0.10.4 → 0.10.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +248 -234
- data/Rakefile +1 -1
- data/bin/piano +62 -87
- data/bin/piano~ +87 -0
- data/lib/piano.rb +46 -186
- data/lib/piano/controllerloader.rb +39 -39
- data/lib/piano/routes.rb +23 -23
- data/lib/piano/version.rb +3 -3
- data/lib/sinatra/piano.rb +141 -0
- data/sample/data/index.yaml +7 -7
- data/sample/index.haml +10 -10
- data/sample/style.sass +5 -5
- metadata +22 -21
- data/Gemfile +0 -4
- data/spec/controller_spec.rb +0 -90
data/README.rdoc
CHANGED
@@ -1,234 +1,248 @@
|
|
1
|
-
= Piano
|
2
|
-
|
3
|
-
Out-of-the-box Sinatra server for fast website sketching using Haml and Sass and CoffeeScript (and YAML!).
|
4
|
-
|
5
|
-
The magic triplet, one command away!
|
6
|
-
|
7
|
-
== Installation
|
8
|
-
|
9
|
-
gem install piano
|
10
|
-
|
11
|
-
== Standalone Usage
|
12
|
-
|
13
|
-
server/folder$ piano [<port-number> <environment> [options]]
|
14
|
-
|
15
|
-
Piano will start a Sinatra server based in the same folder where you run the command, in the port and environment given. If no port is given, Piano will start in the default Sinatra port <tt>4567</tt> and the default environment <tt>:development</tt>.
|
16
|
-
|
17
|
-
Haml (http://haml-lang.com) <tt>.haml</tt> files and Sass (http://sass-lang.com) <tt>.sass</tt> and CoffeeScript (http://github.com/josh/ruby-coffee-script) <tt>.coffee</tt> files in the base folder will automatically be mapped to urls.
|
18
|
-
|
19
|
-
yoursite.com/users => server/folder/users.haml
|
20
|
-
yoursite.com/style.css => server/folder/style.sass
|
21
|
-
yoursite.com/app.js => server/folder/app.coffee
|
22
|
-
|
23
|
-
Other files (images, plain text files, etc) will be loaded from the <tt>server/folder/public</tt> as is default behavior in Sinatra.
|
24
|
-
|
25
|
-
== Extending functionality
|
26
|
-
|
27
|
-
Piano will try to load a file named <tt>server/folder/Pianofile</tt>. There you can add functionality, like custom helpers and routes.
|
28
|
-
|
29
|
-
Any route added to the <tt>Pianofile</tt> will be parsed before the default routes from Piano, overriding them.
|
30
|
-
|
31
|
-
==== Sample <tt>Pianofile</tt>
|
32
|
-
|
33
|
-
This file, for example, will bring back the email masking functionality that was deprecated in version 0.7.6
|
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
|
-
==== folder/
|
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
|
-
get "/
|
138
|
-
"
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
Piano
|
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
|
-
*
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
*
|
211
|
-
*
|
212
|
-
*
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
1
|
+
= Piano
|
2
|
+
|
3
|
+
Out-of-the-box Sinatra server for fast website sketching using Haml and Sass and CoffeeScript (and YAML!).
|
4
|
+
|
5
|
+
The magic triplet, one command away!
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
gem install piano
|
10
|
+
|
11
|
+
== Standalone Usage
|
12
|
+
|
13
|
+
server/folder$ piano [<port-number> <environment> [options]]
|
14
|
+
|
15
|
+
Piano will start a Sinatra server based in the same folder where you run the command, in the port and environment given. If no port or environment is given, Piano will start in the default Sinatra port <tt>4567</tt> and the default environment <tt>:development</tt>.
|
16
|
+
|
17
|
+
Haml (http://haml-lang.com) <tt>.haml</tt> files and Sass (http://sass-lang.com) <tt>.sass</tt> and CoffeeScript (http://github.com/josh/ruby-coffee-script) <tt>.coffee</tt> files in the base folder will automatically be mapped to urls.
|
18
|
+
|
19
|
+
yoursite.com/users => server/folder/users.haml
|
20
|
+
yoursite.com/style.css => server/folder/style.sass
|
21
|
+
yoursite.com/app.js => server/folder/app.coffee
|
22
|
+
|
23
|
+
Other files (images, plain text files, etc) will be loaded from the <tt>server/folder/public</tt> as is default behavior in Sinatra.
|
24
|
+
|
25
|
+
== Extending functionality
|
26
|
+
|
27
|
+
Piano will try to load a file named <tt>server/folder/Pianofile</tt>. There you can add functionality, like custom helpers and routes.
|
28
|
+
|
29
|
+
Any route added to the <tt>Pianofile</tt> will be parsed before the default routes from Piano, overriding them.
|
30
|
+
|
31
|
+
==== Sample <tt>Pianofile</tt>
|
32
|
+
|
33
|
+
This file, for example, will bring back the email masking functionality that was deprecated in version 0.7.6
|
34
|
+
|
35
|
+
get "/" do
|
36
|
+
"Hi! Just testing"
|
37
|
+
end
|
38
|
+
|
39
|
+
get "/email" do
|
40
|
+
"Here is my email: #{unicode_entities('xavier@example.com')}"
|
41
|
+
end
|
42
|
+
|
43
|
+
post "/" do # The "/" route, is considered "index" for the haml and yaml files
|
44
|
+
require "psych"
|
45
|
+
|
46
|
+
File.open "data/index.yaml", "w" do |file|
|
47
|
+
file.write params.to_yaml
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
helpers do
|
52
|
+
def unicode_entities(string)
|
53
|
+
encodings = ""
|
54
|
+
string.codepoints do |c|
|
55
|
+
encodings += "&##{c};"
|
56
|
+
end
|
57
|
+
encodings
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
== YAML Data
|
62
|
+
|
63
|
+
When receiving a request for <tt>"/users"</tt>, Piano will look up for a YAML file <tt>server/folder/data/users.haml</tt>. If it is there, the YAML file will be loaded and available for the correspondent Haml template in the <tt>@data</tt> variable.
|
64
|
+
|
65
|
+
== 5 minutes site!
|
66
|
+
|
67
|
+
...all working with stylesheet, scripts and YAML data sources.
|
68
|
+
|
69
|
+
==== folder/index.haml
|
70
|
+
|
71
|
+
!!! 5
|
72
|
+
%head
|
73
|
+
%title= @data['title']
|
74
|
+
= style "style.css"
|
75
|
+
= script "app.js"
|
76
|
+
%body
|
77
|
+
%h1= @data['title']
|
78
|
+
%p= @data['description']
|
79
|
+
%ul
|
80
|
+
- @data['list'].each do |item|
|
81
|
+
%li= item
|
82
|
+
|
83
|
+
==== folder/style.sass
|
84
|
+
|
85
|
+
body
|
86
|
+
width: 960px
|
87
|
+
margin: 0 auto
|
88
|
+
font:
|
89
|
+
family: sans-serif
|
90
|
+
size: 15px
|
91
|
+
|
92
|
+
==== folder/app.coffee
|
93
|
+
|
94
|
+
alert "This is too simple to be true"
|
95
|
+
|
96
|
+
==== folder/data/index.yaml
|
97
|
+
|
98
|
+
title: 5 minutes site!
|
99
|
+
description: Is amazing how simple it gets
|
100
|
+
list:
|
101
|
+
- and I can have
|
102
|
+
- a list
|
103
|
+
- also.
|
104
|
+
|
105
|
+
Note: You can find this sample in the repository within the <tt>/sample</tt> folder.
|
106
|
+
|
107
|
+
== Going :production!
|
108
|
+
|
109
|
+
Piano goes production in command line just adding <tt>production</tt> to its arguments. When it goes, it goes this way:
|
110
|
+
|
111
|
+
* Now any unmatched route will give a zero-information-disclosure nice old 404 error page
|
112
|
+
* And the default behaviour for 500 errors in Sinatra.
|
113
|
+
|
114
|
+
For nicety sake, you can personalize 404 pages simply by creating a <tt>server/folder/404.haml</tt> template. Beware when you do: out there be dragons.
|
115
|
+
|
116
|
+
Note: you can also add a <tt>server/folder/data/404.yaml</tt> file to keep layer separation even in your error pages.
|
117
|
+
|
118
|
+
== Command line options summary
|
119
|
+
|
120
|
+
* Port number: Any number passed as an argument to the <tt>piano</tt> command will be used as the port number.
|
121
|
+
* Environment: Any string that does not matches any other argument will be setted as the environment.
|
122
|
+
* <tt>noetags</tt>: Adding <tt>noetags</tt> to the shell command will cause Piano to run without etags.
|
123
|
+
* <tt>views:<views_path></tt> Sets the views folder, within server/folder
|
124
|
+
* <tt>public:<public_path></tt> Sets the public folder, within server/folder
|
125
|
+
== Library Usage as Sinatra Extension
|
126
|
+
|
127
|
+
Piano is fully usable as a Sinatra Extension. Provide the helpers, <tt>sass("template")</tt>, <tt>coffee("template")</tt>, <tt>try_haml("template")</tt>.
|
128
|
+
|
129
|
+
Note: Prior to version 0.8.2, Piano was intended to be used as a subclass of Sinatra::Base, but now it works both as a Sinatra Extension and as a subclass.
|
130
|
+
Further moves to keep most functionality as a Sinatra Extension will be done in the future, except in the <tt>piano/routes</tt>.
|
131
|
+
|
132
|
+
require "piano"
|
133
|
+
|
134
|
+
class MyPiano < Sinatra::Base
|
135
|
+
helpers Sinatra::Piano
|
136
|
+
|
137
|
+
get "/" do
|
138
|
+
"Let's change the default behaviour"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
MyPiano.run!
|
143
|
+
|
144
|
+
=== Routes
|
145
|
+
|
146
|
+
To load the routes (the ones that match your requests with your haml, sass and coffee templates) you have to require also <tt>"piano/routes"</tt>. Usually you'll want to load them after you define your own ones, otherwise you won't be able to override them.
|
147
|
+
|
148
|
+
require "piano"
|
149
|
+
|
150
|
+
class Piano
|
151
|
+
get "/special" do
|
152
|
+
"A special route, overriding the default 'special.haml'"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
require "piano/routes"
|
157
|
+
|
158
|
+
Piano.play! # .play! added 4 the lulz; Piano.run! will do the trick aswell
|
159
|
+
|
160
|
+
<tt>Piano</tt> inherits <tt>Sinatra::Base</tt>, so all of <tt>Sinatra::Base</tt> own methods are available. Read the Sinatra documentation (http://www.sinatrarb.com/intro) for further information.
|
161
|
+
|
162
|
+
Tip: put
|
163
|
+
|
164
|
+
Piano.environment = :production
|
165
|
+
|
166
|
+
just before letting it play for play in production environment!
|
167
|
+
|
168
|
+
By setting <tt>Piano.etags = :off</tt>, etags will be disabled.
|
169
|
+
|
170
|
+
== Candies for the kidz
|
171
|
+
|
172
|
+
=== Convenience helpers
|
173
|
+
|
174
|
+
==== <tt>style</tt> and <tt>script</tt>
|
175
|
+
|
176
|
+
Piano features two convenience helpers to include stylesheets and javascripts: <tt>style("style.css")</tt> and <tt>script("app.js")</tt>.
|
177
|
+
|
178
|
+
You can use them in your haml templates like this:
|
179
|
+
|
180
|
+
!!! 5
|
181
|
+
%html
|
182
|
+
%head
|
183
|
+
%title Out-of-the-box is pretty awesome!
|
184
|
+
= style "style.css"
|
185
|
+
= script "app.js"
|
186
|
+
|
187
|
+
==== <tt>extract</tt>
|
188
|
+
|
189
|
+
Another helper you may find useful is <tt>extract("source_text/html", word_count = 80)</tt>. Returns an extract of the first <tt>word_count</tt> words (default is 80), html tags stripped, and closed by <tt>"..."</tt> . It does nothing is the text is less than <tt>word_count</tt> words in length.
|
190
|
+
|
191
|
+
%p= extract content, 25
|
192
|
+
|
193
|
+
Code is poetry.
|
194
|
+
|
195
|
+
=== Etags
|
196
|
+
|
197
|
+
Since parsing YAML, Sass, Haml and CoffeeScript can be quite a burden for the processor, each response is marked with an Etag hash featuring the required file name and the timestamp of the last modification.
|
198
|
+
|
199
|
+
Etags cause client side caching. This should not be a problem since the hash changes every time a source file is modified (including the YAML data files), forcing the User-Agent to update its cache, but still is worth noting as I might not be fully aware of cache-related issues that Etag-ging may trigger.
|
200
|
+
|
201
|
+
== Gem dependencies
|
202
|
+
|
203
|
+
* sinatra (http://sinatrarb.com)
|
204
|
+
* haml (http://haml-lang.com)
|
205
|
+
* sass (http://sass-lang.com)
|
206
|
+
* coffee-script (http://github.com/josh/ruby-coffee-script)
|
207
|
+
|
208
|
+
== Desired (future) features
|
209
|
+
|
210
|
+
* Folder paths configurable.
|
211
|
+
* <tt>style</tt> and <tt>script</tt> helpers working with symbols.
|
212
|
+
* Further documentation of Piano helpers
|
213
|
+
* More helpers for semantic data handling.
|
214
|
+
* Deploy of sample with command line <tt>--sample</tt> argument.
|
215
|
+
* Online source files edition.
|
216
|
+
|
217
|
+
* Now it would be nice to give Piano personalized templates not only to 404 but for all error pages, specially 500
|
218
|
+
* Custom error when there's no data
|
219
|
+
|
220
|
+
==== Done
|
221
|
+
|
222
|
+
* Setup to production enviroment option (why not?!)
|
223
|
+
* Etag on/off (currently etags are hardcoded on)
|
224
|
+
* CoffeeScript appears to be working everywhere once <tt>therubyracer</tt> was setup to be suggested for install in no default javascript environment, so the nocoffee option was deleted.
|
225
|
+
* Default Piano routes are now overridable.
|
226
|
+
* No longer relevant since Piano is now fully extendable -> Test <tt>use Piano</tt> within a <tt>Sinatra::Base</tt> class.
|
227
|
+
|
228
|
+
== Tips
|
229
|
+
|
230
|
+
As for v0.7.3, Piano has now the ability to go <tt>:production</tt> mode both in command line and library modes.
|
231
|
+
|
232
|
+
== Deprecated functions
|
233
|
+
|
234
|
+
From version 0.7.6 on, <tt>unicode_entities</tt> has been deprecated for better Ruby version backwards compatibility.
|
235
|
+
|
236
|
+
= License
|
237
|
+
|
238
|
+
(The MIT License)
|
239
|
+
|
240
|
+
Copyright © 2011:
|
241
|
+
|
242
|
+
* Xavier Via (http://germino.com.ar)
|
243
|
+
|
244
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
245
|
+
|
246
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
247
|
+
|
248
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|