sinatra-static-assets 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.markdown +202 -0
- data/VERSION.yml +4 -0
- data/examples/rconfig.ru +13 -0
- data/examples/rsummer/config.ru +8 -0
- data/examples/rsummer/public/images/tatry1.jpg +0 -0
- data/examples/rsummer/public/javascripts/mapp.js +1 -0
- data/examples/rsummer/public/stylesheets/mapp.css +14 -0
- data/examples/rsummer/public/stylesheets/src/background.png +0 -0
- data/examples/rsummer/summer.rb +21 -0
- data/examples/rsummer/tmp/always_restart.txt +0 -0
- data/examples/rsummer/views/index.erb +3 -0
- data/examples/rsummer/views/layout.erb +17 -0
- data/examples/rwinter/config.ru +8 -0
- data/examples/rwinter/public/images/tatry2.jpg +0 -0
- data/examples/rwinter/public/javascripts/mapp.js +1 -0
- data/examples/rwinter/public/stylesheets/mapp.css +14 -0
- data/examples/rwinter/public/stylesheets/src/background.png +0 -0
- data/examples/rwinter/tmp/always_restart.txt +0 -0
- data/examples/rwinter/views/index.erb +3 -0
- data/examples/rwinter/views/layout.erb +17 -0
- data/examples/rwinter/winter.rb +21 -0
- data/examples/summer/config.ru +4 -0
- data/examples/summer/public/images/tatry1.jpg +0 -0
- data/examples/summer/public/javascripts/app.js +1 -0
- data/examples/summer/public/stylesheets/app.css +14 -0
- data/examples/summer/public/stylesheets/src/bronzed_olive.png +0 -0
- data/examples/summer/summer.rb +16 -0
- data/examples/summer/tmp/always_restart.txt +0 -0
- data/examples/summer/views/index.erb +6 -0
- data/examples/summer/views/layout.erb +16 -0
- data/examples/summer/views/topr.erb +5 -0
- data/examples/winter/config.ru +4 -0
- data/examples/winter/public/images/tatry2.jpg +0 -0
- data/examples/winter/public/javascripts/app.js +1 -0
- data/examples/winter/public/stylesheets/app.css +18 -0
- data/examples/winter/public/stylesheets/src/skating.png +0 -0
- data/examples/winter/tmp/always_restart.txt +0 -0
- data/examples/winter/views/index.erb +5 -0
- data/examples/winter/views/layout.erb +16 -0
- data/examples/winter/winter.rb +12 -0
- data/lib/sinatra/static_assets.rb +69 -0
- data/test/sinatra_app.rb +45 -0
- data/test/sinatra_static_assets_test.rb +57 -0
- metadata +148 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2009 Wlodek Bzyl
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
# Sinatra Extension: StaticAssets
|
2
|
+
|
3
|
+
Gem *sinatra-static-assets* implements the following helpers methods:
|
4
|
+
|
5
|
+
* `image_tag`
|
6
|
+
* `stylesheet_link_tag`
|
7
|
+
* `javascript_script_tag`
|
8
|
+
* `link_to`
|
9
|
+
|
10
|
+
To install it, run:
|
11
|
+
|
12
|
+
sudo gem install sinatra-static-assets -s http://gemcutter.com
|
13
|
+
|
14
|
+
All these methods are simple wrappers around the `url_for` method
|
15
|
+
from the [sinatra-url-for](http://github.com/emk/sinatra-url-for/) gem.
|
16
|
+
|
17
|
+
## When will you need it?
|
18
|
+
|
19
|
+
Whenever you use the
|
20
|
+
[Passenger module for Apache2](http://www.modrails.com/documentation/Users%20guide%20Apache.html#deploying_rack_to_sub_uri)
|
21
|
+
or use `Rack::URLMap` to dispatch an application to
|
22
|
+
sub URI.
|
23
|
+
|
24
|
+
Example: Suppose that we already have a virtual host `hitch.local`
|
25
|
+
and two Sinatra applications that live in
|
26
|
+
`/home/me/www/summer` and `/home/me/www/winter`
|
27
|
+
directories, respectively.
|
28
|
+
We want our Sinatra applications to be accessible from
|
29
|
+
the following sub URI:
|
30
|
+
|
31
|
+
http://hitch.local/summer
|
32
|
+
|
33
|
+
and
|
34
|
+
|
35
|
+
http://hitch.local/winter
|
36
|
+
|
37
|
+
To configure Apache2 and Passenger to serve our applications
|
38
|
+
we need to create a new configuration file with the following content:
|
39
|
+
|
40
|
+
<VirtualHost *:80>
|
41
|
+
ServerName hitch.local
|
42
|
+
DocumentRoot /srv/www/hitch.local
|
43
|
+
|
44
|
+
RackBaseURI /summer
|
45
|
+
RackBaseURI /winter
|
46
|
+
</VirtualHost>
|
47
|
+
|
48
|
+
and a link to the applications directories in `/srv/www/hitch.local`:
|
49
|
+
|
50
|
+
ln -s /home/me/www/summer/public /srv/www/hitch.local/summer
|
51
|
+
ln -s /home/me/www/winter/public /srv/www/hitch.local/winter
|
52
|
+
|
53
|
+
After restarting an Apache2 server and visiting, for example, the first
|
54
|
+
application at `http://hitch.local/summer` we see that links to
|
55
|
+
images, stylesheets and javascripts are broken.
|
56
|
+
|
57
|
+
The hitch here is that in Sinatra applications we usually refer to
|
58
|
+
images/stylesheets/javascripts with absolute URI:
|
59
|
+
|
60
|
+
/images/tatry1.jpg /stylesheets/app.css /javascripts/app.js
|
61
|
+
|
62
|
+
That setup **works** whenever we are running applications locally.
|
63
|
+
The absolute URI above tells a browser to request images
|
64
|
+
(stylesheets and javascripts) from:
|
65
|
+
|
66
|
+
http://localhost:4567/images/tatry1.jpg
|
67
|
+
|
68
|
+
which in turn, tells a server to send a file:
|
69
|
+
|
70
|
+
/home/me/www/summer/public/images/tatry1.jpg
|
71
|
+
|
72
|
+
The `public` directory is the default directory where static files
|
73
|
+
should be served from.
|
74
|
+
So, the `/images/tatry1.jpg` picture will be there and will be served
|
75
|
+
unless we had changed that default directory.
|
76
|
+
|
77
|
+
But these absolute URIs do not work when, for example,
|
78
|
+
the *summer* application is dispatched to `/summer` sub URI.
|
79
|
+
As a result the images are at:
|
80
|
+
|
81
|
+
http://hitch.local/summer/images/tatry1.jpg
|
82
|
+
|
83
|
+
but we request them from:
|
84
|
+
|
85
|
+
http://hitch.local/images/tatry1.jpg
|
86
|
+
|
87
|
+
And this **does not work** because there is no application
|
88
|
+
dispatched to *images* sub URI.
|
89
|
+
|
90
|
+
The recommended way to deal with an absolute URI
|
91
|
+
is to use a helper method that automatically converts
|
92
|
+
`/images/tatry1.jpg` to `/summer/images/tatry1.jpg`
|
93
|
+
for application dispatched to `/summer` sub URI.
|
94
|
+
|
95
|
+
In the above example you can simply remove the `<img>`
|
96
|
+
HTML tag and replace it with a Ruby inline code like this:
|
97
|
+
|
98
|
+
<%= image_tag("/images/tatry1.jpg", :alt => "Błyszcz, 2159 m") %>
|
99
|
+
|
100
|
+
See also, [How to fix broken images/CSS/JavaScript URIs in sub-URI
|
101
|
+
deployments](http://www.modrails.com/documentation/Users%20guide%20Apache.html#sub_uri_deployment_uri_fix)
|
102
|
+
|
103
|
+
## Usage examples
|
104
|
+
|
105
|
+
In HTML `<link>` and `<img>` tags have no end tag.
|
106
|
+
In XHTML, on the contrary, these tags must be properly closed.
|
107
|
+
|
108
|
+
We can choose the appropriate behaviour with *closed* option:
|
109
|
+
|
110
|
+
image_tag "/images/tatry1.jpg", :alt => "Błyszcz, 2159 m", :closed => true
|
111
|
+
|
112
|
+
The default value of *closed* option is `false`.
|
113
|
+
|
114
|
+
stylesheet_link_tag "/stylesheets/screen.css", "/stylesheets/summer.css", :media => "projection"
|
115
|
+
javascript_script_tag "/javascripts/jquery.js", "/javascripts/summer.js", :charset => "iso-8859-2"
|
116
|
+
link_to "Tatry Mountains Rescue Team", "/topr"
|
117
|
+
|
118
|
+
In order to use include the following in a Sinatra application:
|
119
|
+
|
120
|
+
gem 'sinatra-static-assets'
|
121
|
+
require 'sinatra/static_assets'
|
122
|
+
|
123
|
+
Or, if subclassing `Sinatra::Base`, include helpers manually:
|
124
|
+
|
125
|
+
gem 'emk-sinatra-url-for'
|
126
|
+
require 'sinatra/url_for'
|
127
|
+
|
128
|
+
gem 'sinatra-static-assets'
|
129
|
+
require 'sinatra/static_assets'
|
130
|
+
|
131
|
+
class Summer < Sinatra::Base
|
132
|
+
helpers Sinatra::UrlForHelper
|
133
|
+
helpers Sinatra::StaticAssets
|
134
|
+
# ...
|
135
|
+
end
|
136
|
+
|
137
|
+
## Dispatching reusable Sinatra applications
|
138
|
+
|
139
|
+
With the latest version of Sinatra it is possible to build
|
140
|
+
reusable Sinatra applications. This means that multiple Sinatra applications
|
141
|
+
can be run in isolation and co-exist peacefully with other Rack
|
142
|
+
based applications. Subclassing `Sinatra::Base` creates such a
|
143
|
+
reusable application.
|
144
|
+
|
145
|
+
The `example` directory contains two reusable Sinatra applications:
|
146
|
+
*rsummer*, *rwinter* and a rackup file `rconfig.ru` which
|
147
|
+
dispatches these applications to `/summer` and `/winter` sub URI:
|
148
|
+
|
149
|
+
$LOAD_PATH.unshift('rsummer')
|
150
|
+
require 'summer'
|
151
|
+
|
152
|
+
$LOAD_PATH.unshift('rwinter')
|
153
|
+
require 'winter'
|
154
|
+
|
155
|
+
map '/summer' do
|
156
|
+
run Sinatra::Summer.new
|
157
|
+
end
|
158
|
+
|
159
|
+
map '/winter' do
|
160
|
+
run Sinatra::Winter.new
|
161
|
+
end
|
162
|
+
|
163
|
+
Run `rconfig.ru` file with:
|
164
|
+
|
165
|
+
rackup -p 3000 rconfig.ru
|
166
|
+
|
167
|
+
This file could **also** be used to deploy to virtual host's root with
|
168
|
+
Passenger.
|
169
|
+
|
170
|
+
To this end, create an Apache2 configuration file with the following
|
171
|
+
content:
|
172
|
+
|
173
|
+
<VirtualHost *:80>
|
174
|
+
ServerName hitch.local
|
175
|
+
DocumentRoot /srv/www/hitch.local
|
176
|
+
</VirtualHost>
|
177
|
+
|
178
|
+
Next, create directories required by Passenger:
|
179
|
+
|
180
|
+
mkdir /srv/www/hitch.local/{public,tmp}
|
181
|
+
|
182
|
+
and, finally, copy `config.ru` into `/srv/www/hitch.local` and
|
183
|
+
update `LOAD_PATH` in the copied file.
|
184
|
+
|
185
|
+
With everything in place, after restarting Apache2,
|
186
|
+
the applications are accessible from the
|
187
|
+
|
188
|
+
http://hitch.local/summer
|
189
|
+
|
190
|
+
and
|
191
|
+
|
192
|
+
http://hitch.local/winter
|
193
|
+
|
194
|
+
respectively.
|
195
|
+
|
196
|
+
## Miscellaneous stuff
|
197
|
+
|
198
|
+
1\. The `examples` directory contains *summer* and *winter* applications.
|
199
|
+
|
200
|
+
2\. In order to create a virual host add the following to */etc/hosts/*:
|
201
|
+
|
202
|
+
127.0.0.1 localhost.localdomain localhost hitch.local
|
data/VERSION.yml
ADDED
data/examples/rconfig.ru
ADDED
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
/* mapp1 */
|
@@ -0,0 +1,14 @@
|
|
1
|
+
html {
|
2
|
+
margin: 0;
|
3
|
+
padding: 0;
|
4
|
+
background: #ABA418 url(src/background.png);
|
5
|
+
}
|
6
|
+
|
7
|
+
body {
|
8
|
+
width: 600px;
|
9
|
+
margin: 1em auto;
|
10
|
+
padding: 1em 2em;
|
11
|
+
border: black solid 1px;
|
12
|
+
background-color: #D1C704;
|
13
|
+
font: normal 14px/1.6 Arial, Helvetica, sans-serif;
|
14
|
+
}
|
Binary file
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
|
5
|
+
gem 'sinatra-static-assets'
|
6
|
+
require 'sinatra/static_assets'
|
7
|
+
|
8
|
+
module Sinatra
|
9
|
+
class Summer < Sinatra::Base
|
10
|
+
helpers Sinatra::UrlForHelper
|
11
|
+
helpers Sinatra::StaticAssets
|
12
|
+
|
13
|
+
set :app_file, __FILE__
|
14
|
+
set :static, true
|
15
|
+
|
16
|
+
get '/?' do
|
17
|
+
@title = "Tatra Mountains, Błyszcz (2159 m)"
|
18
|
+
erb :index
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
+
|
6
|
+
<%= stylesheet_link_tag "/stylesheets/mapp.css" %>
|
7
|
+
<%= javascript_script_tag "/javascripts/mapp.js" %>
|
8
|
+
|
9
|
+
<title><%= @title %></title>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
|
14
|
+
<%= yield %>
|
15
|
+
|
16
|
+
</body>
|
17
|
+
</html>
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
/* mapp.js */
|
@@ -0,0 +1,14 @@
|
|
1
|
+
html {
|
2
|
+
margin: 0;
|
3
|
+
padding: 0;
|
4
|
+
background: #ABA418 url(src/background.png);
|
5
|
+
}
|
6
|
+
|
7
|
+
body {
|
8
|
+
width: 600px;
|
9
|
+
margin: 1em auto;
|
10
|
+
padding: 1em 2em;
|
11
|
+
border: black solid 1px;
|
12
|
+
background-color: #D8E8FF;
|
13
|
+
font: normal 14px/1.6 Arial, Helvetica, sans-serif;
|
14
|
+
}
|
Binary file
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
+
|
6
|
+
<%= stylesheet_link_tag "/stylesheets/mapp.css" %>
|
7
|
+
<%= javascript_script_tag "/javascripts/mapp.js" %>
|
8
|
+
|
9
|
+
<title><%= @title %></title>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
|
14
|
+
<%= yield %>
|
15
|
+
|
16
|
+
</body>
|
17
|
+
</html>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
|
5
|
+
gem 'sinatra-static-assets'
|
6
|
+
require 'sinatra/static_assets'
|
7
|
+
|
8
|
+
module Sinatra
|
9
|
+
class Winter < Sinatra::Base
|
10
|
+
helpers Sinatra::UrlForHelper
|
11
|
+
helpers Sinatra::StaticAssets
|
12
|
+
|
13
|
+
set :app_file, __FILE__
|
14
|
+
set :static, true
|
15
|
+
|
16
|
+
get '/?' do
|
17
|
+
@title = "Tatra Mountains, Dolina Gąsienicowa (1500 m)"
|
18
|
+
erb :index
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
/* summer: app.js */
|
@@ -0,0 +1,14 @@
|
|
1
|
+
html {
|
2
|
+
margin: 0;
|
3
|
+
padding: 0;
|
4
|
+
background: #ABA418 url(src/bronzed_olive.png);
|
5
|
+
}
|
6
|
+
|
7
|
+
body {
|
8
|
+
width: 600px;
|
9
|
+
margin: 1em auto;
|
10
|
+
padding: 1em 2em;
|
11
|
+
border: black solid 1px;
|
12
|
+
background-color: #D1C704;
|
13
|
+
font: normal 14px/1.6 Arial, Helvetica, sans-serif;
|
14
|
+
}
|
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'sinatra'
|
4
|
+
|
5
|
+
gem 'sinatra-static-assets'
|
6
|
+
require 'sinatra/static_assets'
|
7
|
+
|
8
|
+
get "/?" do
|
9
|
+
@title = "Tatra Mountains, Błyszcz (2159 m)"
|
10
|
+
erb :index
|
11
|
+
end
|
12
|
+
|
13
|
+
get "/:page" do
|
14
|
+
@title = "#{params[:page]}"
|
15
|
+
erb :"#{params[:page]}"
|
16
|
+
end
|
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
+
<%= stylesheet_link_tag "/stylesheets/app.css" %>
|
6
|
+
<%= javascript_script_tag "/javascripts/app.js" %>
|
7
|
+
|
8
|
+
<title><%= @title %></title>
|
9
|
+
</head>
|
10
|
+
|
11
|
+
<body>
|
12
|
+
|
13
|
+
<%= yield %>
|
14
|
+
|
15
|
+
</body>
|
16
|
+
</html>
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
/* app1.js */
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/*
|
2
|
+
Theme for app2: http://www.colourlovers.com/palette/81363/rainy
|
3
|
+
*/
|
4
|
+
|
5
|
+
html {
|
6
|
+
margin: 0;
|
7
|
+
padding: 0;
|
8
|
+
background: #659DF1 url(src/skating.png);
|
9
|
+
}
|
10
|
+
|
11
|
+
body {
|
12
|
+
width: 600px;
|
13
|
+
margin: 1em auto;
|
14
|
+
padding: 1em 2em;
|
15
|
+
border: black solid 1px;
|
16
|
+
background-color: #D8E8FF;
|
17
|
+
font: normal 14px/1.6 Arial, Helvetica, sans-serif;
|
18
|
+
}
|
Binary file
|
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
+
<%= stylesheet_link_tag "/stylesheets/app.css" %>
|
6
|
+
<%= javascript_script_tag "/javascripts/app.js" %>
|
7
|
+
|
8
|
+
<title><%= @title %></title>
|
9
|
+
</head>
|
10
|
+
|
11
|
+
<body>
|
12
|
+
|
13
|
+
<%= yield %>
|
14
|
+
|
15
|
+
</body>
|
16
|
+
</html>
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra/url_for'
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
module StaticAssets
|
6
|
+
# In HTML <link> and <img> tags have no end tag.
|
7
|
+
# In XHTML, on the contrary, these tags must be properly closed.
|
8
|
+
#
|
9
|
+
# We can choose the appropriate behaviour with +closed+ option:
|
10
|
+
#
|
11
|
+
# image_tag "/images/foo.png", :alt => "Foo itself", :closed => true
|
12
|
+
#
|
13
|
+
# The default value of +closed+ option is +false+.
|
14
|
+
#
|
15
|
+
def image_tag(source, options = {})
|
16
|
+
closed = options.delete(:closed)
|
17
|
+
options[:src] = url_for(source)
|
18
|
+
tag("img", options, closed)
|
19
|
+
end
|
20
|
+
|
21
|
+
def stylesheet_link_tag(*sources)
|
22
|
+
list, options = extract_options(sources)
|
23
|
+
closed = options.delete(:closed)
|
24
|
+
list.collect { |source| stylesheet_tag(source, options, closed) }.join("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
def javascript_script_tag(*sources)
|
28
|
+
list, options = extract_options(sources)
|
29
|
+
list.collect { |source| javascript_tag(source, options) }.join("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
def link_to(desc, url)
|
33
|
+
"<a href='#{url_for(url)}'>#{desc}</a>"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def tag(name, options = {}, closed = false)
|
39
|
+
"<#{name}#{tag_options(options) if options}#{closed ? " />" : ">"}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def tag_options(options)
|
43
|
+
unless options.empty?
|
44
|
+
attrs = []
|
45
|
+
attrs = options.map { |key, value| %(#{key}="#{value}") }
|
46
|
+
" #{attrs.sort * ' '}" unless attrs.empty?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def stylesheet_tag(source, options, closed = false)
|
51
|
+
tag("link", { :type => "text/css",
|
52
|
+
:charset => "utf-8", :media => "screen", :rel => "stylesheet",
|
53
|
+
:href => url_for(source) }.merge(options), closed)
|
54
|
+
end
|
55
|
+
|
56
|
+
def javascript_tag(source, options)
|
57
|
+
tag("script", { :type => "text/javascript", :charset => "utf-8",
|
58
|
+
:src => url_for(source) }.merge(options), false) + "</script>"
|
59
|
+
end
|
60
|
+
|
61
|
+
def extract_options(a)
|
62
|
+
opts = a.last.is_a?(::Hash) ? a.pop : {}
|
63
|
+
[a, opts]
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
helpers StaticAssets
|
69
|
+
end
|
data/test/sinatra_app.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
path = File.expand_path("../lib" + File.dirname(__FILE__))
|
2
|
+
$:.unshift(path) unless $:.include?(path)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
require 'sinatra'
|
7
|
+
require 'sinatra/url_for'
|
8
|
+
require 'sinatra/static_assets'
|
9
|
+
|
10
|
+
get "/url_for" do
|
11
|
+
content_type "text/plain"
|
12
|
+
<<"EOD"
|
13
|
+
#{url_for("/")}
|
14
|
+
#{url_for("/foo")}
|
15
|
+
#{url_for("/foo", :full)}
|
16
|
+
EOD
|
17
|
+
end
|
18
|
+
|
19
|
+
get "/image_tag" do
|
20
|
+
content_type "text/plain"
|
21
|
+
<<"EOD"
|
22
|
+
#{image_tag("/images/foo.jpg", :alt => "[foo image]")}
|
23
|
+
EOD
|
24
|
+
end
|
25
|
+
|
26
|
+
get "/stylesheet_link_tag" do
|
27
|
+
content_type "text/plain"
|
28
|
+
<<"EOD"
|
29
|
+
#{stylesheet_link_tag("/stylesheets/winter.css", "/stylesheets/summer.css", :media => "projection")}
|
30
|
+
EOD
|
31
|
+
end
|
32
|
+
|
33
|
+
get "/javascript_script_tag" do
|
34
|
+
content_type "text/plain"
|
35
|
+
<<"EOD"
|
36
|
+
#{javascript_script_tag "/javascripts/summer.js", :charset => "iso-8859-2"}
|
37
|
+
EOD
|
38
|
+
end
|
39
|
+
|
40
|
+
get "/link_to_tag" do
|
41
|
+
content_type "text/plain"
|
42
|
+
<<"EOD"
|
43
|
+
#{link_to "Tatry Mountains Rescue Team", "/topr"}
|
44
|
+
EOD
|
45
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'sinatra_app'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rack/test'
|
4
|
+
|
5
|
+
set :environment, :test
|
6
|
+
|
7
|
+
class SintraStaticAssetsTest < Test::Unit::TestCase
|
8
|
+
include Rack::Test::Methods
|
9
|
+
|
10
|
+
def app
|
11
|
+
Sinatra::Application
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_url_for_returns_absolute_paths_and_full_urls
|
15
|
+
get '/url_for', {}, 'SCRIPT_NAME' => '/bar'
|
16
|
+
assert last_response.ok?
|
17
|
+
assert_equal last_response.body, <<EOD
|
18
|
+
/bar/
|
19
|
+
/bar/foo
|
20
|
+
http://example.org/bar/foo
|
21
|
+
EOD
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_image_tag_returns_sub_uri
|
25
|
+
get '/image_tag', {}, 'SCRIPT_NAME' => '/bar'
|
26
|
+
assert last_response.ok?
|
27
|
+
assert_equal last_response.body, <<EOD
|
28
|
+
<img alt="[foo image]" src="/bar/images/foo.jpg">
|
29
|
+
EOD
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_stylesheet_link_tag_returns_sub_uri
|
33
|
+
get '/stylesheet_link_tag', {}, 'SCRIPT_NAME' => '/bar'
|
34
|
+
assert last_response.ok?
|
35
|
+
assert_equal last_response.body, <<EOD
|
36
|
+
<link charset="utf-8" href="/bar/stylesheets/winter.css" media="projection" rel="stylesheet" type="text/css">
|
37
|
+
<link charset="utf-8" href="/bar/stylesheets/summer.css" media="projection" rel="stylesheet" type="text/css">
|
38
|
+
EOD
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_javascript_script_tag_returns_sub_uri
|
42
|
+
get '/javascript_script_tag', {}, 'SCRIPT_NAME' => '/bar'
|
43
|
+
assert last_response.ok?
|
44
|
+
assert_equal last_response.body, <<EOD
|
45
|
+
<script charset="iso-8859-2" src="/bar/javascripts/summer.js" type="text/javascript"></script>
|
46
|
+
EOD
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_link_to_tag_returns_sub_uri
|
50
|
+
get '/link_to_tag', {}, 'SCRIPT_NAME' => '/bar'
|
51
|
+
assert last_response.ok?
|
52
|
+
assert_equal last_response.body, <<EOD
|
53
|
+
<a href='/bar/topr'>Tatry Mountains Rescue Team</a>
|
54
|
+
EOD
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-static-assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wlodek Bzyl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-19 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sinatra
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.1
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: emk-sinatra-url-for
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.2.1
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rack-test
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.3.0
|
54
|
+
version:
|
55
|
+
description: |
|
56
|
+
This Sinatra extensions provides following helper methods:
|
57
|
+
- image_tag
|
58
|
+
- stylesheet_link_tag
|
59
|
+
- javascript_script_tag
|
60
|
+
|
61
|
+
email: matwb@univ.gda.pl
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files:
|
67
|
+
- LICENSE
|
68
|
+
- README.markdown
|
69
|
+
files:
|
70
|
+
- VERSION.yml
|
71
|
+
- examples/rconfig.ru
|
72
|
+
- examples/rsummer/config.ru
|
73
|
+
- examples/rsummer/public/images/tatry1.jpg
|
74
|
+
- examples/rsummer/public/javascripts/mapp.js
|
75
|
+
- examples/rsummer/public/stylesheets/mapp.css
|
76
|
+
- examples/rsummer/public/stylesheets/src/background.png
|
77
|
+
- examples/rsummer/summer.rb
|
78
|
+
- examples/rsummer/tmp/always_restart.txt
|
79
|
+
- examples/rsummer/views/index.erb
|
80
|
+
- examples/rsummer/views/layout.erb
|
81
|
+
- examples/rwinter/config.ru
|
82
|
+
- examples/rwinter/public/images/tatry2.jpg
|
83
|
+
- examples/rwinter/public/javascripts/mapp.js
|
84
|
+
- examples/rwinter/public/stylesheets/mapp.css
|
85
|
+
- examples/rwinter/public/stylesheets/src/background.png
|
86
|
+
- examples/rwinter/tmp/always_restart.txt
|
87
|
+
- examples/rwinter/views/index.erb
|
88
|
+
- examples/rwinter/views/layout.erb
|
89
|
+
- examples/rwinter/winter.rb
|
90
|
+
- examples/summer/config.ru
|
91
|
+
- examples/summer/public/images/tatry1.jpg
|
92
|
+
- examples/summer/public/javascripts/app.js
|
93
|
+
- examples/summer/public/stylesheets/app.css
|
94
|
+
- examples/summer/public/stylesheets/src/bronzed_olive.png
|
95
|
+
- examples/summer/summer.rb
|
96
|
+
- examples/summer/tmp/always_restart.txt
|
97
|
+
- examples/summer/views/index.erb
|
98
|
+
- examples/summer/views/layout.erb
|
99
|
+
- examples/summer/views/topr.erb
|
100
|
+
- examples/winter/config.ru
|
101
|
+
- examples/winter/public/images/tatry2.jpg
|
102
|
+
- examples/winter/public/javascripts/app.js
|
103
|
+
- examples/winter/public/stylesheets/app.css
|
104
|
+
- examples/winter/public/stylesheets/src/skating.png
|
105
|
+
- examples/winter/tmp/always_restart.txt
|
106
|
+
- examples/winter/views/index.erb
|
107
|
+
- examples/winter/views/layout.erb
|
108
|
+
- examples/winter/winter.rb
|
109
|
+
- lib/sinatra/static_assets.rb
|
110
|
+
- test/sinatra_app.rb
|
111
|
+
- test/sinatra_static_assets_test.rb
|
112
|
+
- LICENSE
|
113
|
+
- README.markdown
|
114
|
+
has_rdoc: true
|
115
|
+
homepage: http://github.com/wbzyl/sinatra-static-assets
|
116
|
+
licenses: []
|
117
|
+
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options:
|
120
|
+
- --charset=UTF-8
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: "0"
|
128
|
+
version:
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: "0"
|
134
|
+
version:
|
135
|
+
requirements: []
|
136
|
+
|
137
|
+
rubyforge_project: sinatra-static-assets
|
138
|
+
rubygems_version: 1.3.5
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Sinatra extension providing helper methods to output tags for static assetgemspec.
|
142
|
+
test_files:
|
143
|
+
- test/sinatra_static_assets_test.rb
|
144
|
+
- test/sinatra_app.rb
|
145
|
+
- examples/summer/summer.rb
|
146
|
+
- examples/rsummer/summer.rb
|
147
|
+
- examples/rwinter/winter.rb
|
148
|
+
- examples/winter/winter.rb
|