padrino-assets 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -4
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/.yardopts +10 -0
- data/CHANGELOG.md +11 -8
- data/Gemfile +1 -1
- data/LICENSE +18 -18
- data/README.md +126 -64
- data/Rakefile +30 -7
- data/lib/padrino-assets.rb +154 -91
- data/lib/padrino-assets/helpers.rb +445 -379
- data/lib/padrino-assets/version.rb +5 -4
- data/lib/tasks/cleanup.rake +8 -7
- data/lib/tasks/clobber.rake +7 -6
- data/lib/tasks/compress.rake +14 -13
- data/lib/tasks/precompile.rake +32 -29
- data/padrino-assets.gemspec +25 -24
- data/spec/assets_spec.rb +61 -0
- data/spec/fixtures/assets/application.css +11 -0
- data/spec/fixtures/assets/application.js +1 -0
- data/spec/fixtures/assets/pony.jpg +0 -0
- data/spec/fixtures/compiled_assets/application-b8588c6975a4539fbf2c11471870bdca.css +11 -0
- data/spec/fixtures/compiled_assets/manifest.json +1 -0
- data/spec/helpers_spec.rb +293 -0
- data/spec/spec.rb +37 -0
- data/spec/tasks_spec.rb +114 -0
- metadata +39 -21
- data/examples/nginx.md +0 -11
- data/test/test.rb +0 -52
- data/test/test_helpers.rb +0 -339
@@ -1,379 +1,445 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# @param [
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# # => <link href="
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# @
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
# @
|
199
|
-
#
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
228
|
-
#
|
229
|
-
#
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
236
|
-
#
|
237
|
-
#
|
238
|
-
#
|
239
|
-
#
|
240
|
-
#
|
241
|
-
#
|
242
|
-
#
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
# # =>
|
256
|
-
#
|
257
|
-
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
#
|
261
|
-
#
|
262
|
-
#
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
#
|
288
|
-
#
|
289
|
-
# @
|
290
|
-
#
|
291
|
-
#
|
292
|
-
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
#
|
297
|
-
#
|
298
|
-
#
|
299
|
-
#
|
300
|
-
#
|
301
|
-
#
|
302
|
-
#
|
303
|
-
#
|
304
|
-
#
|
305
|
-
#
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
#
|
314
|
-
#
|
315
|
-
#
|
316
|
-
#
|
317
|
-
#
|
318
|
-
#
|
319
|
-
#
|
320
|
-
#
|
321
|
-
#
|
322
|
-
#
|
323
|
-
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
#
|
332
|
-
# # =>
|
333
|
-
#
|
334
|
-
#
|
335
|
-
# @
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
#
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
#
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
#
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
module Padrino
|
3
|
+
module Assets
|
4
|
+
module Helpers
|
5
|
+
URI_REGEXP = %r(^[a-z]+://|^/)
|
6
|
+
###
|
7
|
+
# Returns an HTML stylesheet link tag for the specified sources
|
8
|
+
#
|
9
|
+
# @overload stylesheet(sources)
|
10
|
+
# @param [Array<String, Symbol>] sources
|
11
|
+
# Asset sources
|
12
|
+
#
|
13
|
+
# @overload stylesheet(sources, options)
|
14
|
+
# @param [Array<String, Symbol>] sources
|
15
|
+
# Asset sources
|
16
|
+
# @param [Hash] options
|
17
|
+
# The HTML options to include in this stylesheet
|
18
|
+
#
|
19
|
+
# @option options [String] :media ('screen')
|
20
|
+
# Specifies the type of device the linked document is optimized for
|
21
|
+
#
|
22
|
+
# @return [String]
|
23
|
+
# Generated HTML for sources with specified options
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# stylesheet(:application)
|
27
|
+
# # => <link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css">
|
28
|
+
#
|
29
|
+
# stylesheets(:application, :theme)
|
30
|
+
# # => <link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css">
|
31
|
+
# # => <link href="/assets/theme.css" media="screen" rel="stylesheet" type="text/css">
|
32
|
+
#
|
33
|
+
# stylesheet(:handheld, media: 'handheld')
|
34
|
+
# # => <link href="/assets/handheld.css" media="handheld" rel="stylesheet" type="text/css">
|
35
|
+
#
|
36
|
+
# stylesheet('http://www.example.com/style.css')
|
37
|
+
# # => <link href="http://www.example.com/style.css" media="screen" rel="stylesheet" type="text/css">
|
38
|
+
#
|
39
|
+
# @since 0.1.0
|
40
|
+
# @api public
|
41
|
+
def stylesheet(*sources)
|
42
|
+
options = sources.extract_options!.symbolize_keys
|
43
|
+
options.reverse_merge!(media: 'screen', rel: 'stylesheet', type: 'text/css')
|
44
|
+
sources.collect do |source|
|
45
|
+
tag(:link, options.reverse_merge(href: asset_path(source, :css)))
|
46
|
+
end.join("\n")
|
47
|
+
end
|
48
|
+
alias_method :stylesheets, :stylesheet
|
49
|
+
alias_method :include_stylesheet, :stylesheet
|
50
|
+
alias_method :include_stylesheets, :stylesheet
|
51
|
+
alias_method :stylesheet_link_tag, :stylesheet
|
52
|
+
|
53
|
+
###
|
54
|
+
# Returns an HTML script tag for the specified sources
|
55
|
+
#
|
56
|
+
# @overload javascript(sources)
|
57
|
+
# @param [Array<String, Symbol>] source
|
58
|
+
# Asset sources
|
59
|
+
#
|
60
|
+
# @overload javascript(sources, options)
|
61
|
+
# @param [Array<String, Symbol>] sources
|
62
|
+
# Asset sources
|
63
|
+
# @param [Hash] options
|
64
|
+
# The HTML options to include in this script tag
|
65
|
+
#
|
66
|
+
# @return [String]
|
67
|
+
# Generated HTML for sources with specified options
|
68
|
+
#
|
69
|
+
# @example
|
70
|
+
# javascript(:jquery)
|
71
|
+
# # => <script type="text/javascript" src="/assets/jquery.js"></script>
|
72
|
+
#
|
73
|
+
# javascripts(:jquery, :application)
|
74
|
+
# # => <script type="text/javascript" src="/assets/jquery.js"></script>
|
75
|
+
# # => <script type="text/javascript" src="/assets/application.js"></script>
|
76
|
+
#
|
77
|
+
# javascript('http://www.example.com/application.js')
|
78
|
+
# # => <script type="text/javascript" src="http://www.example.com/application.js"></script>
|
79
|
+
#
|
80
|
+
# @since 0.1.0
|
81
|
+
# @api public
|
82
|
+
def javascript(*sources)
|
83
|
+
options = sources.extract_options!.symbolize_keys
|
84
|
+
options.reverse_merge!(type: 'text/javascript')
|
85
|
+
sources.collect do |source|
|
86
|
+
content_tag(:script, nil, options.reverse_merge(src: asset_path(source, :js)))
|
87
|
+
end.join("\n")
|
88
|
+
end
|
89
|
+
alias_method :javascripts, :javascript
|
90
|
+
alias_method :include_javascript, :javascript
|
91
|
+
alias_method :include_javascripts, :javascript
|
92
|
+
alias_method :javascript_include_tag, :javascript
|
93
|
+
|
94
|
+
###
|
95
|
+
# Returns an HTML image element for the specified sources
|
96
|
+
#
|
97
|
+
# @overload image(sources)
|
98
|
+
# @param [Array<String>] sources
|
99
|
+
# Asset sources
|
100
|
+
#
|
101
|
+
# @overload image(sources, options)
|
102
|
+
# @param [Array<String>] sources
|
103
|
+
# Asset ources
|
104
|
+
# @param [Hash] options
|
105
|
+
# The HTML options to include in this image
|
106
|
+
#
|
107
|
+
# @option options [String] :id
|
108
|
+
# Specifies a unique identifier for the image
|
109
|
+
# @option options [String] :class
|
110
|
+
# Specifies the class of the image
|
111
|
+
# @option options [String] :size
|
112
|
+
# Specifies the width and height of the image
|
113
|
+
# @option options [Integer] :width
|
114
|
+
# Specifies the width of the image
|
115
|
+
# @option options [Integer] :height
|
116
|
+
# Specifies the height of the image
|
117
|
+
# @option options [String] :alt
|
118
|
+
# Specifies an alternate text for an image
|
119
|
+
# @option options [String] :title
|
120
|
+
# Specifies the title of the image
|
121
|
+
# @option options [Boolean] :draggable
|
122
|
+
# Specifies whether or not the image is draggable (true, false, :auto)
|
123
|
+
# @option options [Boolean] :hidden
|
124
|
+
# Should the image be hidden from view
|
125
|
+
#
|
126
|
+
# @return [String]
|
127
|
+
# Generated HTML for sources with specified options
|
128
|
+
#
|
129
|
+
# @example
|
130
|
+
# image('example.png')
|
131
|
+
# # => <img src="/assets/example.png" alt="Example">
|
132
|
+
#
|
133
|
+
# image('example.png', size: '40x40')
|
134
|
+
# # => <img src="/assets/example.png" width="40" height="40" alt="Example">
|
135
|
+
#
|
136
|
+
# image('example.png', size: [40, 40])
|
137
|
+
# # => <img src="/assets/example.png" width="40" height="40" alt="Example">
|
138
|
+
#
|
139
|
+
# image('example.png', alt: 'My Little Pony')
|
140
|
+
# # => <img src="/assets/example.png" alt="My Little Pony">
|
141
|
+
#
|
142
|
+
# image('http://www.example.com/example.png')
|
143
|
+
# # => <img src="http://www.example.com/example.png" alt="Example">
|
144
|
+
#
|
145
|
+
# images('example.png', 'example.jpg')
|
146
|
+
# # => <img src="/assets/example.png" alt="Example">
|
147
|
+
# # => <img src="/assets/example.jpg" alt="Example">
|
148
|
+
#
|
149
|
+
# image('example.jpg', data: { nsfw: true, geo: [34.087, -118.407] })
|
150
|
+
# # => <img src="example.jpg" data-nsfw="true" data-geo="34.087 -118.407">
|
151
|
+
#
|
152
|
+
# @since 0.1.0
|
153
|
+
# @api public
|
154
|
+
def image(*sources)
|
155
|
+
options = sources.extract_options!.symbolize_keys
|
156
|
+
|
157
|
+
if size = options.delete(:size)
|
158
|
+
options[:width], options[:height] = size =~ /^\d+x\d+$/ ? size.split('x') : size
|
159
|
+
end
|
160
|
+
|
161
|
+
sources.collect do |source|
|
162
|
+
alternate_text = options.fetch(:alt, alternate_text(source))
|
163
|
+
tag(:img, options.reverse_merge(src: asset_path(source), alt: alternate_text))
|
164
|
+
end.join("\n")
|
165
|
+
end
|
166
|
+
alias_method :images, :image
|
167
|
+
alias_method :image_tag, :image
|
168
|
+
|
169
|
+
###
|
170
|
+
# Returns an alternate text based off the specified source
|
171
|
+
#
|
172
|
+
# @param [String] source
|
173
|
+
# Asset Source
|
174
|
+
#
|
175
|
+
# @return [String]
|
176
|
+
# Humanized alternate text
|
177
|
+
#
|
178
|
+
# @example
|
179
|
+
# alternate_text('padrino.jpg')
|
180
|
+
# # => 'Padrino'
|
181
|
+
#
|
182
|
+
# alternate_text('my_pony.jpg')
|
183
|
+
# # => 'My pony'
|
184
|
+
#
|
185
|
+
# @since 0.3.0
|
186
|
+
# @api semipublic
|
187
|
+
def alternate_text(source)
|
188
|
+
File.basename(source, '.*').humanize
|
189
|
+
end
|
190
|
+
|
191
|
+
###
|
192
|
+
# Returns an HTML video element for the specified sources
|
193
|
+
#
|
194
|
+
# @overload video(sources)
|
195
|
+
# @param [Array<String>] source
|
196
|
+
# Asset Sources
|
197
|
+
#
|
198
|
+
# @overload video(sources, options)
|
199
|
+
# @param [Array<String>] sources
|
200
|
+
# Asset Sources
|
201
|
+
# @param [Hash] options
|
202
|
+
# The HTML options to include in this video
|
203
|
+
#
|
204
|
+
# @option options [String] :id
|
205
|
+
# Specifies a unique identifier for the video
|
206
|
+
# @option options [String] :class
|
207
|
+
# Specifies the class of the video
|
208
|
+
# @option options [String] :size
|
209
|
+
# Specifies the width and height of the video
|
210
|
+
# @option options [Integer] :width
|
211
|
+
# Specifies the width of the video
|
212
|
+
# @option options [Integer] :height
|
213
|
+
# Specifies the height of the video
|
214
|
+
# @option options [String] :title
|
215
|
+
# Specifies the title of the video
|
216
|
+
# @option options [Boolean] :draggable
|
217
|
+
# Specifies whether or not the video is draggable (true, false, :auto)
|
218
|
+
# @option options [Symbol] :preload
|
219
|
+
# Specifies the method the web browser should use to preload the video (:auto, :metadata, :none)
|
220
|
+
# @option options [String] :poster
|
221
|
+
# Specifies an image to be shown while the video is downloading, or until the user hits the play button
|
222
|
+
# @option options [Boolean] :hidden
|
223
|
+
# Should the video be hidden from view
|
224
|
+
# @option options [Boolean] :controls
|
225
|
+
# Should the video controls be shown if present
|
226
|
+
# @option options [Boolean] :muted
|
227
|
+
# Should the video automatically start off muted
|
228
|
+
# @option options [Boolean] :autoplay
|
229
|
+
# Should the video automatically play when it's ready
|
230
|
+
# @option options [Boolean] :loop
|
231
|
+
# Should the video automatically loop when it's done
|
232
|
+
#
|
233
|
+
# @return [String]
|
234
|
+
# Generated HTML for sources with specified options
|
235
|
+
#
|
236
|
+
# @example
|
237
|
+
# video('example.webm')
|
238
|
+
# # => <video src="/assets/example.webm">
|
239
|
+
#
|
240
|
+
# video('example.webm', loop: true, autoplay: true)
|
241
|
+
# # => <video src="/assets/example.webm" loop="loop" autoplay="autoplay">
|
242
|
+
#
|
243
|
+
# video('example.webm', size: '40x40')
|
244
|
+
# # => <video src="/assets/example.webm" width="40" height="40">
|
245
|
+
#
|
246
|
+
# video('example.webm', size: [40, 40])
|
247
|
+
# # => <video src="/assets/example.webm" width="40" height="40">
|
248
|
+
#
|
249
|
+
# video('http://www.example.com/example.webm')
|
250
|
+
# # => <video src="http://www.example.com/example.webm">
|
251
|
+
#
|
252
|
+
# videos('example.webm', 'example.mov')
|
253
|
+
# # => <video>
|
254
|
+
# # => <source src="/assets/example.webm">
|
255
|
+
# # => <source src="/assets/example.mov">
|
256
|
+
# # => </video>
|
257
|
+
#
|
258
|
+
# video('example.webm', poster: 'preload.jpg')
|
259
|
+
# # => <video src="/assets/example.webm" poster="/assets/preload.jpg">
|
260
|
+
#
|
261
|
+
# @since 0.1.0
|
262
|
+
# @api public
|
263
|
+
def video(*sources)
|
264
|
+
options = sources.extract_options!.symbolize_keys
|
265
|
+
sources = sources.shift if sources.size == 1
|
266
|
+
|
267
|
+
if options[:poster]
|
268
|
+
options[:poster] = asset_path(options[:poster])
|
269
|
+
end
|
270
|
+
|
271
|
+
if size = options.delete(:size)
|
272
|
+
options[:width], options[:height] = size =~ /^\d+x\d+$/ ? size.split('x') : size
|
273
|
+
end
|
274
|
+
|
275
|
+
if sources.is_a?(Array)
|
276
|
+
content_tag(:video, options) do
|
277
|
+
sources.collect { |source| tag(:source, src: asset_path(source)) }.join("\n")
|
278
|
+
end
|
279
|
+
else
|
280
|
+
tag(:video, options.reverse_merge(src: asset_path(sources)))
|
281
|
+
end
|
282
|
+
end
|
283
|
+
alias_method :videos, :video
|
284
|
+
alias_method :video_tag, :video
|
285
|
+
|
286
|
+
###
|
287
|
+
# Returns an HTML audio element for the specified sources
|
288
|
+
#
|
289
|
+
# @overload audio(sources)
|
290
|
+
# @param [Array<String>] sources
|
291
|
+
# Asset sources
|
292
|
+
#
|
293
|
+
# @overload audio(sources, options)
|
294
|
+
# @param [Array<String>] sources
|
295
|
+
# Asset sources
|
296
|
+
# @param [Hash] options
|
297
|
+
# The HTML options to include in this audio
|
298
|
+
#
|
299
|
+
# @option options [String] :id
|
300
|
+
# Specifies a unique identifier for the audio
|
301
|
+
# @option options [String] :class
|
302
|
+
# Specifies the class of the audio
|
303
|
+
# @option options [Boolean] :draggable
|
304
|
+
# Specifies whether or not the audio is draggable (true, false, :auto)
|
305
|
+
# @option options [Symbol] :preload
|
306
|
+
# Specifies the method the web browser should use to preload the audio (:auto, :metadata, :none)
|
307
|
+
# @option options [Boolean] :hidden
|
308
|
+
# Should the audio be hidden from view
|
309
|
+
# @option options [Boolean] :controls
|
310
|
+
# Should the audio controls be shown if present
|
311
|
+
# @option options [Boolean] :autoplay
|
312
|
+
# Should the audio automatically play when it's ready
|
313
|
+
# @option options [Boolean] :loop
|
314
|
+
# Should the audio automatically loop when it's done
|
315
|
+
#
|
316
|
+
# @return [String]
|
317
|
+
# Generated HTML for sources with specified options
|
318
|
+
#
|
319
|
+
# @example
|
320
|
+
# audio('example.ogg')
|
321
|
+
# # => <audio src="/assets/example.ogg">
|
322
|
+
#
|
323
|
+
# audio('example.ogg', loop: true, autoplay: true)
|
324
|
+
# # => <audio src="/assets/example.ogg" loop="loop" autoplay="autoplay">
|
325
|
+
#
|
326
|
+
# audio('http://www.example.com/example.ogg')
|
327
|
+
# # => <audio src="http://www.example.com/example.ogg">
|
328
|
+
#
|
329
|
+
# audios('example.ogg', 'example.mp4')
|
330
|
+
# # => <audio>
|
331
|
+
# # => <source src="/assets/example.ogg">
|
332
|
+
# # => <source src="/assets/example.mp4">
|
333
|
+
# # => </audio>
|
334
|
+
#
|
335
|
+
# @since 0.1.0
|
336
|
+
# @api public
|
337
|
+
def audio(*sources)
|
338
|
+
options = sources.extract_options!.symbolize_keys
|
339
|
+
sources = sources.shift if sources.size == 1
|
340
|
+
|
341
|
+
if sources.is_a?(Array)
|
342
|
+
content_tag(:audio, options) do
|
343
|
+
sources.collect { |source| tag(:source, src: asset_path(source)) }.join("\n")
|
344
|
+
end
|
345
|
+
else
|
346
|
+
tag(:audio, options.reverse_merge(src: asset_path(sources)))
|
347
|
+
end
|
348
|
+
end
|
349
|
+
alias_method :audios, :audio
|
350
|
+
alias_method :audio_tag, :audio
|
351
|
+
|
352
|
+
###
|
353
|
+
# Returns whether or not the provided source is a valid URI
|
354
|
+
#
|
355
|
+
# @param [String] source
|
356
|
+
# URI source
|
357
|
+
#
|
358
|
+
# @return [Boolean]
|
359
|
+
# *true* if it is a valid, *false* otherwise
|
360
|
+
#
|
361
|
+
# @example
|
362
|
+
# is_uri?('http://www.example.com')
|
363
|
+
# # => true
|
364
|
+
#
|
365
|
+
# is_uri?('www.example.com')
|
366
|
+
# # => false
|
367
|
+
#
|
368
|
+
# is_uri?('//example.com')
|
369
|
+
# # => true
|
370
|
+
#
|
371
|
+
# is_uri?('/example/example.css')
|
372
|
+
# # => true
|
373
|
+
#
|
374
|
+
# is_uri?('example.css')
|
375
|
+
# # => false
|
376
|
+
#
|
377
|
+
# @since 0.1.0
|
378
|
+
# @api public
|
379
|
+
def is_uri?(source)
|
380
|
+
!!(source =~ URI_REGEXP)
|
381
|
+
end
|
382
|
+
|
383
|
+
###
|
384
|
+
# Returns a modified asset source based on the current application settings
|
385
|
+
#
|
386
|
+
# @param [String] source
|
387
|
+
# Asset source
|
388
|
+
# @param [Symbol] extension
|
389
|
+
# Asset file extension
|
390
|
+
#
|
391
|
+
# @return [String]
|
392
|
+
# Modified source based on application settings
|
393
|
+
#
|
394
|
+
# @example
|
395
|
+
# asset_path('example.webm')
|
396
|
+
# # => '/assets/example.webm'
|
397
|
+
#
|
398
|
+
# asset_path('stylesheet', :css)
|
399
|
+
# # => '/assets/stylesheet.css'
|
400
|
+
#
|
401
|
+
# asset_path('http://www.example.com/stylesheet.css')
|
402
|
+
# # => 'http://www.example.com/stylesheet.css'
|
403
|
+
#
|
404
|
+
# @since 0.1.0
|
405
|
+
# @api public
|
406
|
+
def asset_path(source, extension = nil)
|
407
|
+
return source if is_uri?(source)
|
408
|
+
source = source.to_s
|
409
|
+
source = rewrite_extension(source, extension)
|
410
|
+
source = rewrite_asset(source)
|
411
|
+
source = rewrite_asset_path(source)
|
412
|
+
source = rewrite_asset_host(source)
|
413
|
+
source
|
414
|
+
end
|
415
|
+
|
416
|
+
private
|
417
|
+
|
418
|
+
def rewrite_extension(source, extension)
|
419
|
+
if extension && File.extname(source).empty?
|
420
|
+
"#{source}.#{extension}"
|
421
|
+
else
|
422
|
+
source
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
def rewrite_asset(source)
|
427
|
+
if settings.index_assets && asset = Assets.manifest.assets[source]
|
428
|
+
source = asset
|
429
|
+
end
|
430
|
+
source
|
431
|
+
end
|
432
|
+
|
433
|
+
def rewrite_asset_path(source)
|
434
|
+
source = File.join(settings.assets_prefix, source)
|
435
|
+
source = "/#{source}" unless source[0] == ?/
|
436
|
+
source
|
437
|
+
end
|
438
|
+
|
439
|
+
def rewrite_asset_host(source)
|
440
|
+
host = settings.assets_host rescue settings.assets_host(source, request)
|
441
|
+
host ? host + source : source
|
442
|
+
end
|
443
|
+
end # Helpers
|
444
|
+
end # Assets
|
445
|
+
end # Padrino
|