erubi 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +6 -0
- data/lib/erubi/capture_end.rb +44 -0
- data/lib/tilt/erubi.rb +6 -2
- data/test/test.rb +141 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33a3418c5b03e4cbef20234176d4b4d92308fa2a
|
4
|
+
data.tar.gz: eba2a09e387237585fb664206eb917b6b19838a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cb262e67db9196ce7a1b894dd611fdd62e9662d327ad6e68e95eb7226ca225c0dae98afa5d7edbda0ebb51d4711b8c0871d502876d3865e976965c0f853b1a2
|
7
|
+
data.tar.gz: f85eda1b8942c038a4261dbad958cba7aeb7c72423fa7cf06101dd5b15a6420e4301e4a3cb84168b429f177f853f3775b146d815c5e87478f6f89c9cc6b98049
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 1.3.0 (2016-12-29)
|
2
|
+
|
3
|
+
* Support :capture=>:explicit option in tilt support to use Erubi::CaptureEndEngine (jeremyevans)
|
4
|
+
|
5
|
+
* Add erubi/capture_end containing Erubi::CaptureEndEngine, allowing <%|= and <%|== for opening capture tags, and <%| for closing capture tags (jeremyevans)
|
6
|
+
|
1
7
|
=== 1.2.1 (2016-11-21)
|
2
8
|
|
3
9
|
* Don't automatically freeze template text strings on ruby 1.9 or 2.0 (jeremyevans)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erubi'
|
4
|
+
|
5
|
+
module Erubi
|
6
|
+
# An engine class that supports capturing blocks via the <%|= and <%|== tags,
|
7
|
+
# explicitly ending the captures using <%| end %> blocks.
|
8
|
+
class CaptureEndEngine < Engine
|
9
|
+
# Initializes the engine. Accepts the same arguments as ::Erubi::Engine, and these
|
10
|
+
# additional options:
|
11
|
+
# :escape_capture :: Whether to make <%|= escape by default, and <%|== not escape by default,
|
12
|
+
# defaults to the same value as :escape.
|
13
|
+
def initialize(input, properties={})
|
14
|
+
properties = Hash[properties]
|
15
|
+
escape = properties.fetch(:escape){properties.fetch(:escape_html, false)}
|
16
|
+
@escape_capture = properties.fetch(:escape_capture, escape)
|
17
|
+
@bufval = properties[:bufval] ||= 'String.new'
|
18
|
+
@bufstack = '__erubi_stack'
|
19
|
+
properties[:regexp] ||= /<%(\|?={1,2}|-|\#|%|\|)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Handle the <%|= and <%|== tags
|
26
|
+
def handle(indicator, code, tailch, rspace, lspace)
|
27
|
+
case indicator
|
28
|
+
when '|=', '|=='
|
29
|
+
rspace = nil if tailch && !tailch.empty?
|
30
|
+
add_text(lspace) if lspace
|
31
|
+
escape_capture = ((indicator == '|=') ^ @escape_capture)
|
32
|
+
src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code
|
33
|
+
add_text(rspace) if rspace
|
34
|
+
when '|'
|
35
|
+
rspace = nil if tailch && !tailch.empty?
|
36
|
+
add_text(lspace) if lspace
|
37
|
+
src << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
|
38
|
+
add_text(rspace) if rspace
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/tilt/erubi.rb
CHANGED
@@ -10,8 +10,12 @@ module Tilt
|
|
10
10
|
|
11
11
|
engine_class = if @options[:engine_class]
|
12
12
|
@options[:engine_class]
|
13
|
-
elsif @options[:capture]
|
14
|
-
|
13
|
+
elsif capture = @options[:capture]
|
14
|
+
if capture == :explicit
|
15
|
+
Erubi::CaptureEndEngine
|
16
|
+
else
|
17
|
+
Erubi::CaptureEngine
|
18
|
+
end
|
15
19
|
else
|
16
20
|
Erubi::Engine
|
17
21
|
end
|
data/test/test.rb
CHANGED
@@ -23,6 +23,7 @@ end
|
|
23
23
|
|
24
24
|
require 'erubi'
|
25
25
|
require 'erubi/capture'
|
26
|
+
require 'erubi/capture_end'
|
26
27
|
require 'tilt/erubi'
|
27
28
|
require 'minitest/spec'
|
28
29
|
require 'minitest/autorun'
|
@@ -34,8 +35,8 @@ describe Erubi::Engine do
|
|
34
35
|
|
35
36
|
def check_output(input, src, result, &block)
|
36
37
|
t = (@options[:engine] || Erubi::Engine).new(input, @options)
|
37
|
-
eval(t.src, block.binding).must_equal result
|
38
38
|
tsrc = t.src
|
39
|
+
eval(tsrc, block.binding).must_equal result
|
39
40
|
tsrc = tsrc.gsub("'.freeze;", "';") if RUBY_VERSION >= '2.1'
|
40
41
|
tsrc.must_equal src
|
41
42
|
end
|
@@ -52,6 +53,21 @@ describe Erubi::Engine do
|
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
56
|
+
def setup_bar
|
57
|
+
def self.bar
|
58
|
+
@a << "a"
|
59
|
+
yield
|
60
|
+
@a << 'b'
|
61
|
+
@a.upcase
|
62
|
+
end
|
63
|
+
def self.baz
|
64
|
+
@a << "c"
|
65
|
+
yield
|
66
|
+
@a << 'd'
|
67
|
+
@a * 2
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
55
71
|
it "should handle no options" do
|
56
72
|
list = ['&\'<>"2']
|
57
73
|
check_output(<<END1, <<END2, <<END3){}
|
@@ -146,7 +162,7 @@ END3
|
|
146
162
|
end
|
147
163
|
|
148
164
|
[['', false], ['=', true]].each do |ind, escape|
|
149
|
-
it "should allow <%|=#{ind} for capturing
|
165
|
+
it "should allow <%|=#{ind} for capturing with CaptureEngine with :escape_capture => #{escape} and :escape => #{escape}" do
|
150
166
|
@options[:bufvar] = '@a'
|
151
167
|
@options[:capture] = true
|
152
168
|
@options[:escape_capture] = escape
|
@@ -192,7 +208,7 @@ END3
|
|
192
208
|
end
|
193
209
|
|
194
210
|
[['', true], ['=', false]].each do |ind, escape|
|
195
|
-
it "should allow <%|=#{ind} for capturing with
|
211
|
+
it "should allow <%|=#{ind} for capturing with CaptureEngine with :escape => #{escape}" do
|
196
212
|
@options[:bufvar] = '@a'
|
197
213
|
@options[:capture] = true
|
198
214
|
@options[:escape] = escape
|
@@ -227,6 +243,119 @@ END3
|
|
227
243
|
end
|
228
244
|
end
|
229
245
|
|
246
|
+
[['', false], ['=', true]].each do |ind, escape|
|
247
|
+
it "should allow <%|=#{ind} and <%| for capturing with CaptureEndEngine with :escape_capture => #{escape} and :escape => #{!escape}" do
|
248
|
+
@options[:bufvar] = '@a'
|
249
|
+
@options[:capture] = true
|
250
|
+
@options[:escape_capture] = escape
|
251
|
+
@options[:escape] = !escape
|
252
|
+
@options[:engine] = ::Erubi::CaptureEndEngine
|
253
|
+
setup_bar
|
254
|
+
check_output(<<END1, <<END2, <<END3){}
|
255
|
+
<table>
|
256
|
+
<tbody>
|
257
|
+
<%|=#{ind} bar do %>
|
258
|
+
<b><%=#{ind} '&' %></b>
|
259
|
+
<%| end %>
|
260
|
+
</tbody>
|
261
|
+
</table>
|
262
|
+
END1
|
263
|
+
#{'__erubi = ::Erubi;' unless escape}@a = String.new; @a << '<table>
|
264
|
+
<tbody>
|
265
|
+
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << #{!escape ? '__erubi' : '::Erubi'}.h(( bar do @a << '
|
266
|
+
'; @a << ' <b>'; @a << #{!escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
|
267
|
+
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
|
268
|
+
'; @a << ' </tbody>
|
269
|
+
</table>
|
270
|
+
';
|
271
|
+
@a.to_s
|
272
|
+
END2
|
273
|
+
<table>
|
274
|
+
<tbody>
|
275
|
+
A
|
276
|
+
<B>&AMP;</B>
|
277
|
+
B
|
278
|
+
</tbody>
|
279
|
+
</table>
|
280
|
+
END3
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
[['', true], ['=', false]].each do |ind, escape|
|
285
|
+
it "should allow <%|=#{ind} and <%| for capturing with CaptureEndEngine when with :escape => #{escape}" do
|
286
|
+
@options[:bufvar] = '@a'
|
287
|
+
@options[:capture] = true
|
288
|
+
@options[:escape] = escape
|
289
|
+
@options[:engine] = ::Erubi::CaptureEndEngine
|
290
|
+
setup_bar
|
291
|
+
check_output(<<END1, <<END2, <<END3){}
|
292
|
+
<table>
|
293
|
+
<tbody>
|
294
|
+
<%|=#{ind} bar do %>
|
295
|
+
<b><%=#{ind} '&' %></b>
|
296
|
+
<%| end %>
|
297
|
+
</tbody>
|
298
|
+
</table>
|
299
|
+
END1
|
300
|
+
#{'__erubi = ::Erubi;' if escape}@a = String.new; @a << '<table>
|
301
|
+
<tbody>
|
302
|
+
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( bar do @a << '
|
303
|
+
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
|
304
|
+
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
|
305
|
+
'; @a << ' </tbody>
|
306
|
+
</table>
|
307
|
+
';
|
308
|
+
@a.to_s
|
309
|
+
END2
|
310
|
+
<table>
|
311
|
+
<tbody>
|
312
|
+
A
|
313
|
+
<B>&</B>
|
314
|
+
B
|
315
|
+
</tbody>
|
316
|
+
</table>
|
317
|
+
END3
|
318
|
+
end
|
319
|
+
|
320
|
+
it "should allow <%|=#{ind} and <%| for nested capturing with CaptureEndEngine when with :escape => #{escape}" do
|
321
|
+
@options[:bufvar] = '@a'
|
322
|
+
@options[:capture] = true
|
323
|
+
@options[:escape] = escape
|
324
|
+
@options[:engine] = ::Erubi::CaptureEndEngine
|
325
|
+
setup_bar
|
326
|
+
check_output(<<END1, <<END2, <<END3){}
|
327
|
+
<table>
|
328
|
+
<tbody>
|
329
|
+
<%|=#{ind} bar do %>
|
330
|
+
<b><%=#{ind} '&' %></b>
|
331
|
+
<%|=#{ind} baz do %>e<%| end %>
|
332
|
+
<%| end %>
|
333
|
+
</tbody>
|
334
|
+
</table>
|
335
|
+
END1
|
336
|
+
#{'__erubi = ::Erubi;' if escape}@a = String.new; @a << '<table>
|
337
|
+
<tbody>
|
338
|
+
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( bar do @a << '
|
339
|
+
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
|
340
|
+
'; @a << ' ';begin; (__erubi_stack ||= []) << @a; @a = String.new; __erubi_stack.last << (( baz do @a << 'e'; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
|
341
|
+
'; @a << ' '; end )).to_s; ensure; @a = __erubi_stack.pop; end; @a << '
|
342
|
+
'; @a << ' </tbody>
|
343
|
+
</table>
|
344
|
+
';
|
345
|
+
@a.to_s
|
346
|
+
END2
|
347
|
+
<table>
|
348
|
+
<tbody>
|
349
|
+
A
|
350
|
+
<B>&</B>
|
351
|
+
CEDCED
|
352
|
+
B
|
353
|
+
</tbody>
|
354
|
+
</table>
|
355
|
+
END3
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
230
359
|
[:outvar, :bufvar].each do |var|
|
231
360
|
it "should handle :#{var} and :freeze options" do
|
232
361
|
@options[var] = "@_out_buf"
|
@@ -533,6 +662,15 @@ END1
|
|
533
662
|
END2
|
534
663
|
end
|
535
664
|
|
665
|
+
it "should have working tilt support for explicit capturing" do
|
666
|
+
setup_bar
|
667
|
+
Tilt::ErubiTemplate.new(:capture=>:explicit, :outvar=>'@a'){<<END1}.render(self).must_equal(<<END2)
|
668
|
+
1<%|= bar do %>b<%|= baz do %>e<%| end %>ar<%| end %>2
|
669
|
+
END1
|
670
|
+
1ABCEDCEDARB2
|
671
|
+
END2
|
672
|
+
end
|
673
|
+
|
536
674
|
it "should have working tilt support for specifying engine class" do
|
537
675
|
setup_foo
|
538
676
|
@a = 1
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erubi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-12-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: tilt
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- Rakefile
|
55
55
|
- lib/erubi.rb
|
56
56
|
- lib/erubi/capture.rb
|
57
|
+
- lib/erubi/capture_end.rb
|
57
58
|
- lib/tilt/erubi.rb
|
58
59
|
- test/test.rb
|
59
60
|
homepage: https://github.com/jeremyevans/erubi
|