erubi 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +20 -0
- data/README.rdoc +21 -4
- data/lib/erubi.rb +31 -10
- data/lib/erubi/capture.rb +88 -0
- data/lib/tilt/erubi.rb +15 -19
- data/test/test.rb +181 -3
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba16a36a1aa6da80b461f49724bf565bbd3245e5
|
4
|
+
data.tar.gz: 91c6545b66c34061ed72b00292a1a882cb4b6d45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75fb3e6d36720ceefec4de3358b7052de90e6de7bb1d28570878f3bfc38fda5a2696c0c66e0b4159a22fdb5c5c63d95aeed78bbd5b3caeb0fc417a044679bb4d
|
7
|
+
data.tar.gz: f7edc95fef95ffce7537ea88ac3a68216c56cb999977b2008da27c50e9577e644776b44d6eda8d18366ee3235d0ee819e6552deb3d354b3ca67308aebe35a6a7
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
=== 1.1.0 (2016-11-14)
|
2
|
+
|
3
|
+
* Add :ensure option to supporting restoring bufvar to original value (jeremyevans)
|
4
|
+
|
5
|
+
* Don't have tilt support require erb (jeremyevans)
|
6
|
+
|
7
|
+
* Support :engine_class option in tilt support to override engine class used (jeremyevans)
|
8
|
+
|
9
|
+
* Support :capture option in tilt support to use Erubi::CaptureEngine (jeremyevans)
|
10
|
+
|
11
|
+
* Add erubi/capture file containing Erubi::CaptureEngine, allowing <%|= and <%|== for capture (and escaping) blocks in templates (jeremyevans)
|
12
|
+
|
13
|
+
* Raise ArgumentError if template source code contains indicators matched by regexp but not handled (jeremyevans)
|
14
|
+
|
15
|
+
* Add :bufval option to support arbitrary buffer values (jeremyevans)
|
16
|
+
|
17
|
+
* Add :regexp option to specify regexp used for scanning (jeremyevans)
|
18
|
+
|
19
|
+
* Add :src option to specify initial template source (jeremyevans)
|
20
|
+
|
1
21
|
=== 1.0.0 (2016-11-10)
|
2
22
|
|
3
23
|
* Initial Public Release
|
data/README.rdoc
CHANGED
@@ -3,11 +3,12 @@
|
|
3
3
|
Erubi is a ERB template engine for ruby. It is a simplified fork of Erubis, with
|
4
4
|
the following differences:
|
5
5
|
|
6
|
-
* Handles postfix conditionals when using escaping (e.g. <tt><%= foo if bar
|
6
|
+
* Handles postfix conditionals when using escaping (e.g. <tt><%= foo if bar %></tt>)
|
7
7
|
* Supports frozen_string_literal: true in templates via :freeze option
|
8
8
|
* Works with ruby's --enable-frozen-string-literal option
|
9
9
|
* Escapes ' (apostrophe) when escaping for better XSS protection
|
10
|
-
* Has 90% smaller memory footprint
|
10
|
+
* Has 90% smaller memory footprint for base engine
|
11
|
+
* Has 75% smaller memory footprint for tilt support
|
11
12
|
* Does no monkey patching (Erubis adds a method to Kernel)
|
12
13
|
* Uses an immutable design (all options passed to the constructor, which returns a frozen object)
|
13
14
|
* Has simpler internals (1 file, <100 lines of code)
|
@@ -18,7 +19,6 @@ It is not designed with Erubis API compatibility in mind, though most Erubis
|
|
18
19
|
ERB syntax works, with the following exceptions:
|
19
20
|
|
20
21
|
* No support for <tt><%===</tt> for debug output
|
21
|
-
* No support for custom patterns
|
22
22
|
|
23
23
|
= Installation
|
24
24
|
|
@@ -30,7 +30,7 @@ Source code is available on GitHub at https://github.com/jeremyevans/erubi
|
|
30
30
|
|
31
31
|
= Usage
|
32
32
|
|
33
|
-
The expected usage is via tilt, and erubi ships with tilt integration:
|
33
|
+
The expected usage is via tilt, and erubi ships with tilt 2 integration:
|
34
34
|
|
35
35
|
require 'tilt/erubi'
|
36
36
|
Tilt.new("filename.erb").render
|
@@ -45,6 +45,23 @@ source:
|
|
45
45
|
require 'erubi'
|
46
46
|
eval(Erubi::Engine.new(File.read('filename.erb')).src)
|
47
47
|
|
48
|
+
== Capturing
|
49
|
+
|
50
|
+
Erubi does not support capturing block output into the template by default.
|
51
|
+
However, it comes with an +erubi/capture+ file that supports capturing
|
52
|
+
via <tt><%|=</tt> and <tt><%|==</tt> tags:
|
53
|
+
|
54
|
+
<%|= form do %>
|
55
|
+
<input>
|
56
|
+
<% end %>
|
57
|
+
|
58
|
+
This offers similar functionality to that offered by Rails' <tt><%=</tt>
|
59
|
+
tags, but without the corner cases with that approach (which are due to
|
60
|
+
attempting to parse ruby code via a regexp). Similar to the <tt><%=</tt>
|
61
|
+
and <tt><%==</tt> tags, <tt><%|=</tt> captures by default and
|
62
|
+
<tt><%|==</tt> captures and escapes by default, but this can be reversed
|
63
|
+
via the +:escape_capture+ or +:escape+ options.
|
64
|
+
|
48
65
|
= Reporting Bugs
|
49
66
|
|
50
67
|
The bug tracker is located at https://github.com/jeremyevans/erubi/issues
|
data/lib/erubi.rb
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
module Erubi
|
4
4
|
ESCAPE_TABLE = {'&' => '&'.freeze, '<' => '<'.freeze, '>' => '>'.freeze, '"' => '"'.freeze, "'" => '''.freeze}.freeze
|
5
|
-
RANGE_FIRST = 0..0
|
6
5
|
RANGE_ALL = 0..-1
|
7
|
-
RANGE_LAST = -1..-1
|
8
6
|
|
9
7
|
if RUBY_VERSION >= '1.9'
|
8
|
+
RANGE_FIRST = 0
|
9
|
+
RANGE_LAST = -1
|
10
|
+
|
10
11
|
# Escape the following characters with their HTML/XML
|
11
12
|
# equivalents.
|
12
13
|
def self.h(value)
|
@@ -14,6 +15,9 @@ module Erubi
|
|
14
15
|
end
|
15
16
|
else
|
16
17
|
# :nocov:
|
18
|
+
RANGE_FIRST = 0..0
|
19
|
+
RANGE_LAST = -1..-1
|
20
|
+
|
17
21
|
def self.h(value)
|
18
22
|
value.to_s.gsub(/[&<>"']/){|s| ESCAPE_TABLE[s]}
|
19
23
|
end
|
@@ -31,26 +35,33 @@ module Erubi
|
|
31
35
|
attr_reader :bufvar
|
32
36
|
|
33
37
|
# Initialize a new Erubi::Engine. Options:
|
38
|
+
# :bufval :: The value to use for the buffer variable, as a string.
|
34
39
|
# :bufvar :: The variable name to use for the buffer variable, as a string.
|
40
|
+
# :ensure :: Wrap the template in a begin/ensure block restoring the previous value of bufvar.
|
35
41
|
# :escapefunc :: The function to use for escaping, as a string (default: ::Erubi.h).
|
36
42
|
# :escape :: Whether to make <%= escape by default, and <%== not escape by default.
|
37
43
|
# :escape_html :: Same as :escape, with lower priority.
|
38
44
|
# :filename :: The filename for the template.
|
39
45
|
# :freeze :: Whether to enable frozen string literals in the resulting source code.
|
40
46
|
# :outvar :: Same as bufvar, with lower priority.
|
41
|
-
# :
|
47
|
+
# :postamble :: The postamble for the template, by default returns the resulting source code.
|
42
48
|
# :preamble :: The preamble for the template, by default initializes up the buffer variable.
|
49
|
+
# :regexp :: The regexp to use for scanning.
|
50
|
+
# :src :: The initial value to use for the source code
|
43
51
|
# :trim :: Whether to trim leading and trailing whitespace, true by default.
|
44
52
|
def initialize(input, properties={})
|
45
53
|
escape = properties.fetch(:escape){properties.fetch(:escape_html, false)}
|
46
54
|
trim = properties[:trim] != false
|
47
55
|
@filename = properties[:filename]
|
48
56
|
@bufvar = bufvar = properties[:bufvar] || properties[:outvar] || "_buf"
|
49
|
-
|
57
|
+
bufval = properties[:bufval] || 'String.new'
|
58
|
+
regexp = properties[:regexp] || /<%(={1,2}|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
|
59
|
+
preamble = properties[:preamble] || "#{bufvar} = #{bufval};"
|
50
60
|
postamble = properties[:postamble] || "#{bufvar}.to_s\n"
|
51
61
|
|
52
|
-
@src = src = String.new
|
62
|
+
@src = src = properties[:src] || String.new
|
53
63
|
src << "# frozen_string_literal: true\n" if properties[:freeze]
|
64
|
+
src << "begin; __original_outvar = #{bufvar} if defined?(#{bufvar}); " if properties[:ensure]
|
54
65
|
|
55
66
|
unless escapefunc = properties[:escapefunc]
|
56
67
|
if escape
|
@@ -65,12 +76,13 @@ module Erubi
|
|
65
76
|
|
66
77
|
pos = 0
|
67
78
|
is_bol = true
|
68
|
-
input.scan(
|
79
|
+
input.scan(regexp) do |indicator, code, tailch, rspace|
|
69
80
|
match = Regexp.last_match
|
70
81
|
len = match.begin(0) - pos
|
71
82
|
text = input[pos, len]
|
72
83
|
pos = match.end(0)
|
73
84
|
ch = indicator ? indicator[RANGE_FIRST] : nil
|
85
|
+
|
74
86
|
lspace = nil
|
75
87
|
|
76
88
|
unless ch == '='
|
@@ -98,7 +110,8 @@ module Erubi
|
|
98
110
|
|
99
111
|
is_bol = rspace ? true : false
|
100
112
|
add_text(text) if text && !text.empty?
|
101
|
-
|
113
|
+
case ch
|
114
|
+
when '='
|
102
115
|
rspace = nil if tailch && !tailch.empty?
|
103
116
|
add_text(lspace) if lspace
|
104
117
|
if ((indicator == '=') ^ escape)
|
@@ -107,7 +120,7 @@ module Erubi
|
|
107
120
|
src << " #{bufvar} << #{escapefunc}((" << code << '));'
|
108
121
|
end
|
109
122
|
add_text(rspace) if rspace
|
110
|
-
|
123
|
+
when '#'
|
111
124
|
n = code.count("\n") + (rspace ? 1 : 0)
|
112
125
|
if trim
|
113
126
|
add_code("\n" * n)
|
@@ -116,9 +129,9 @@ module Erubi
|
|
116
129
|
add_code("\n" * n)
|
117
130
|
add_text(rspace) if rspace
|
118
131
|
end
|
119
|
-
|
132
|
+
when '%'
|
120
133
|
add_text("#{lspace}#{prefix||='<%'}#{code}#{tailch}#{postfix||='%>'}#{rspace}")
|
121
|
-
|
134
|
+
when nil, '-'
|
122
135
|
if trim
|
123
136
|
add_code("#{lspace}#{code}#{rspace}")
|
124
137
|
else
|
@@ -126,6 +139,8 @@ module Erubi
|
|
126
139
|
add_code(code)
|
127
140
|
add_text(rspace) if rspace
|
128
141
|
end
|
142
|
+
else
|
143
|
+
handle(indicator, code, tailch, rspace, lspace)
|
129
144
|
end
|
130
145
|
end
|
131
146
|
rest = pos == 0 ? input : input[pos..-1]
|
@@ -133,6 +148,7 @@ module Erubi
|
|
133
148
|
|
134
149
|
src << "\n" unless src[RANGE_LAST] == "\n"
|
135
150
|
src << postamble
|
151
|
+
src << "; ensure\n #{bufvar} = __original_outvar\nend\n" if properties[:ensure]
|
136
152
|
freeze
|
137
153
|
end
|
138
154
|
|
@@ -148,5 +164,10 @@ module Erubi
|
|
148
164
|
@src << code
|
149
165
|
@src << ';' unless code[RANGE_LAST] == "\n"
|
150
166
|
end
|
167
|
+
|
168
|
+
# Raise an exception, as the base engine class does not support handling other indicators.
|
169
|
+
def handle(indicator, code, tailch, rspace, lspace)
|
170
|
+
raise ArgumentError, "Invalid indicator: #{indicator}"
|
171
|
+
end
|
151
172
|
end
|
152
173
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erubi'
|
4
|
+
|
5
|
+
module Erubi
|
6
|
+
# A buffer class used for templates that support captures
|
7
|
+
class Buffer
|
8
|
+
def initialize
|
9
|
+
@bufs = [new_buffer]
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return the current buffer
|
13
|
+
def buffer
|
14
|
+
@bufs.last
|
15
|
+
end
|
16
|
+
|
17
|
+
# Append to the current buffer
|
18
|
+
def <<(str)
|
19
|
+
buffer << str
|
20
|
+
end
|
21
|
+
|
22
|
+
# Add a new buffer, that future appends will go to.
|
23
|
+
def before_append!
|
24
|
+
@bufs << new_buffer
|
25
|
+
end
|
26
|
+
|
27
|
+
# Take the current buffer and append it to the previous buffer.
|
28
|
+
def append=(_)
|
29
|
+
buf = @bufs.pop
|
30
|
+
buffer << buf.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
# Escape the current buffer and append it to the previous buffer,
|
34
|
+
def escape=(_)
|
35
|
+
buf = @bufs.pop
|
36
|
+
buffer << escape(buf.to_s)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return the current buffer, as a string.
|
40
|
+
def to_s
|
41
|
+
buffer.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# An object to use for the underlying buffers.
|
47
|
+
def new_buffer
|
48
|
+
String.new
|
49
|
+
end
|
50
|
+
|
51
|
+
# HTML/XML escape the given string.
|
52
|
+
def escape(str)
|
53
|
+
::Erubi.h(str)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# An engine class that supports capturing blocks via the <%|= and <%|== tags.
|
58
|
+
class CaptureEngine < Engine
|
59
|
+
# Initializes the engine. Accepts the same arguments as ::Erubi::Engine, and these
|
60
|
+
# additional options:
|
61
|
+
# :escape_capture :: Whether to make <%|= escape by default, and <%|== not escape by default,
|
62
|
+
# defaults to the same value as :escape.
|
63
|
+
def initialize(input, properties={})
|
64
|
+
properties = Hash[properties]
|
65
|
+
escape = properties.fetch(:escape){properties.fetch(:escape_html, false)}
|
66
|
+
@escape_capture = properties.fetch(:escape_capture, escape)
|
67
|
+
properties[:regexp] ||= /<%(\|?={1,2}|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
|
68
|
+
properties[:bufval] ||= "::Erubi::Buffer.new"
|
69
|
+
super
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Handle the <%|= and <%|== tags
|
75
|
+
def handle(indicator, code, tailch, rspace, lspace)
|
76
|
+
case indicator
|
77
|
+
when '|=', '|=='
|
78
|
+
rspace = nil if tailch && !tailch.empty?
|
79
|
+
add_text(lspace) if lspace
|
80
|
+
meth = ((indicator == '|=') ^ @escape_capture) ? 'append' : 'escape'
|
81
|
+
src << " #{@bufvar}.before_append!; #{@bufvar}.#{meth}= " << code
|
82
|
+
add_text(rspace) if rspace
|
83
|
+
else
|
84
|
+
super
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/tilt/erubi.rb
CHANGED
@@ -1,32 +1,28 @@
|
|
1
1
|
require 'tilt'
|
2
|
-
require 'tilt/
|
2
|
+
require 'tilt/template'
|
3
3
|
require 'erubi'
|
4
4
|
|
5
5
|
module Tilt
|
6
6
|
# Erubi (a simplified version of Erubis) template implementation
|
7
|
-
class ErubiTemplate <
|
7
|
+
class ErubiTemplate < Template
|
8
8
|
def prepare
|
9
|
-
@options.merge!(:preamble => false, :postamble => false)
|
10
|
-
@engine = Erubi::Engine.new(data, @options)
|
11
|
-
@outvar = @engine.bufvar
|
12
|
-
@engine
|
13
|
-
end
|
9
|
+
@options.merge!(:preamble => false, :postamble => false, :ensure=>true)
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
engine_class = if @options[:engine_class]
|
12
|
+
@options[:engine_class]
|
13
|
+
elsif @options[:capture]
|
14
|
+
Erubi::CaptureEngine
|
15
|
+
else
|
16
|
+
Erubi::Engine
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
@engine = engine_class.new(data, @options)
|
20
|
+
@outvar = @engine.bufvar
|
21
|
+
@engine
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
if RUBY_VERSION >= '1.9.0'
|
26
|
-
def precompiled(locals)
|
27
|
-
source, offset = super
|
28
|
-
[source, offset - 1]
|
29
|
-
end
|
24
|
+
def precompiled_template(locals)
|
25
|
+
@engine.src
|
30
26
|
end
|
31
27
|
|
32
28
|
Tilt.register self, 'erb', 'rhtml', 'erubi'
|
data/test/test.rb
CHANGED
@@ -22,6 +22,8 @@ if ENV['COVERAGE']
|
|
22
22
|
end
|
23
23
|
|
24
24
|
require 'erubi'
|
25
|
+
require 'erubi/capture'
|
26
|
+
require 'tilt/erubi'
|
25
27
|
require 'minitest/spec'
|
26
28
|
require 'minitest/autorun'
|
27
29
|
|
@@ -31,9 +33,21 @@ describe Erubi::Engine do
|
|
31
33
|
end
|
32
34
|
|
33
35
|
def check_output(input, src, result, &block)
|
34
|
-
t = Erubi::Engine.new(input, @options)
|
35
|
-
t.src.must_equal src
|
36
|
+
t = (@options[:engine] || Erubi::Engine).new(input, @options)
|
36
37
|
eval(t.src, block.binding).must_equal result
|
38
|
+
t.src.must_equal src
|
39
|
+
end
|
40
|
+
|
41
|
+
def setup_foo
|
42
|
+
@foo = Object.new
|
43
|
+
@foo.instance_variable_set(:@t, self)
|
44
|
+
def self.a; @a; end
|
45
|
+
def @foo.bar
|
46
|
+
@t.a << "a"
|
47
|
+
yield
|
48
|
+
@t.a << 'b'
|
49
|
+
@t.a.buffer.upcase!
|
50
|
+
end
|
37
51
|
end
|
38
52
|
|
39
53
|
it "should handle no options" do
|
@@ -79,6 +93,138 @@ END2
|
|
79
93
|
END3
|
80
94
|
end
|
81
95
|
|
96
|
+
it "should handle ensure option" do
|
97
|
+
list = ['&\'<>"2']
|
98
|
+
@options[:ensure] = true
|
99
|
+
@options[:bufvar] = '@a'
|
100
|
+
@a = 'bar'
|
101
|
+
check_output(<<END1, <<END2, <<END3){}
|
102
|
+
<table>
|
103
|
+
<tbody>
|
104
|
+
<% i = 0
|
105
|
+
list.each_with_index do |item, i| %>
|
106
|
+
<tr>
|
107
|
+
<td><%= i+1 %></td>
|
108
|
+
<td><%== item %></td>
|
109
|
+
</tr>
|
110
|
+
<% end %>
|
111
|
+
</tbody>
|
112
|
+
</table>
|
113
|
+
<%== i+1 %>
|
114
|
+
END1
|
115
|
+
begin; __original_outvar = @a if defined?(@a); @a = String.new; @a << '<table>
|
116
|
+
<tbody>
|
117
|
+
'; i = 0
|
118
|
+
list.each_with_index do |item, i|
|
119
|
+
@a << ' <tr>
|
120
|
+
<td>'; @a << ( i+1 ).to_s; @a << '</td>
|
121
|
+
<td>'; @a << ::Erubi.h(( item )); @a << '</td>
|
122
|
+
</tr>
|
123
|
+
'; end
|
124
|
+
@a << ' </tbody>
|
125
|
+
</table>
|
126
|
+
'; @a << ::Erubi.h(( i+1 )); @a << '
|
127
|
+
';
|
128
|
+
@a.to_s
|
129
|
+
; ensure
|
130
|
+
@a = __original_outvar
|
131
|
+
end
|
132
|
+
END2
|
133
|
+
<table>
|
134
|
+
<tbody>
|
135
|
+
<tr>
|
136
|
+
<td>1</td>
|
137
|
+
<td>&'<>"2</td>
|
138
|
+
</tr>
|
139
|
+
</tbody>
|
140
|
+
</table>
|
141
|
+
1
|
142
|
+
END3
|
143
|
+
@a.must_equal 'bar'
|
144
|
+
end
|
145
|
+
|
146
|
+
[['', false], ['=', true]].each do |ind, escape|
|
147
|
+
it "should allow <%|=#{ind} for capturing without escaping when :escape_capture => #{escape}" do
|
148
|
+
@options[:bufvar] = '@a'
|
149
|
+
@options[:capture] = true
|
150
|
+
@options[:escape_capture] = escape
|
151
|
+
@options[:escape] = !escape
|
152
|
+
@options[:engine] = ::Erubi::CaptureEngine
|
153
|
+
setup_foo
|
154
|
+
check_output(<<END1, <<END2, <<END3){}
|
155
|
+
<table>
|
156
|
+
<tbody>
|
157
|
+
<%|=#{ind} @foo.bar do %>
|
158
|
+
<tr>
|
159
|
+
<td><%=#{ind} 1 %></td>
|
160
|
+
<td><%=#{ind} '&' %></td>
|
161
|
+
</tr>
|
162
|
+
<% end %>
|
163
|
+
</tbody>
|
164
|
+
</table>
|
165
|
+
END1
|
166
|
+
#{'__erubi = ::Erubi;' unless escape}@a = ::Erubi::Buffer.new; @a << '<table>
|
167
|
+
<tbody>
|
168
|
+
'; @a << ' '; @a.before_append!; @a.append= @foo.bar do @a << '
|
169
|
+
'; @a << ' <tr>
|
170
|
+
<td>'; @a << #{!escape ? '__erubi' : '::Erubi'}.h(( 1 )); @a << '</td>
|
171
|
+
<td>'; @a << #{!escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</td>
|
172
|
+
</tr>
|
173
|
+
'; end
|
174
|
+
@a << ' </tbody>
|
175
|
+
</table>
|
176
|
+
';
|
177
|
+
@a.to_s
|
178
|
+
END2
|
179
|
+
<table>
|
180
|
+
<tbody>
|
181
|
+
A
|
182
|
+
<TR>
|
183
|
+
<TD>1</TD>
|
184
|
+
<TD>&</TD>
|
185
|
+
</TR>
|
186
|
+
B </tbody>
|
187
|
+
</table>
|
188
|
+
END3
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
[['', true], ['=', false]].each do |ind, escape|
|
193
|
+
it "should allow <%|=#{ind} for capturing with escaping when :escape => #{escape}" do
|
194
|
+
@options[:bufvar] = '@a'
|
195
|
+
@options[:capture] = true
|
196
|
+
@options[:escape] = escape
|
197
|
+
@options[:engine] = ::Erubi::CaptureEngine
|
198
|
+
setup_foo
|
199
|
+
check_output(<<END1, <<END2, <<END3){}
|
200
|
+
<table>
|
201
|
+
<tbody>
|
202
|
+
<%|=#{ind} @foo.bar do %>
|
203
|
+
<b><%=#{ind} '&' %></b>
|
204
|
+
<% end %>
|
205
|
+
</tbody>
|
206
|
+
</table>
|
207
|
+
END1
|
208
|
+
#{'__erubi = ::Erubi;' if escape}@a = ::Erubi::Buffer.new; @a << '<table>
|
209
|
+
<tbody>
|
210
|
+
'; @a << ' '; @a.before_append!; @a.escape= @foo.bar do @a << '
|
211
|
+
'; @a << ' <b>'; @a << #{escape ? '__erubi' : '::Erubi'}.h(( '&' )); @a << '</b>
|
212
|
+
'; end
|
213
|
+
@a << ' </tbody>
|
214
|
+
</table>
|
215
|
+
';
|
216
|
+
@a.to_s
|
217
|
+
END2
|
218
|
+
<table>
|
219
|
+
<tbody>
|
220
|
+
A
|
221
|
+
<B>&AMP;</B>
|
222
|
+
B </tbody>
|
223
|
+
</table>
|
224
|
+
END3
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
82
228
|
[:outvar, :bufvar].each do |var|
|
83
229
|
it "should handle :#{var} and :freeze options" do
|
84
230
|
@options[var] = "@_out_buf"
|
@@ -339,8 +485,12 @@ END3
|
|
339
485
|
Erubi::Engine.new('').frozen?.must_equal true
|
340
486
|
end
|
341
487
|
|
488
|
+
it "should raise an error if a tag is not handled when a custom regexp is used" do
|
489
|
+
proc{Erubi::Engine.new('<%] %>', :regexp =>/<%(={1,2}|\]|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m)}.must_raise ArgumentError
|
490
|
+
proc{Erubi::CaptureEngine.new('<%] %>', :regexp =>/<%(={1,2}|\]|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m)}.must_raise ArgumentError
|
491
|
+
end
|
492
|
+
|
342
493
|
it "should have working tilt support" do
|
343
|
-
require 'tilt/erubi'
|
344
494
|
@list = ['&\'<>"2']
|
345
495
|
Tilt::ErubiTemplate.new{<<END1}.render(self).must_equal(<<END2)
|
346
496
|
<table>
|
@@ -365,6 +515,34 @@ END1
|
|
365
515
|
</tbody>
|
366
516
|
</table>
|
367
517
|
1
|
518
|
+
END2
|
519
|
+
end
|
520
|
+
|
521
|
+
it "should have working tilt support for capturing" do
|
522
|
+
setup_foo
|
523
|
+
Tilt::ErubiTemplate.new(:capture=>true, :outvar=>'@a'){<<END1}.render(self).must_equal(<<END2)
|
524
|
+
1<%|= @foo.bar do %>bar<% end %>2
|
525
|
+
END1
|
526
|
+
1ABARB2
|
527
|
+
END2
|
528
|
+
end
|
529
|
+
|
530
|
+
it "should have working tilt support for specifying engine class" do
|
531
|
+
setup_foo
|
532
|
+
@a = 1
|
533
|
+
Tilt::ErubiTemplate.new(:engine_class=>Erubi::CaptureEngine, :outvar=>'@a'){<<END1}.render(self).must_equal(<<END2)
|
534
|
+
1<%|= @foo.bar do %>bar<% end %>2
|
535
|
+
END1
|
536
|
+
1ABARB2
|
537
|
+
END2
|
538
|
+
@a.must_equal 1
|
539
|
+
end
|
540
|
+
|
541
|
+
it "should have working tilt support for locals" do
|
542
|
+
Tilt::ErubiTemplate.new{<<END1}.render(self, :b=>3).must_equal(<<END2)
|
543
|
+
<%= b %>
|
544
|
+
END1
|
545
|
+
3
|
368
546
|
END2
|
369
547
|
end
|
370
548
|
end
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
@@ -9,8 +9,36 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-11-
|
13
|
-
dependencies:
|
12
|
+
date: 2016-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tilt
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: minitest
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
14
42
|
description: Erubi is a ERB template engine for ruby. It is a simplified fork of Erubis
|
15
43
|
email: code@jeremyevans.net
|
16
44
|
executables: []
|
@@ -25,6 +53,7 @@ files:
|
|
25
53
|
- README.rdoc
|
26
54
|
- Rakefile
|
27
55
|
- lib/erubi.rb
|
56
|
+
- lib/erubi/capture.rb
|
28
57
|
- lib/tilt/erubi.rb
|
29
58
|
- test/test.rb
|
30
59
|
homepage: https://github.com/jeremyevans/erubi
|