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>]

Piano will start a Sinatra server based in the same folder where you run the command, in the port given. If no port is given, piano will start in the default Sinatra port, 4567.

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.

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.

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.

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

Desired (future) features

  • Etag on/off (currently etags are hardcoded on)

  • Folder paths and sinatra default arguments configurable.

  • Condition for disabling default Piano routes

  • Further documentation of Piano helpers

  • Custom error when there’s no data

  • More helpers for semantic data handling.

  • Deploy of sample with command line --sample argument.

  • Setup to production enviroment option (why not?!)

  • Online source files edition.

Tips

As for v0.7, not really suitable for production use, since any empty URL will return a full path disclosure of the file that Piano intended to load.

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:

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.