Piano
Out-of-the-box Sinatra server for fast website sketching using Haml and Sass and CoffeeScript (and YAML!).
The magic triplet, one command away!
Installation
gem install piano
Standalone Usage
server/folder$ piano [<port-number> <environment> [options]]
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 4567
and the default environment :development
.
Haml (haml-lang.com) .haml
files and Sass (sass-lang.com) .sass
and CoffeeScript (github.com/josh/ruby-coffee-script) .coffee
files in the base folder will automatically be mapped to urls.
yoursite.com/users => server/folder/users.haml
yoursite.com/style.css => server/folder/style.sass
yoursite.com/app.js => server/folder/app.coffee
Other files (images, plain text files, etc) will be loaded from the server/folder/public
as is default behavior in Sinatra.
YAML Data
When receiving a request for "/users"
, Piano will look up for a YAML file server/folder/data/users.haml
. If it is there, the YAML file will be loaded and available for the correspondent Haml template in the @data
variable.
5 minutes site!
…all working with stylesheet, scripts and YAML data sources.
folder/index.haml
!!! 5
%head
%title= @data['title']
= style "style.css"
= script "app.js"
%body
%h1= @data['title']
%p= @data['description']
%ul
- @data['list'].each do |item|
%li= item
folder/style.sass
body
width: 960px
margin: 0 auto
font:
family: sans-serif
size: 15px
folder/app.coffee
alert "This is too simple to be true"
folder/data/index.yaml
title: 5 minutes site!
description: Is amazing how simple it gets
list:
- and I can have
- a list
- also.
Note: You can find this sample in the repository within the /sample
folder.
Going :production!
Piano goes production in command line just adding production
to its arguments. When it goes, it goes this way:
-
Now any unmatched route will give a zero-information-disclosure nice old 404 error page
-
And the default behaviour for 500 errors in Sinatra.
For nicety sake, you can personalize 404 pages simply by creating a server/folder/404.haml
template. Beware when you do: out there be dragons.
Note: you can also add a server/folder/data/404.yaml
file to keep layer separation even in your error pages.
Command line options summary
-
Port number: Any number passed as an argument to the
piano
command will be used as the port number. -
Environment: Any string that does not matches any other argument will be setted as the environment.
-
noetags
: Addingnoetags
to the shell command will cause Piano to run without etags. -
nocoffee
: Addingnocoffee
to the shell command will cause Piano to Not load the CoffeeScript library. Useful when no JavaScript environment is available.
Library Usage
If you want, you can include Piano as a library in your own server. Kind of defeats the point of using Piano, but useful if you want to extend its functionality.
require "piano"
Piano.play! # .play! added 4 the lulz; Piano.run! will do the trick aswell
Piano
inherits Sinatra::Base
, so all of Sinatra::Base
own methods are available. Read the Sinatra documentation for further information. Tip: put
Piano.environment = :production
just before letting it play for play in production environment!
By setting Piano.etags = :off
, etags will be disabled.
And extending it…
Of course, you can also do this:
require "piano"
class ExtendingPiano < Piano
get "/" do
"This will not work since this route has already been matched."
end
# ...but you can add "before do" and "after do" filters
run! # or play!
end
Remember that the server folder will be the working directory when the script is executed, and not the script path as usual in Sinatra.
You can even do this:
class MySinatraServer < Sinatra::Base
use Piano
get "/" do
"My guess is this still won't work, yet I haven't properly tested it"
end
# ...many other things
end
…currently I can’t predict its behavior since I haven’t tested it.
Candies for the kidz
Convenience helpers
style
and script
Piano features two convenience helpers to include stylesheets and javascripts: style("style.css")
and script("app.js")
.
You can use them in your haml templates like this:
!!! 5
%html
%head
%title Out-of-the-box is pretty awesome!
= style "style.css"
= script "app.js"
extract
Another helper you may find useful is extract("source_text/html", word_count = 80)
. Returns an extract of the first word_count
words (default is 80), html tags stripped, and closed by "..."
. It does nothing is the text is less than word_count
words in length.
%p= extract content, 25
unicode_entities
Useful for hashing email addresses (and hiding them from crawlers. Returns the htmlentities in unicode for each letter of the argument string. For example:
%p= unicode_entities "[email protected]"
Code is poetry.
Etags
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.
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.
Gem dependencies
-
sinatra (sinatrarb.com)
-
haml (haml-lang.com)
-
sass (sass-lang.com)
-
coffee-script (github.com/josh/ruby-coffee-script)
Desired (future) features
-
Folder paths configurable.
-
Condition for disabling default Piano routes
-
Further documentation of Piano helpers
-
More helpers for semantic data handling.
-
Deploy of sample with command line
--sample
argument. -
Online source files edition.
-
Test
use Piano
within aSinatra::Base
class. -
If CoffeeScript can’t run, inform of the
nocoffee
command line option. -
Now it would be nice to give Piano personalized templates not only to 404 but for all error pages, specially 500
-
Custom error when there’s no data
Done
-
Setup to production enviroment option (why not?!)
-
Etag on/off (currently etags are hardcoded on)
Tips
As for v0.7.3, Piano has now the ability to go :production
mode both in command line and library modes.
Piano is in intensive development, you might want to keep track of small updates with Bundler pointing to the github repository. Here is the line to put in your Gemfile
gem "piano", :git => "git://github.com/xaviervia/piano.git"
License
(The MIT License)
Copyright © 2011:
-
Xavier Via (germino.com.ar)
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.