File tree 3 files changed +11
-6
lines changed
3 files changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -9,13 +9,13 @@ module ErbParser
9
9
def self . parse ( str , options = { } )
10
10
result = ParsedErb . new TreetopRunner . run ( str )
11
11
12
- if options [ :transform ]
12
+ if options [ :transform ] or options [ :map ]
13
13
result . tokens . map do |elem |
14
14
case elem
15
15
when String
16
16
elem
17
17
when ErbTag
18
- options [ :transform ] . call ( elem )
18
+ ( options [ :transform ] || options [ :map ] ) . call ( elem )
19
19
end
20
20
end . join
21
21
else
Original file line number Diff line number Diff line change @@ -35,23 +35,26 @@ Pass in an ERB template as a string:
35
35
36
36
The above usage sets ` result ` to an array of tokens.
37
37
38
- ## The ` :transform ` option
38
+ ## The ` :map ` option
39
39
40
40
Often, you'll want to parse an ERB template, apply some transformation function to all
41
- the ERB tags, and get back a new string. That's what the ` :transform ` option is for:
41
+ the ERB tags, and get back a new string. That's what the ` :map ` option is for:
42
42
43
43
result = ErbParser.parse(
44
44
'This is a<% TSET %>',
45
- :transform => lambda { |t| t.ruby_code.reverse.downcase }
45
+ :map => lambda { |t| t.ruby_code.reverse.downcase }
46
46
)
47
47
48
48
# 'This is a test '
49
49
puts result
50
50
51
- As in the above example, the ` :transform ` option must be a proc. The proc will be passed
51
+ As in the above example, the ` :map ` option must be a proc. The proc will be passed
52
52
an ` ErbTag ` as its only parameter. It must return a string. The returned string will be
53
53
inserted into the output where the ERB tag used to be.
54
54
55
+ ` :map ` was called ` :transform ` in version 0.0.0. ` :transform ` is still available but
56
+ deprecated.
57
+
55
58
## XML/HTML usage synopsis
56
59
57
60
Alternatively, you may pass in an XML/HTML document or fragment and have the ERB tags
Original file line number Diff line number Diff line change @@ -57,6 +57,8 @@ def test_complex_document
57
57
58
58
def test_transform_option
59
59
str = 'foo<% bar %>baz'
60
+ result = ErbParser . parse str , :map => lambda { |tag | tag . ruby_code . upcase . reverse }
61
+ assert_equal 'foo RAB baz' , result
60
62
result = ErbParser . parse str , :transform => lambda { |tag | tag . ruby_code . upcase . reverse }
61
63
assert_equal 'foo RAB baz' , result
62
64
end
You can’t perform that action at this time.
0 commit comments