sinatra-pages 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +36 -30
- data/lib/sinatra/pages.rb +64 -13
- data/spec/pages_spec.rb +81 -14
- metadata +28 -12
data/README.markdown
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Sinatra Pages
|
2
|
-
This is a [Sinatra][1] extension that renders any static page (including sub-pages) located under your *
|
2
|
+
This is a [Sinatra][1] extension that renders any static page (including sub-pages) located under your *:pages* directory using [HAML][2] and [SASS][3] as the rendering engines for HTML and stylesheets. 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][4] 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:
|
@@ -9,7 +9,9 @@ In order to install this gem, you just need to install the gem from your command
|
|
9
9
|
You should take into account that this library have the following dependencies:
|
10
10
|
|
11
11
|
* [sinatra][1]
|
12
|
+
* [tilt][13]
|
12
13
|
* [haml][2]
|
14
|
+
* [sass][3]
|
13
15
|
|
14
16
|
### Usage
|
15
17
|
Before using this extension, you should create the following file structure inside your application.
|
@@ -24,10 +26,8 @@ Before using this extension, you should create the following file structure insi
|
|
24
26
|
|- ...
|
25
27
|
|- styles/
|
26
28
|
|- ...
|
27
|
-
|-
|
29
|
+
|- pages/
|
28
30
|
|- home.haml
|
29
|
-
|- layout.haml
|
30
|
-
|- not_found.haml
|
31
31
|
|- a_file.haml
|
32
32
|
|- another_file.haml
|
33
33
|
|- ...
|
@@ -37,8 +37,11 @@ Before using this extension, you should create the following file structure insi
|
|
37
37
|
|- yet_another_file/
|
38
38
|
|- still_another_file.haml
|
39
39
|
|- ...
|
40
|
+
|- views/
|
41
|
+
|- layout.haml
|
42
|
+
|- not_found.haml
|
40
43
|
|
41
|
-
Please notice that this extension requires you to create the *
|
44
|
+
Please notice that this extension requires you to create the *layout.haml* (if required) and other common pages inside your *:views* directory on your application. Then you're free to add the static pages you need under any file structure hierarchy inside the *:pages* directory. Please don't forget to give the extension *.haml* to all your views.
|
42
45
|
|
43
46
|
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.
|
44
47
|
|
@@ -81,15 +84,16 @@ You can try your modular application by executing the following command in your
|
|
81
84
|
In order to verify if you application is working, open your web browser with the address that will appear after the execution described above.
|
82
85
|
|
83
86
|
### Built-in variables
|
84
|
-
This extension
|
85
|
-
|
86
|
-
In any case, you are able to change these values as required.
|
87
|
+
This extension defines the following setup as default. In any case, you are able to change these values as required.
|
87
88
|
|
88
|
-
* __:
|
89
|
-
* __:
|
90
|
-
* __:
|
91
|
-
* __:
|
92
|
-
* __:
|
89
|
+
* __:public__ => <*Path to your public directory*> || **settings.root/public**
|
90
|
+
* __:views__ => <*Path to your views directory*> || **settings.root/views**
|
91
|
+
* __:pages__ => <*Path to your pages directory*> || **settings.root/views**
|
92
|
+
* __:html__ => [*:v4*, *:vX*, *:v5*] || **:v5**
|
93
|
+
* __:stylesheet__ => [*:css*, *:sass*, *:scss*] || **:scss**
|
94
|
+
* __:format__ => [*:tidy*, *:ugly*] || **:tidy**
|
95
|
+
* __:cache__ => [*:write*, *:read*] || **:write**
|
96
|
+
* __:escaping__ => [*true*, *false*] || **:false**
|
93
97
|
|
94
98
|
### Customization
|
95
99
|
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.
|
@@ -130,35 +134,37 @@ Everybody is welcome to contribute to this project by commenting the source code
|
|
130
134
|
|
131
135
|
In case you would like to contribute on this library, here's the list of extra dependencies you would need:
|
132
136
|
|
133
|
-
* [rspec][
|
134
|
-
* [rcov][
|
135
|
-
* [rack-test][
|
137
|
+
* [rspec][5]
|
138
|
+
* [rcov][6]
|
139
|
+
* [rack-test][7]
|
136
140
|
|
137
141
|
### Contributors
|
138
|
-
* [Julio Javier Cicchelli][
|
142
|
+
* [Julio Javier Cicchelli][8]
|
139
143
|
|
140
144
|
### Sites
|
141
145
|
The following sites are proudly using this extension:
|
142
146
|
|
143
|
-
* [Rock & Code][
|
144
|
-
* [Izcheznali][
|
147
|
+
* [Rock & Code][11]
|
148
|
+
* [Izcheznali][12]
|
145
149
|
|
146
150
|
If your site is also using this extension, please let us know!
|
147
151
|
|
148
152
|
### Notes
|
149
|
-
This extension have been tested on the versions 1.8.6, 1.8.7 and 1.9.1 of the [Ruby interpreter][
|
153
|
+
This extension have been tested on the versions 1.8.6, 1.8.7 and 1.9.1 of the [Ruby interpreter][9].
|
150
154
|
|
151
155
|
### License
|
152
|
-
This extension is licensed under the [MIT License][
|
156
|
+
This extension is licensed under the [MIT License][10].
|
153
157
|
|
154
158
|
[1]: http://www.sinatrarb.com/
|
155
159
|
[2]: http://haml-lang.com/
|
156
|
-
[3]: http://
|
157
|
-
[4]: http://
|
158
|
-
[5]: http://
|
159
|
-
[6]: http://
|
160
|
-
[7]: http://
|
161
|
-
[8]: http://
|
162
|
-
[9]: http://
|
163
|
-
[10]: http://
|
164
|
-
[11]: http://
|
160
|
+
[3]: http://sass-lang.com/
|
161
|
+
[4]: http://rack.rubyforge.org/
|
162
|
+
[5]: http://rspec.info/
|
163
|
+
[6]: http://eigenclass.org/hiki/rcov
|
164
|
+
[7]: http://gitrdoc.com/brynary/rack-test/tree/master
|
165
|
+
[8]: http://github.com/mr-rock
|
166
|
+
[9]: http://www.ruby-lang.org/en/
|
167
|
+
[10]: http://creativecommons.org/licenses/MIT/
|
168
|
+
[11]: http://rock-n-code.com
|
169
|
+
[12]: http://izcheznali.net
|
170
|
+
[13]: http://github.com/rtomayko/tilt
|
data/lib/sinatra/pages.rb
CHANGED
@@ -1,24 +1,51 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'haml'
|
3
|
+
require 'sass'
|
3
4
|
|
4
5
|
module Sinatra
|
5
6
|
module Pages
|
6
7
|
def self.registered(app)
|
8
|
+
app.configure do
|
9
|
+
app.set :root, Dir.pwd
|
10
|
+
app.enable :static
|
11
|
+
end
|
12
|
+
|
13
|
+
app.set :pages, Proc.new {File.join app.root, %w[pages]}
|
14
|
+
app.set :styles, Proc.new {find_styles_directory app.public}
|
7
15
|
app.set :html, :v5
|
16
|
+
app.set :stylesheet, :scss
|
17
|
+
app.set :cache, :write
|
8
18
|
app.set :format, :tidy
|
9
19
|
app.disable :escaping
|
10
20
|
|
11
21
|
app.configure do
|
12
|
-
app.set :
|
13
|
-
app.set :
|
14
|
-
app.enable :static
|
22
|
+
app.set :haml, Proc.new {generate_setup :haml, app}
|
23
|
+
app.set :sass, Proc.new {generate_setup :sass, app}
|
15
24
|
end
|
25
|
+
|
26
|
+
unless app.stylesheet == :css
|
27
|
+
app.get '/*.css' do
|
28
|
+
content_type :css, :charset => 'utf-8'
|
16
29
|
|
30
|
+
sass File.basename(params[:splat].first).to_sym, :views => settings.styles
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
app.before do
|
35
|
+
unless settings.views == settings.pages
|
36
|
+
unless File.exist?(File.join settings.views, settings.pages.split('/').last)
|
37
|
+
FileUtils.ln_s settings.pages, settings.views \
|
38
|
+
if (File.exist?(settings.views) && File.exist?(settings.pages))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
17
43
|
%w[/? /:page/? /*/:page/?].each do |route|
|
18
44
|
app.get route do
|
19
45
|
params[:page] = 'home' if params[:page].nil?
|
20
46
|
|
21
|
-
page_to_render =
|
47
|
+
page_to_render = settings.views != settings.pages ? "#{settings.pages.split('/').last}/" : ''
|
48
|
+
page_to_render << "#{params[:splat].first}/" unless params[:splat].nil?
|
22
49
|
page_to_render << params[:page]
|
23
50
|
|
24
51
|
begin
|
@@ -38,19 +65,43 @@ module Sinatra
|
|
38
65
|
|
39
66
|
private
|
40
67
|
|
41
|
-
def
|
42
|
-
options = {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
68
|
+
def generate_setup(type, settings)
|
69
|
+
options = {}
|
70
|
+
|
71
|
+
case type
|
72
|
+
when :haml
|
73
|
+
options[:ugly] = settings.format == :ugly ? true : false
|
74
|
+
options[:escape_html] = settings.escaping
|
75
|
+
options[:format] = case settings.html
|
76
|
+
when :v5 then :html5
|
77
|
+
when :v4 then :html4
|
78
|
+
when :vX then :xhtml
|
79
|
+
end
|
80
|
+
when :sass
|
81
|
+
options[:style] = settings.format == :tidy ? :expanded : :compressed
|
82
|
+
options[:syntax] = settings.stylesheet
|
83
|
+
options[:cache] = true if settings.cache == :write
|
84
|
+
options[:read_cache] = true if settings.cache == :read
|
50
85
|
end
|
51
86
|
|
52
87
|
options
|
53
88
|
end
|
89
|
+
|
90
|
+
def find_styles_directory(public_directory)
|
91
|
+
%w[*.css *.scss *.sass].each do |extension|
|
92
|
+
files = Dir.glob File.join(public_directory, '**', extension)
|
93
|
+
|
94
|
+
return File.dirname(files.first) unless files.empty?
|
95
|
+
end
|
96
|
+
|
97
|
+
%w[css styles stylesheets].each do |directory|
|
98
|
+
directory = File.join public_directory, directory
|
99
|
+
|
100
|
+
return directory if File.exist?(directory)
|
101
|
+
end
|
102
|
+
|
103
|
+
nil
|
104
|
+
end
|
54
105
|
end
|
55
106
|
|
56
107
|
register Pages
|
data/spec/pages_spec.rb
CHANGED
@@ -8,12 +8,13 @@ describe Sinatra::Pages do
|
|
8
8
|
TestApp
|
9
9
|
end
|
10
10
|
|
11
|
-
context '
|
11
|
+
context 'Directories settings' do
|
12
12
|
context 'by default' do
|
13
13
|
subject {app}
|
14
14
|
its(:root) {should == Dir.pwd}
|
15
15
|
its(:public) {should == File.join(Dir.pwd, 'public')}
|
16
16
|
its(:views) {should == File.join(Dir.pwd, 'views')}
|
17
|
+
its(:pages) {should == File.join(Dir.pwd, 'pages')}
|
17
18
|
its(:static) {should == true}
|
18
19
|
end
|
19
20
|
|
@@ -25,6 +26,7 @@ describe Sinatra::Pages do
|
|
25
26
|
its(:root) {should == File.dirname(__FILE__)}
|
26
27
|
its(:public) {should == File.join(File.dirname(__FILE__), 'public')}
|
27
28
|
its(:views) {should == File.join(File.dirname(__FILE__), 'views')}
|
29
|
+
its(:pages) {should == File.join(File.dirname(__FILE__), 'pages')}
|
28
30
|
its(:static) {should == true}
|
29
31
|
end
|
30
32
|
|
@@ -33,6 +35,7 @@ describe Sinatra::Pages do
|
|
33
35
|
its(:root) {should == Dir.pwd}
|
34
36
|
its(:public) {should == File.join(File.dirname(__FILE__), 'public')}
|
35
37
|
its(:views) {should == File.join(Dir.pwd, 'views')}
|
38
|
+
its(:pages) {should == File.join(Dir.pwd, 'pages')}
|
36
39
|
its(:static) {should == true}
|
37
40
|
end
|
38
41
|
|
@@ -41,6 +44,17 @@ describe Sinatra::Pages do
|
|
41
44
|
its(:root) {should == Dir.pwd}
|
42
45
|
its(:public) {should == File.join(Dir.pwd, 'public')}
|
43
46
|
its(:views) {should == File.join(File.dirname(__FILE__), 'views')}
|
47
|
+
its(:pages) {should == File.join(Dir.pwd, 'pages')}
|
48
|
+
its(:static) {should == true}
|
49
|
+
end
|
50
|
+
|
51
|
+
context '#pages' do
|
52
|
+
before {app.set :root, Dir.pwd}
|
53
|
+
subject {app.set :pages, File.join(File.dirname(__FILE__), 'pages')}
|
54
|
+
its(:root) {should == Dir.pwd}
|
55
|
+
its(:public) {should == File.join(Dir.pwd, 'public')}
|
56
|
+
its(:views) {should == File.join(Dir.pwd, 'views')}
|
57
|
+
its(:pages) {should == File.join(File.dirname(__FILE__), 'pages')}
|
44
58
|
its(:static) {should == true}
|
45
59
|
end
|
46
60
|
end
|
@@ -62,25 +76,78 @@ describe Sinatra::Pages do
|
|
62
76
|
app.disable :escaping
|
63
77
|
end
|
64
78
|
|
65
|
-
[:v4, :vX].each do |
|
79
|
+
[:v4, :vX].each do |html|
|
66
80
|
context '#html' do
|
67
|
-
subject {app.set :html,
|
68
|
-
its(:html) {should ==
|
69
|
-
it {subject.haml[:format].should == (
|
81
|
+
subject {app.set :html, html}
|
82
|
+
its(:html) {should == html}
|
83
|
+
it {subject.haml[:format].should == (html == :v4 ? :html4 : :xhtml)}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
[:tidy, :ugly].each do |format|
|
88
|
+
context '#format' do
|
89
|
+
subject {app.set :format, format}
|
90
|
+
its(:format) {should == format}
|
91
|
+
it {subject.haml[:ugly].should == (format == :ugly ? true : false)}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
[true, false].each do |escaping|
|
96
|
+
context '#escaping' do
|
97
|
+
#subject {app.enable :escaping}
|
98
|
+
subject {app.set :escaping, escaping}
|
99
|
+
its(:escaping) {should == escaping}
|
100
|
+
it {subject.haml[:escape_html].should == escaping}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'SASS settings' do
|
107
|
+
context 'by default' do
|
108
|
+
subject {app}
|
109
|
+
its(:stylesheet) {should == :scss}
|
110
|
+
its(:format) {should == :tidy}
|
111
|
+
its(:cache) {should == :write}
|
112
|
+
its(:sass) {should == {:style => :expanded, :syntax => :scss, :cache => true}}
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'on defining' do
|
116
|
+
before do
|
117
|
+
app.set :stylesheet, :css
|
118
|
+
app.set :format, :tidy
|
119
|
+
app.set :cache, :write
|
120
|
+
end
|
121
|
+
|
122
|
+
[:css, :sass, :scss].each do |stylesheet|
|
123
|
+
context '#stylesheet' do
|
124
|
+
subject {app.set :stylesheet, stylesheet}
|
125
|
+
its(:stylesheet) {should == stylesheet}
|
126
|
+
it {subject.sass[:syntax].should == stylesheet}
|
70
127
|
end
|
71
128
|
end
|
72
129
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
130
|
+
[:tidy, :ugly].each do |format|
|
131
|
+
context '#format' do
|
132
|
+
subject {app.set :format, format}
|
133
|
+
its(:format) {should == format}
|
134
|
+
format == :tidy ? it {subject.sass[:style].should == :expanded} :
|
135
|
+
it {subject.sass[:style].should == :compressed}
|
136
|
+
end
|
77
137
|
end
|
78
138
|
|
79
|
-
|
80
|
-
#
|
81
|
-
|
82
|
-
|
83
|
-
|
139
|
+
[:write, :read].each do |cache|
|
140
|
+
context '#cache' do
|
141
|
+
subject {app.set :cache, cache}
|
142
|
+
its(:cache) {should == cache}
|
143
|
+
if cache == :write
|
144
|
+
it {subject.sass[:cache].should == true}
|
145
|
+
it {subject.sass[:read_cache].should == nil}
|
146
|
+
else
|
147
|
+
it {subject.sass[:cache].should == nil}
|
148
|
+
it {subject.sass[:read_cache].should == true}
|
149
|
+
end
|
150
|
+
end
|
84
151
|
end
|
85
152
|
end
|
86
153
|
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: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Julio Javier Cicchelli
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-28 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -35,9 +35,25 @@ dependencies:
|
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: tilt
|
39
39
|
prerelease: false
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 21
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 1
|
50
|
+
version: 1.0.1
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: haml
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
57
|
none: false
|
42
58
|
requirements:
|
43
59
|
- - ">="
|
@@ -49,11 +65,11 @@ dependencies:
|
|
49
65
|
- 15
|
50
66
|
version: 3.0.15
|
51
67
|
type: :runtime
|
52
|
-
version_requirements: *
|
68
|
+
version_requirements: *id003
|
53
69
|
- !ruby/object:Gem::Dependency
|
54
70
|
name: rspec
|
55
71
|
prerelease: false
|
56
|
-
requirement: &
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
57
73
|
none: false
|
58
74
|
requirements:
|
59
75
|
- - ">="
|
@@ -65,11 +81,11 @@ dependencies:
|
|
65
81
|
- 0
|
66
82
|
version: 1.3.0
|
67
83
|
type: :development
|
68
|
-
version_requirements: *
|
84
|
+
version_requirements: *id004
|
69
85
|
- !ruby/object:Gem::Dependency
|
70
86
|
name: rcov
|
71
87
|
prerelease: false
|
72
|
-
requirement: &
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
73
89
|
none: false
|
74
90
|
requirements:
|
75
91
|
- - ">="
|
@@ -81,11 +97,11 @@ dependencies:
|
|
81
97
|
- 8
|
82
98
|
version: 0.9.8
|
83
99
|
type: :development
|
84
|
-
version_requirements: *
|
100
|
+
version_requirements: *id005
|
85
101
|
- !ruby/object:Gem::Dependency
|
86
102
|
name: rack-test
|
87
103
|
prerelease: false
|
88
|
-
requirement: &
|
104
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
89
105
|
none: false
|
90
106
|
requirements:
|
91
107
|
- - ">="
|
@@ -97,7 +113,7 @@ dependencies:
|
|
97
113
|
- 4
|
98
114
|
version: 0.5.4
|
99
115
|
type: :development
|
100
|
-
version_requirements: *
|
116
|
+
version_requirements: *id006
|
101
117
|
description: " A Sinatra extension for static pages rendering using the HAML rendering engine.\n"
|
102
118
|
email: javier@rock-n-code.com
|
103
119
|
executables: []
|