piano 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- views/*
5
+ views/*
6
+ test/*
@@ -1,6 +1,6 @@
1
1
  = Piano
2
2
 
3
- Out-of-the-box sinatra server for fast website sketching using haml and sass and coffee-script.
3
+ Out-of-the-box Sinatra server for fast website sketching using Haml and Sass and CoffeeScript (and YAML!).
4
4
 
5
5
  The magic triplet, one command away!
6
6
 
@@ -8,13 +8,13 @@ The magic triplet, one command away!
8
8
 
9
9
  gem install piano
10
10
 
11
- == Usage
11
+ == Standalone Usage
12
12
 
13
13
  server/folder$ piano [<port-number>]
14
14
 
15
15
  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.
16
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.
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
18
 
19
19
  yoursite.com/users => server/folder/users.haml
20
20
  yoursite.com/style.css => server/folder/style.sass
@@ -22,8 +22,81 @@ Haml(http://haml-lang.com) <tt>.haml</tt> files and Sass (http://sass-lang.com)
22
22
 
23
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
24
 
25
+ == YAML Data
26
+
27
+ When receiving a request for <tt>"/users"</tt>, Piano will look up for a YAML file <tt>server/folder/<strong>data/</strong>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.
28
+
29
+ == 5 minutes site!
30
+
31
+ ...all working with stylesheet, scripts and YAML data sources.
32
+
33
+ <strong>folder/index.haml:</strong>
34
+
35
+ !!! 5
36
+ %head
37
+ %title= @data['title']
38
+ = style "style.css"
39
+ = script "app.js"
40
+ %body
41
+ %h1= @data['title']
42
+ %p= @data['description']
43
+ %ul
44
+ - @data['list'].each do |item|
45
+ %li= item
46
+
47
+ <strong>folder/style.sass:</strong>
48
+
49
+ body
50
+ width: 960px
51
+ margin: 0 auto
52
+ font:
53
+ family: sans-serif
54
+ size: 15px
55
+
56
+ <strong>folder/app.coffee:</strong>
57
+
58
+ alert "This is too simple to be true"
59
+
60
+ <strong>folder/data/index.yaml:</strong>
61
+
62
+ title: 5 minutes site!
63
+ description: Is amazing how simple it gets
64
+ list:
65
+ - and I can have
66
+ - a list
67
+ - also.
68
+
69
+ Note: You can find this sample in the repository within the sample folder.
70
+
71
+ == Library Usage
72
+
73
+ 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.
74
+
75
+ require "piano"
76
+
77
+ Piano.play! # .play! added 4 the lulz; Piano.run! will do the trick aswell
78
+
79
+ Piano inherits Sinatra::Base, so all of Sinatra::Base own methods are available.
80
+
81
+ Of course, you can also do this:
82
+
83
+ require "piano"
84
+
85
+ class ExtendingPiano < Piano
86
+ get "/" do
87
+ "This will not work since this route has already been matched."
88
+ end
89
+ # ...but you can add "before do" and "after do" filters
90
+
91
+ run! # or play!
92
+ end
93
+
94
+ Remember that the server folder will be the working directory when the script is executed, and not the script path as usual in Sinatra.
95
+
25
96
  == Candies for the kidz
26
97
 
98
+ === Convenience helpers
99
+
27
100
  Piano features two convenience helpers to include stylesheets and javascripts: <tt>style("style.css")</tt> and <tt>script("app.js")</tt>.
28
101
 
29
102
  You can use them in your haml templates like this:
@@ -37,6 +110,12 @@ You can use them in your haml templates like this:
37
110
 
38
111
  Code is poetry.
39
112
 
113
+ === Etags
114
+
115
+ 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.
116
+
117
+ 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.
118
+
40
119
  == Gem dependencies
41
120
 
42
121
  * sinatra (http://sinatrarb.com)
@@ -46,6 +125,8 @@ Code is poetry.
46
125
 
47
126
  == Desired (future) features
48
127
 
128
+ * Etag on/off (currently etags are hardcoded on)
129
+ * <tt>"data"</tt> folder name configurable.
49
130
  * Online source files edition.
50
131
 
51
132
  == Tips
@@ -2,74 +2,62 @@ require "sinatra/base"
2
2
  require "haml"
3
3
  require "sass"
4
4
  require "coffee-script"
5
+ require "yaml"
5
6
 
6
7
  class Piano < Sinatra::Base
7
8
  set :root, File.expand_path(Dir.pwd)
8
9
  set :views, File.expand_path(Dir.pwd)
9
10
 
10
11
  get "/" do
11
- try_haml :index
12
+ @data = data_for "index"
13
+ try_haml "index"
12
14
  end
13
15
 
14
16
  get %r{/(.+?).css} do |something|
15
17
  content_type :css
16
- sass something.to_sym
18
+ sass something
17
19
  end
18
20
 
19
21
  get %r{/(.+?).js} do |something|
20
22
  content_type :js
21
- coffee something.to_sym
23
+ coffee something
22
24
  end
23
25
 
24
26
  get %r{/(.+)$} do |something|
25
- try_haml something.to_sym
27
+ @data = data_for something
28
+ try_haml something
26
29
  end
27
30
 
28
31
  helpers do
29
32
  def try_haml(template)
30
- begin
31
- return haml template
32
- rescue Exception => error
33
- if error.message =~ /No such file or directory/
34
- path = File.expand_path(Dir.pwd)
35
- response = "<h1>You have still to put something here.</h1><p>This is <em>#{path}/#{template.to_sym}.haml</em></p><blockquote>Good luck!</blockquote>"
36
- return response
37
- else
38
- raise error
39
- end
40
- end
33
+ file_name = "#{pwd}/#{template}.haml"
34
+ bad_luck file_name unless File.exists? file_name
35
+
36
+ hash = hash_for template, :haml
37
+ hash += hash_for "data/#{template}", :yaml if File.exists? "#{pwd}/data/#{template}.yaml"
38
+ etag hash
39
+ haml template.to_sym
41
40
  end
42
41
 
43
42
  def sass(template)
44
- begin
45
- dir = File.expand_path(Dir.pwd)
46
- data = File.read "#{dir}/#{template.to_s}.sass"
47
- return Sass.compile(data, :syntax => :sass)
48
- rescue Exception => error
49
- if error.message =~ /No such file or directory/
50
- path = File.expand_path(Dir.pwd)
51
- response = "<h1>You have still to put something here.</h1><p>This is <em>#{path}/#{template.to_s}.sass</em></p><blockquote>Good luck!</blockquote>"
52
- return response
53
- else
54
- raise error
55
- end
56
- end
43
+ file_name = "#{pwd}/#{template}.sass"
44
+ bad_luck file_name unless File.exists? file_name
45
+
46
+ etag hash_for(template, :sass)
47
+ Sass.compile File.read(file_name), :syntax => :sass
57
48
  end
58
49
 
59
50
  def coffee(template)
60
- begin
61
- dir = File.expand_path(Dir.pwd)
62
- data = File.read "#{dir}/#{template.to_s}.coffee"
63
- return CoffeeScript.compile(data)
64
- rescue Exception => error
65
- if error.message =~ /No such file or directory/
66
- path = File.expand_path(Dir.pwd)
67
- response = "<h1>You have still to put something here.</h1><p>This is <em>#{path}/#{template.to_s}.coffee</em></p><blockquote>Good luck!</blockquote>"
68
- return response
69
- else
70
- raise error
71
- end
72
- end
51
+ file_name = "#{pwd}/#{template}.coffee"
52
+ bad_luck file_name unless File.exists? file_name
53
+
54
+ etag hash_for(template, :coffee)
55
+ CoffeeScript.compile(File.read(file_name))
56
+ end
57
+
58
+ def data_for(template)
59
+ file_name = "#{pwd}/data/#{template}.yaml"
60
+ YAML.load_file(file_name) if File.exists?(file_name)
73
61
  end
74
62
 
75
63
  def style(path)
@@ -79,6 +67,21 @@ class Piano < Sinatra::Base
79
67
  def script(path)
80
68
  "<script type='text/javascript' src='#{path}'></script>"
81
69
  end
70
+
71
+ def pwd
72
+ File.expand_path(Dir.pwd)
73
+ end
74
+
75
+ def bad_luck(path)
76
+ halt 404, "<h1>You have still to put something here.</h1><p>This is <em>#{path}</em></p><blockquote>Good luck!</blockquote>"
77
+ end
78
+
79
+ def hash_for(name, type)
80
+ "#{name}.#{type} - " + File.mtime("#{pwd}/#{name}.#{type}").to_s
81
+ end
82
82
  end
83
83
 
84
+ def self.play!
85
+ self.run!
86
+ end
84
87
  end
@@ -1,3 +1,3 @@
1
1
  module Piano
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -0,0 +1 @@
1
+ alert "This is too simple to be true"
@@ -0,0 +1,6 @@
1
+ title: 5 minutes site!
2
+ description: Is amazing how simple it gets
3
+ list:
4
+ - and I can have
5
+ - a list
6
+ - also.
@@ -0,0 +1,11 @@
1
+ !!! 5
2
+ %head
3
+ %title= @data['title']
4
+ = style "style.css"
5
+ = script "app.js"
6
+ %body
7
+ %h1= @data['title']
8
+ %p= @data['description']
9
+ %ul
10
+ - @data['list'].each do |item|
11
+ %li= item
@@ -0,0 +1,6 @@
1
+ body
2
+ width: 960px
3
+ margin: 0 auto
4
+ font:
5
+ family: sans-serif
6
+ size: 15px
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piano
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
17
- requirement: &22065996 !ruby/object:Gem::Requirement
17
+ requirement: &20907024 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.2.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *22065996
25
+ version_requirements: *20907024
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: haml
28
- requirement: &22065696 !ruby/object:Gem::Requirement
28
+ requirement: &20906724 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 3.1.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *22065696
36
+ version_requirements: *20906724
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sass
39
- requirement: &22065420 !ruby/object:Gem::Requirement
39
+ requirement: &20906448 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 3.1.1
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *22065420
47
+ version_requirements: *20906448
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: coffee-script
50
- requirement: &21335700 !ruby/object:Gem::Requirement
50
+ requirement: &20906172 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: 2.2.0
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *21335700
58
+ version_requirements: *20906172
59
59
  description: Out-of-the-box sinatra server for web site sketching using haml + sass
60
60
  + coffee-script
61
61
  email:
@@ -73,6 +73,10 @@ files:
73
73
  - lib/piano.rb
74
74
  - lib/piano/version.rb
75
75
  - piano.gemspec
76
+ - sample/app.coffee
77
+ - sample/data/index.yaml
78
+ - sample/index.haml
79
+ - sample/style.sass
76
80
  has_rdoc: true
77
81
  homepage: http://github.com/xaviervia/piano
78
82
  licenses: []