temple 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/README.md +17 -18
- data/Rakefile +17 -12
- data/lib/temple.rb +19 -14
- data/lib/temple/engine.rb +13 -18
- data/lib/temple/erb/engine.rb +13 -0
- data/lib/temple/erb/parser.rb +38 -0
- data/lib/temple/erb/template.rb +7 -0
- data/lib/temple/erb/trimming.rb +33 -0
- data/lib/temple/filter.rb +7 -0
- data/lib/temple/filters/debugger.rb +15 -0
- data/lib/temple/filters/dynamic_inliner.rb +11 -19
- data/lib/temple/filters/escape_html.rb +27 -0
- data/lib/temple/filters/multi_flattener.rb +5 -13
- data/lib/temple/filters/static_merger.rb +6 -14
- data/lib/temple/{core.rb → generators.rb} +78 -37
- data/lib/temple/html/fast.rb +79 -127
- data/lib/temple/html/pretty.rb +75 -0
- data/lib/temple/mixins.rb +66 -0
- data/lib/temple/template.rb +35 -0
- data/lib/temple/utils.rb +51 -15
- data/lib/temple/version.rb +3 -0
- data/temple.gemspec +8 -13
- data/test/filters/test_dynamic_inliner.rb +42 -58
- data/test/filters/test_escape_html.rb +32 -0
- data/test/filters/test_multi_flattener.rb +33 -0
- data/test/filters/test_static_merger.rb +21 -23
- data/test/helper.rb +11 -17
- data/test/html/test_fast.rb +153 -0
- data/test/test_erb.rb +68 -0
- data/test/test_generator.rb +69 -76
- data/test/test_utils.rb +28 -0
- metadata +45 -16
- data/lib/temple/engines/erb.rb +0 -93
- data/lib/temple/generator.rb +0 -76
- data/lib/temple/parsers/erb.rb +0 -83
- data/test/engines/hello.erb +0 -4
- data/test/engines/test_erb.rb +0 -495
- data/test/engines/test_erb_m17n.rb +0 -132
- data/test/test_temple.rb +0 -28
data/test/test_utils.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Temple::Utils do
|
4
|
+
it 'has empty_exp?' do
|
5
|
+
Temple::Utils.empty_exp?([:multi]).should.be.true
|
6
|
+
Temple::Utils.empty_exp?([:multi, [:multi]]).should.be.true
|
7
|
+
Temple::Utils.empty_exp?([:multi, [:multi, [:newline]], [:newline]]).should.be.true
|
8
|
+
Temple::Utils.empty_exp?([:multi]).should.be.true
|
9
|
+
Temple::Utils.empty_exp?([:multi, [:multi, [:static, 'text']]]).should.be.false
|
10
|
+
Temple::Utils.empty_exp?([:multi, [:newline], [:multi, [:dynamic, 'text']]]).should.be.false
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has escape_html' do
|
14
|
+
Temple::Utils.escape_html('<').should.equal '<'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should escape unsafe html strings' do
|
18
|
+
with_html_safe(false) do
|
19
|
+
Temple::Utils.escape_html_safe('<').should.equal '<'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not escape safe html strings' do
|
24
|
+
with_html_safe(true) do
|
25
|
+
Temple::Utils.escape_html_safe('<').should.equal '<'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 4
|
9
|
+
version: 0.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Magnus Holm
|
@@ -14,10 +14,21 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-11-03 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bacon
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
21
32
|
description:
|
22
33
|
email: judofyr@gmail.com
|
23
34
|
executables: []
|
@@ -27,30 +38,40 @@ extensions: []
|
|
27
38
|
extra_rdoc_files: []
|
28
39
|
|
29
40
|
files:
|
41
|
+
- .gitignore
|
30
42
|
- .yardopts
|
31
43
|
- LICENSE
|
32
44
|
- README.md
|
33
45
|
- Rakefile
|
34
46
|
- lib/temple.rb
|
35
|
-
- lib/temple/core.rb
|
36
47
|
- lib/temple/engine.rb
|
37
|
-
- lib/temple/
|
48
|
+
- lib/temple/erb/engine.rb
|
49
|
+
- lib/temple/erb/parser.rb
|
50
|
+
- lib/temple/erb/template.rb
|
51
|
+
- lib/temple/erb/trimming.rb
|
52
|
+
- lib/temple/filter.rb
|
53
|
+
- lib/temple/filters/debugger.rb
|
38
54
|
- lib/temple/filters/dynamic_inliner.rb
|
55
|
+
- lib/temple/filters/escape_html.rb
|
39
56
|
- lib/temple/filters/multi_flattener.rb
|
40
57
|
- lib/temple/filters/static_merger.rb
|
41
|
-
- lib/temple/
|
58
|
+
- lib/temple/generators.rb
|
42
59
|
- lib/temple/html/fast.rb
|
43
|
-
- lib/temple/
|
60
|
+
- lib/temple/html/pretty.rb
|
61
|
+
- lib/temple/mixins.rb
|
62
|
+
- lib/temple/template.rb
|
44
63
|
- lib/temple/utils.rb
|
64
|
+
- lib/temple/version.rb
|
45
65
|
- temple.gemspec
|
46
|
-
- test/engines/hello.erb
|
47
|
-
- test/engines/test_erb.rb
|
48
|
-
- test/engines/test_erb_m17n.rb
|
49
66
|
- test/filters/test_dynamic_inliner.rb
|
67
|
+
- test/filters/test_escape_html.rb
|
68
|
+
- test/filters/test_multi_flattener.rb
|
50
69
|
- test/filters/test_static_merger.rb
|
51
70
|
- test/helper.rb
|
71
|
+
- test/html/test_fast.rb
|
72
|
+
- test/test_erb.rb
|
52
73
|
- test/test_generator.rb
|
53
|
-
- test/
|
74
|
+
- test/test_utils.rb
|
54
75
|
has_rdoc: true
|
55
76
|
homepage: http://dojo.rubyforge.org/
|
56
77
|
licenses: []
|
@@ -80,6 +101,14 @@ rubyforge_project:
|
|
80
101
|
rubygems_version: 1.3.6
|
81
102
|
signing_key:
|
82
103
|
specification_version: 3
|
83
|
-
summary: Template compilation framework in
|
84
|
-
test_files:
|
85
|
-
|
104
|
+
summary: Template compilation framework in Ruby
|
105
|
+
test_files:
|
106
|
+
- test/filters/test_dynamic_inliner.rb
|
107
|
+
- test/filters/test_escape_html.rb
|
108
|
+
- test/filters/test_multi_flattener.rb
|
109
|
+
- test/filters/test_static_merger.rb
|
110
|
+
- test/helper.rb
|
111
|
+
- test/html/test_fast.rb
|
112
|
+
- test/test_erb.rb
|
113
|
+
- test/test_generator.rb
|
114
|
+
- test/test_utils.rb
|
data/lib/temple/engines/erb.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
require 'erb'
|
2
|
-
|
3
|
-
module Temple
|
4
|
-
module Engines
|
5
|
-
# An engine which works in-place for ERB:
|
6
|
-
#
|
7
|
-
# require 'temple'
|
8
|
-
#
|
9
|
-
# template = Temple::Engine::ERB.new("<%= 1 + 1 %>")
|
10
|
-
# template.result # => "2"
|
11
|
-
class ERB < ::ERB
|
12
|
-
OriginalERB = ::ERB
|
13
|
-
Optimizers = [Filters::StaticMerger.new, Filters::DynamicInliner.new]
|
14
|
-
|
15
|
-
# The optional _filename_ argument passed to Kernel#eval when the ERB
|
16
|
-
# code is run
|
17
|
-
attr_accessor :filename
|
18
|
-
|
19
|
-
# The Ruby code generated by ERB
|
20
|
-
attr_reader :src
|
21
|
-
|
22
|
-
# The sexp generated by Temple
|
23
|
-
attr_reader :sexp
|
24
|
-
|
25
|
-
# The optimized sexp generated by Temple
|
26
|
-
attr_reader :optimized_sexp
|
27
|
-
|
28
|
-
# Sets the ERB constant to Temple::Engine::ERB
|
29
|
-
#
|
30
|
-
# Example:
|
31
|
-
#
|
32
|
-
# require 'temple'
|
33
|
-
# Temple::Engine::ERB.rock!
|
34
|
-
# ERB == Temple::Engine::ERB
|
35
|
-
def self.rock!
|
36
|
-
Object.send(:remove_const, :ERB)
|
37
|
-
Object.send(:const_set, :ERB, self)
|
38
|
-
end
|
39
|
-
|
40
|
-
# Sets the ERB constant back to regular ERB
|
41
|
-
#
|
42
|
-
# Example:
|
43
|
-
#
|
44
|
-
# require 'temple'
|
45
|
-
# original_erb = ERB
|
46
|
-
# Temple::Engine::ERB.rock!
|
47
|
-
# ERB.suck!
|
48
|
-
# ERB == original_erb
|
49
|
-
|
50
|
-
def self.suck!
|
51
|
-
Object.send(:remove_const, :ERB)
|
52
|
-
Object.send(:const_set, :ERB, OriginalERB)
|
53
|
-
end
|
54
|
-
|
55
|
-
def initialize(str, safe_level = nil, trim_mode = nil, eoutvar = '_erbout', options = {})
|
56
|
-
@safe_level = safe_level
|
57
|
-
@trim_mode = trim_mode
|
58
|
-
@parser = Parsers::ERB.new(:trim_mode => @trim_mode)
|
59
|
-
|
60
|
-
@generator = options[:generator] || Core::ArrayBuffer
|
61
|
-
|
62
|
-
if @generator.is_a?(Class)
|
63
|
-
@generator = @generator.new(:buffer => eoutvar)
|
64
|
-
end
|
65
|
-
|
66
|
-
@sexp = @parser.compile(str)
|
67
|
-
@optimized_sexp = Optimizers.inject(@sexp) { |m, e| e.compile(m) }
|
68
|
-
@src = @generator.compile(@optimized_sexp)
|
69
|
-
|
70
|
-
if str.respond_to?(:encoding)
|
71
|
-
@enc = detect_magic_comment(str) || str.encoding
|
72
|
-
@src.insert(0, "#coding:#{@enc}\n")
|
73
|
-
@src << ".force_encoding(__ENCODING__)"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def percent?
|
78
|
-
@trim_mode.is_a?(String) and @trim_mode.include?("%")
|
79
|
-
end
|
80
|
-
|
81
|
-
def detect_magic_comment(s)
|
82
|
-
if /\A<%#(.*)%>/ =~ s or (percent? and /\A%#(.*)/ =~ s)
|
83
|
-
comment = $1
|
84
|
-
comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
|
85
|
-
if %r"coding\s*[=:]\s*([[:alnum:]\-_]+)" =~ comment
|
86
|
-
enc = $1.sub(/-(?:mac|dos|unix)/i, '')
|
87
|
-
enc = Encoding.find(enc)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
data/lib/temple/generator.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
module Temple
|
2
|
-
class Generator
|
3
|
-
DEFAULT_OPTIONS = {
|
4
|
-
:buffer => "_buf",
|
5
|
-
}
|
6
|
-
|
7
|
-
def initialize(options = {})
|
8
|
-
@options = DEFAULT_OPTIONS.merge(options)
|
9
|
-
@compiling = false
|
10
|
-
@in_capture = nil
|
11
|
-
end
|
12
|
-
|
13
|
-
def capture_generator
|
14
|
-
@capture_generator ||=
|
15
|
-
@options[:capture_generator] || Temple::Core::StringBuffer
|
16
|
-
end
|
17
|
-
|
18
|
-
def compile(exp, single = false)
|
19
|
-
if @compiling || single
|
20
|
-
type, *args = exp
|
21
|
-
recv = @in_capture || self
|
22
|
-
recv.send("on_#{type}", *args)
|
23
|
-
else
|
24
|
-
begin
|
25
|
-
@compiling = true
|
26
|
-
[preamble, compile(exp), postamble].join(' ; ')
|
27
|
-
ensure
|
28
|
-
@compiling = false
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def buffer(str = '')
|
34
|
-
@options[:buffer] + str
|
35
|
-
end
|
36
|
-
|
37
|
-
def concat(str)
|
38
|
-
buffer " << (#{str})"
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.to_ruby(str)
|
42
|
-
str.inspect
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_ruby(str)
|
46
|
-
Generator.to_ruby(str)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Sensible defaults
|
50
|
-
|
51
|
-
def preamble; '' end
|
52
|
-
def postamble; '' end
|
53
|
-
|
54
|
-
def on_multi(*exp)
|
55
|
-
exp.map { |e| compile(e) }.join(" ; ")
|
56
|
-
end
|
57
|
-
|
58
|
-
def on_newline
|
59
|
-
"\n"
|
60
|
-
end
|
61
|
-
|
62
|
-
def on_capture(name, block)
|
63
|
-
before = @capture
|
64
|
-
@capture = capture_generator.new(:buffer => name)
|
65
|
-
|
66
|
-
[
|
67
|
-
@capture.preamble,
|
68
|
-
@capture.compile(block, true),
|
69
|
-
"#{name} = (#{@capture.postamble})"
|
70
|
-
].join(' ; ')
|
71
|
-
ensure
|
72
|
-
@capture = before
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
data/lib/temple/parsers/erb.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
require 'erb'
|
2
|
-
|
3
|
-
module Temple
|
4
|
-
module Parsers
|
5
|
-
# Parses ERB exactly the same way as erb.rb.
|
6
|
-
class ERB
|
7
|
-
Compiler = ::ERB::Compiler
|
8
|
-
|
9
|
-
def initialize(options = {})
|
10
|
-
@compiler = Compiler.new(options[:trim_mode])
|
11
|
-
end
|
12
|
-
|
13
|
-
def compile(src)
|
14
|
-
if src.respond_to?(:encoding) && src.encoding.dummy?
|
15
|
-
raise ArgumentError, "#{src.encoding} is not ASCII compatible"
|
16
|
-
end
|
17
|
-
|
18
|
-
result = [:multi]
|
19
|
-
|
20
|
-
content = ''
|
21
|
-
scanner = @compiler.make_scanner(src)
|
22
|
-
scanner.scan do |token|
|
23
|
-
next if token.nil?
|
24
|
-
next if token == ''
|
25
|
-
if scanner.stag.nil?
|
26
|
-
case token
|
27
|
-
when Compiler::PercentLine
|
28
|
-
append_static(result, content) if content.size > 0
|
29
|
-
content = ''
|
30
|
-
result << [:block, token.to_s.strip]
|
31
|
-
result << [:newline]
|
32
|
-
when :cr
|
33
|
-
result << [:newline]
|
34
|
-
when '<%', '<%=', '<%#'
|
35
|
-
scanner.stag = token
|
36
|
-
when "\n"
|
37
|
-
content << "\n"
|
38
|
-
append_static(result, content)
|
39
|
-
content = ''
|
40
|
-
when '<%%'
|
41
|
-
result << [:static, '<%']
|
42
|
-
else
|
43
|
-
append_static(result, token)
|
44
|
-
end
|
45
|
-
else
|
46
|
-
case token
|
47
|
-
when '%>'
|
48
|
-
case scanner.stag
|
49
|
-
when '<%'
|
50
|
-
if content[-1] == ?\n
|
51
|
-
content.chop!
|
52
|
-
result << [:block, content]
|
53
|
-
result << [:newline]
|
54
|
-
else
|
55
|
-
result << [:block, content]
|
56
|
-
end
|
57
|
-
when '<%='
|
58
|
-
result << [:dynamic, content]
|
59
|
-
when '<%#'
|
60
|
-
# nothing
|
61
|
-
end
|
62
|
-
scanner.stag = nil
|
63
|
-
content = ''
|
64
|
-
when '%%>'
|
65
|
-
content << '%>'
|
66
|
-
else
|
67
|
-
content << token
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
result
|
73
|
-
end
|
74
|
-
|
75
|
-
def append_static(result, content)
|
76
|
-
result << [:static, content]
|
77
|
-
content.count("\n").times do
|
78
|
-
result << [:newline]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
data/test/engines/hello.erb
DELETED
data/test/engines/test_erb.rb
DELETED
@@ -1,495 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../helper'
|
2
|
-
require 'erb'
|
3
|
-
|
4
|
-
NormalERB = ::ERB
|
5
|
-
TempleERB = Temple::Engines::ERB
|
6
|
-
|
7
|
-
class TestTempleEnginesERB < Test::Unit::TestCase
|
8
|
-
class MyError < RuntimeError ; end
|
9
|
-
|
10
|
-
def test_rock_suck
|
11
|
-
assert_equal(NormalERB, ::ERB)
|
12
|
-
TempleERB.rock!
|
13
|
-
assert_equal(TempleERB, ::ERB)
|
14
|
-
TempleERB.suck!
|
15
|
-
assert_equal(NormalERB, ::ERB)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_change_generator
|
19
|
-
gen = Temple::Core::StringBuffer
|
20
|
-
erb = TempleERB.new("Hello", nil, nil, 'foo', :generator => gen)
|
21
|
-
assert_match(/foo = ''/, erb.src)
|
22
|
-
|
23
|
-
erb = TempleERB.new("Hello", nil, nil, 'foo', :generator => gen.new(:buffer => "bar"))
|
24
|
-
assert_match(/bar = ''/, erb.src)
|
25
|
-
assert_no_match(/foo/, erb.src)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_optimizers
|
29
|
-
obj = Object.new
|
30
|
-
def obj.compile(exp)
|
31
|
-
[:static, "Hello World!"]
|
32
|
-
end
|
33
|
-
|
34
|
-
TempleERB::Optimizers << obj
|
35
|
-
|
36
|
-
erb = TempleERB.new("Hello")
|
37
|
-
assert_equal("Hello World!", erb.result)
|
38
|
-
ensure
|
39
|
-
TempleERB::Optimizers.delete(obj)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_without_filename
|
43
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>")
|
44
|
-
e = assert_raise(MyError) {
|
45
|
-
erb.result
|
46
|
-
}
|
47
|
-
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_with_filename
|
51
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>")
|
52
|
-
erb.filename = "test filename"
|
53
|
-
e = assert_raise(MyError) {
|
54
|
-
erb.result
|
55
|
-
}
|
56
|
-
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_without_filename_with_safe_level
|
60
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>", 1)
|
61
|
-
e = assert_raise(MyError) {
|
62
|
-
erb.result
|
63
|
-
}
|
64
|
-
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_with_filename_and_safe_level
|
68
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>", 1)
|
69
|
-
erb.filename = "test filename"
|
70
|
-
e = assert_raise(MyError) {
|
71
|
-
erb.result
|
72
|
-
}
|
73
|
-
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
class TestTempleEnginesERBCore < Test::Unit::TestCase
|
78
|
-
def setup
|
79
|
-
@erb = TempleERB
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_core
|
83
|
-
_test_core(nil)
|
84
|
-
_test_core(0)
|
85
|
-
_test_core(1)
|
86
|
-
_test_core(2)
|
87
|
-
_test_core(3)
|
88
|
-
end
|
89
|
-
|
90
|
-
def _test_core(safe)
|
91
|
-
erb = @erb.new("hello")
|
92
|
-
assert_equal("hello", erb.result)
|
93
|
-
|
94
|
-
erb = @erb.new("hello", safe, 0)
|
95
|
-
assert_equal("hello", erb.result)
|
96
|
-
|
97
|
-
erb = @erb.new("hello", safe, 1)
|
98
|
-
assert_equal("hello", erb.result)
|
99
|
-
|
100
|
-
erb = @erb.new("hello", safe, 2)
|
101
|
-
assert_equal("hello", erb.result)
|
102
|
-
|
103
|
-
src = <<EOS
|
104
|
-
%% hi
|
105
|
-
= hello
|
106
|
-
<% 3.times do |n| %>
|
107
|
-
% n=0
|
108
|
-
* <%= n %>
|
109
|
-
<% end %>
|
110
|
-
EOS
|
111
|
-
|
112
|
-
ans = <<EOS
|
113
|
-
%% hi
|
114
|
-
= hello
|
115
|
-
|
116
|
-
% n=0
|
117
|
-
* 0
|
118
|
-
|
119
|
-
% n=0
|
120
|
-
* 1
|
121
|
-
|
122
|
-
% n=0
|
123
|
-
* 2
|
124
|
-
|
125
|
-
EOS
|
126
|
-
erb = @erb.new(src)
|
127
|
-
assert_equal(ans, erb.result)
|
128
|
-
erb = @erb.new(src, safe, 0)
|
129
|
-
assert_equal(ans, erb.result)
|
130
|
-
erb = @erb.new(src, safe, '')
|
131
|
-
assert_equal(ans, erb.result)
|
132
|
-
|
133
|
-
ans = <<EOS
|
134
|
-
%% hi
|
135
|
-
= hello
|
136
|
-
% n=0
|
137
|
-
* 0% n=0
|
138
|
-
* 1% n=0
|
139
|
-
* 2
|
140
|
-
EOS
|
141
|
-
erb = @erb.new(src, safe, 1)
|
142
|
-
assert_equal(ans.chomp, erb.result)
|
143
|
-
erb = @erb.new(src, safe, '>')
|
144
|
-
assert_equal(ans.chomp, erb.result)
|
145
|
-
|
146
|
-
ans = <<EOS
|
147
|
-
%% hi
|
148
|
-
= hello
|
149
|
-
% n=0
|
150
|
-
* 0
|
151
|
-
% n=0
|
152
|
-
* 1
|
153
|
-
% n=0
|
154
|
-
* 2
|
155
|
-
EOS
|
156
|
-
|
157
|
-
erb = @erb.new(src, safe, 2)
|
158
|
-
assert_equal(ans, erb.result)
|
159
|
-
erb = @erb.new(src, safe, '<>')
|
160
|
-
assert_equal(ans, erb.result)
|
161
|
-
|
162
|
-
ans = <<EOS
|
163
|
-
% hi
|
164
|
-
= hello
|
165
|
-
|
166
|
-
* 0
|
167
|
-
|
168
|
-
* 0
|
169
|
-
|
170
|
-
* 0
|
171
|
-
|
172
|
-
EOS
|
173
|
-
erb = @erb.new(src, safe, '%')
|
174
|
-
assert_equal(ans, erb.result)
|
175
|
-
|
176
|
-
ans = <<EOS
|
177
|
-
% hi
|
178
|
-
= hello
|
179
|
-
* 0* 0* 0
|
180
|
-
EOS
|
181
|
-
erb = @erb.new(src, safe, '%>')
|
182
|
-
assert_equal(ans.chomp, erb.result)
|
183
|
-
|
184
|
-
ans = <<EOS
|
185
|
-
% hi
|
186
|
-
= hello
|
187
|
-
* 0
|
188
|
-
* 0
|
189
|
-
* 0
|
190
|
-
EOS
|
191
|
-
erb = @erb.new(src, safe, '%<>')
|
192
|
-
assert_equal(ans, erb.result)
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_safe_04
|
196
|
-
erb = @erb.new('<%=$SAFE%>', 4)
|
197
|
-
assert_equal('4', erb.result(TOPLEVEL_BINDING.taint))
|
198
|
-
end
|
199
|
-
|
200
|
-
class Foo; end
|
201
|
-
|
202
|
-
def test_def_class
|
203
|
-
erb = @erb.new('hello')
|
204
|
-
cls = erb.def_class
|
205
|
-
assert_equal(Object, cls.superclass)
|
206
|
-
assert(cls.new.respond_to?('result'))
|
207
|
-
cls = erb.def_class(Foo)
|
208
|
-
assert_equal(Foo, cls.superclass)
|
209
|
-
assert(cls.new.respond_to?('result'))
|
210
|
-
cls = erb.def_class(Object, 'erb')
|
211
|
-
assert_equal(Object, cls.superclass)
|
212
|
-
assert(cls.new.respond_to?('erb'))
|
213
|
-
end
|
214
|
-
|
215
|
-
def test_percent
|
216
|
-
src = <<EOS
|
217
|
-
%n = 1
|
218
|
-
<%= n%>
|
219
|
-
EOS
|
220
|
-
assert_equal("1\n", TempleERB.new(src, nil, '%').result)
|
221
|
-
|
222
|
-
src = <<EOS
|
223
|
-
<%
|
224
|
-
%>
|
225
|
-
EOS
|
226
|
-
ans = "\n"
|
227
|
-
assert_equal(ans, TempleERB.new(src, nil, '%').result)
|
228
|
-
|
229
|
-
src = "<%\n%>"
|
230
|
-
# ans = "\n"
|
231
|
-
ans = ""
|
232
|
-
assert_equal(ans, TempleERB.new(src, nil, '%').result)
|
233
|
-
|
234
|
-
src = <<EOS
|
235
|
-
<%
|
236
|
-
n = 1
|
237
|
-
%><%= n%>
|
238
|
-
EOS
|
239
|
-
assert_equal("1\n", TempleERB.new(src, nil, '%').result)
|
240
|
-
|
241
|
-
src = <<EOS
|
242
|
-
%n = 1
|
243
|
-
%% <% n = 2
|
244
|
-
n.times do |i|%>
|
245
|
-
%% %%><%%<%= i%><%
|
246
|
-
end%>
|
247
|
-
%%%
|
248
|
-
EOS
|
249
|
-
ans = <<EOS
|
250
|
-
%
|
251
|
-
% %%><%0
|
252
|
-
% %%><%1
|
253
|
-
%%
|
254
|
-
EOS
|
255
|
-
assert_equal(ans, TempleERB.new(src, nil, '%').result)
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_def_erb_method
|
259
|
-
klass = Class.new
|
260
|
-
klass.module_eval do
|
261
|
-
extend ERB::DefMethod
|
262
|
-
fname = File.join(File.dirname(File.expand_path(__FILE__)), 'hello.erb')
|
263
|
-
def_erb_method('hello', fname)
|
264
|
-
end
|
265
|
-
assert(klass.new.respond_to?('hello'))
|
266
|
-
|
267
|
-
assert(! klass.new.respond_to?('hello_world'))
|
268
|
-
erb = @erb.new('hello, world')
|
269
|
-
klass.module_eval do
|
270
|
-
def_erb_method('hello_world', erb)
|
271
|
-
end
|
272
|
-
assert(klass.new.respond_to?('hello_world'))
|
273
|
-
end
|
274
|
-
|
275
|
-
def test_def_method_without_filename
|
276
|
-
klass = Class.new
|
277
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>")
|
278
|
-
erb.filename = "test filename"
|
279
|
-
assert(! klass.new.respond_to?('my_error'))
|
280
|
-
erb.def_method(klass, 'my_error')
|
281
|
-
e = assert_raise(::TestTempleEnginesERB::MyError) {
|
282
|
-
klass.new.my_error
|
283
|
-
}
|
284
|
-
assert_match(/\A\(ERB\):1\b/, e.backtrace[0])
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_def_method_with_fname
|
288
|
-
klass = Class.new
|
289
|
-
erb = TempleERB.new("<% raise ::TestTempleEnginesERB::MyError %>")
|
290
|
-
erb.filename = "test filename"
|
291
|
-
assert(! klass.new.respond_to?('my_error'))
|
292
|
-
erb.def_method(klass, 'my_error', 'test fname')
|
293
|
-
e = assert_raise(::TestTempleEnginesERB::MyError) {
|
294
|
-
klass.new.my_error
|
295
|
-
}
|
296
|
-
assert_match(/\Atest fname:1\b/, e.backtrace[0])
|
297
|
-
end
|
298
|
-
|
299
|
-
def test_escape
|
300
|
-
src = <<EOS
|
301
|
-
1.<%% : <%="<%%"%>
|
302
|
-
2.%%> : <%="%%>"%>
|
303
|
-
3.
|
304
|
-
% x = "foo"
|
305
|
-
<%=x%>
|
306
|
-
4.
|
307
|
-
%% print "foo"
|
308
|
-
5.
|
309
|
-
%% <%="foo"%>
|
310
|
-
6.<%="
|
311
|
-
% print 'foo'
|
312
|
-
"%>
|
313
|
-
7.<%="
|
314
|
-
%% print 'foo'
|
315
|
-
"%>
|
316
|
-
EOS
|
317
|
-
ans = <<EOS
|
318
|
-
1.<% : <%%
|
319
|
-
2.%%> : %>
|
320
|
-
3.
|
321
|
-
foo
|
322
|
-
4.
|
323
|
-
% print "foo"
|
324
|
-
5.
|
325
|
-
% foo
|
326
|
-
6.
|
327
|
-
% print 'foo'
|
328
|
-
|
329
|
-
7.
|
330
|
-
%% print 'foo'
|
331
|
-
|
332
|
-
EOS
|
333
|
-
assert_equal(ans, TempleERB.new(src, nil, '%').result)
|
334
|
-
end
|
335
|
-
|
336
|
-
def test_keep_lineno
|
337
|
-
src = <<EOS
|
338
|
-
Hello,
|
339
|
-
% x = "World"
|
340
|
-
<%= x%>
|
341
|
-
% raise("lineno")
|
342
|
-
EOS
|
343
|
-
|
344
|
-
erb = TempleERB.new(src, nil, '%')
|
345
|
-
begin
|
346
|
-
erb.result
|
347
|
-
assert(false)
|
348
|
-
rescue
|
349
|
-
assert_match(/\A\(erb\):4\b/, $@[0].to_s)
|
350
|
-
end
|
351
|
-
|
352
|
-
src = <<EOS
|
353
|
-
%>
|
354
|
-
Hello,
|
355
|
-
<% x = "World%%>
|
356
|
-
"%>
|
357
|
-
<%= x%>
|
358
|
-
EOS
|
359
|
-
|
360
|
-
ans = <<EOS
|
361
|
-
%>Hello,
|
362
|
-
World%>
|
363
|
-
EOS
|
364
|
-
assert_equal(ans, TempleERB.new(src, nil, '>').result)
|
365
|
-
|
366
|
-
ans = <<EOS
|
367
|
-
%>
|
368
|
-
Hello,
|
369
|
-
|
370
|
-
World%>
|
371
|
-
EOS
|
372
|
-
assert_equal(ans, TempleERB.new(src, nil, '<>').result)
|
373
|
-
|
374
|
-
ans = <<EOS
|
375
|
-
%>
|
376
|
-
Hello,
|
377
|
-
|
378
|
-
World%>
|
379
|
-
|
380
|
-
EOS
|
381
|
-
assert_equal(ans, TempleERB.new(src).result)
|
382
|
-
|
383
|
-
src = <<EOS
|
384
|
-
Hello,
|
385
|
-
<% x = "World%%>
|
386
|
-
"%>
|
387
|
-
<%= x%>
|
388
|
-
<% raise("lineno") %>
|
389
|
-
EOS
|
390
|
-
|
391
|
-
erb = TempleERB.new(src)
|
392
|
-
begin
|
393
|
-
erb.result
|
394
|
-
assert(false)
|
395
|
-
rescue
|
396
|
-
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
397
|
-
end
|
398
|
-
|
399
|
-
erb = TempleERB.new(src, nil, '>')
|
400
|
-
begin
|
401
|
-
erb.result
|
402
|
-
assert(false)
|
403
|
-
rescue
|
404
|
-
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
405
|
-
end
|
406
|
-
|
407
|
-
erb = TempleERB.new(src, nil, '<>')
|
408
|
-
begin
|
409
|
-
erb.result
|
410
|
-
assert(false)
|
411
|
-
rescue
|
412
|
-
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
413
|
-
end
|
414
|
-
|
415
|
-
src = <<EOS
|
416
|
-
% y = 'Hello'
|
417
|
-
<%- x = "World%%>
|
418
|
-
"-%>
|
419
|
-
<%= x %><%- x = nil -%>
|
420
|
-
<% raise("lineno") %>
|
421
|
-
EOS
|
422
|
-
|
423
|
-
erb = TempleERB.new(src, nil, '-')
|
424
|
-
begin
|
425
|
-
erb.result
|
426
|
-
assert(false)
|
427
|
-
rescue
|
428
|
-
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
429
|
-
end
|
430
|
-
|
431
|
-
erb = TempleERB.new(src, nil, '%-')
|
432
|
-
begin
|
433
|
-
erb.result
|
434
|
-
assert(false)
|
435
|
-
rescue
|
436
|
-
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
437
|
-
end
|
438
|
-
end
|
439
|
-
|
440
|
-
def test_explicit
|
441
|
-
src = <<EOS
|
442
|
-
<% x = %w(hello world) -%>
|
443
|
-
NotSkip <%- y = x -%> NotSkip
|
444
|
-
<% x.each do |w| -%>
|
445
|
-
<%- up = w.upcase -%>
|
446
|
-
* <%= up %>
|
447
|
-
<% end -%>
|
448
|
-
<%- z = nil -%> NotSkip <%- z = x %>
|
449
|
-
<%- z.each do |w| -%>
|
450
|
-
<%- down = w.downcase -%>
|
451
|
-
* <%= down %>
|
452
|
-
<%- up = w.upcase -%>
|
453
|
-
* <%= up %>
|
454
|
-
<%- end -%>
|
455
|
-
KeepNewLine <%- z = nil -%>
|
456
|
-
EOS
|
457
|
-
|
458
|
-
ans = <<EOS
|
459
|
-
NotSkip NotSkip
|
460
|
-
* HELLO
|
461
|
-
* WORLD
|
462
|
-
NotSkip
|
463
|
-
* hello
|
464
|
-
* HELLO
|
465
|
-
* world
|
466
|
-
* WORLD
|
467
|
-
KeepNewLine
|
468
|
-
EOS
|
469
|
-
assert_equal(ans, TempleERB.new(src, nil, '-').result)
|
470
|
-
assert_equal(ans, TempleERB.new(src, nil, '-%').result)
|
471
|
-
end
|
472
|
-
|
473
|
-
def test_url_encode
|
474
|
-
assert_equal("Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide",
|
475
|
-
ERB::Util.url_encode("Programming Ruby: The Pragmatic Programmer's Guide"))
|
476
|
-
|
477
|
-
if "".respond_to?(:force_encoding)
|
478
|
-
assert_equal("%A5%B5%A5%F3%A5%D7%A5%EB",
|
479
|
-
ERB::Util.url_encode("\xA5\xB5\xA5\xF3\xA5\xD7\xA5\xEB".force_encoding("EUC-JP")))
|
480
|
-
end
|
481
|
-
end
|
482
|
-
end
|
483
|
-
|
484
|
-
class TestTempleEnginesERBCoreWOStrScan < TestTempleEnginesERBCore
|
485
|
-
def setup
|
486
|
-
@save_map = ERB::Compiler::Scanner.instance_variable_get('@scanner_map')
|
487
|
-
map = {[nil, false]=>ERB::Compiler::SimpleScanner}
|
488
|
-
ERB::Compiler::Scanner.instance_variable_set('@scanner_map', map)
|
489
|
-
super
|
490
|
-
end
|
491
|
-
|
492
|
-
def teardown
|
493
|
-
ERB::Compiler::Scanner.instance_variable_set('@scanner_map', @save_map)
|
494
|
-
end
|
495
|
-
end
|