temple 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/temple.rb +1 -1
- data/lib/temple/core.rb +3 -7
- data/lib/temple/engine.rb +1 -1
- data/lib/temple/generator.rb +36 -38
- data/lib/temple/html/fast.rb +4 -0
- data/temple.gemspec +1 -1
- data/test/test_generator.rb +9 -8
- metadata +2 -2
data/lib/temple.rb
CHANGED
data/lib/temple/core.rb
CHANGED
@@ -83,11 +83,11 @@ module Temple
|
|
83
83
|
def postamble; buffer ".join" end
|
84
84
|
|
85
85
|
def on_static(text)
|
86
|
-
to_ruby(text)
|
86
|
+
concat(to_ruby(text))
|
87
87
|
end
|
88
88
|
|
89
89
|
def on_dynamic(code)
|
90
|
-
code
|
90
|
+
concat(code)
|
91
91
|
end
|
92
92
|
|
93
93
|
def on_block(code)
|
@@ -114,11 +114,7 @@ module Temple
|
|
114
114
|
def postamble; buffer end
|
115
115
|
|
116
116
|
def on_dynamic(code)
|
117
|
-
|
118
|
-
code
|
119
|
-
else
|
120
|
-
"(#{code}).to_s"
|
121
|
-
end
|
117
|
+
concat(code) + '.to_s'
|
122
118
|
end
|
123
119
|
end
|
124
120
|
end
|
data/lib/temple/engine.rb
CHANGED
@@ -47,7 +47,7 @@ module Temple
|
|
47
47
|
|
48
48
|
def initialize(options = {})
|
49
49
|
@chain = self.class.filters.map do |filter, args, blk|
|
50
|
-
opt = args.last.is_a?(Hash) ? args.last : {}
|
50
|
+
opt = args.last.is_a?(Hash) ? args.last.dup : {}
|
51
51
|
opt = args.inject(opt) do |memo, ele|
|
52
52
|
memo[ele] = options[ele] if options.has_key?(ele)
|
53
53
|
memo
|
data/lib/temple/generator.rb
CHANGED
@@ -1,30 +1,43 @@
|
|
1
1
|
module Temple
|
2
2
|
class Generator
|
3
3
|
DEFAULT_OPTIONS = {
|
4
|
-
:buffer => "_buf"
|
4
|
+
:buffer => "_buf",
|
5
5
|
}
|
6
6
|
|
7
|
-
CONCATABLE = [:static, :dynamic]
|
8
|
-
|
9
7
|
def initialize(options = {})
|
10
8
|
@options = DEFAULT_OPTIONS.merge(options)
|
11
|
-
@
|
9
|
+
@compiling = false
|
10
|
+
@in_capture = nil
|
12
11
|
end
|
13
|
-
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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)
|
19
23
|
else
|
20
|
-
|
24
|
+
begin
|
25
|
+
@compiling = true
|
26
|
+
[preamble, compile(exp), postamble].join(' ; ')
|
27
|
+
ensure
|
28
|
+
@compiling = false
|
29
|
+
end
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
24
33
|
def buffer(str = '')
|
25
34
|
@options[:buffer] + str
|
26
35
|
end
|
27
|
-
|
36
|
+
|
37
|
+
def concat(str)
|
38
|
+
buffer " << (#{str})"
|
39
|
+
end
|
40
|
+
|
28
41
|
def self.to_ruby(str)
|
29
42
|
str.inspect
|
30
43
|
end
|
@@ -37,18 +50,9 @@ module Temple
|
|
37
50
|
|
38
51
|
def preamble; '' end
|
39
52
|
def postamble; '' end
|
40
|
-
def concat(s) buffer " << (#{s})" end
|
41
53
|
|
42
54
|
def on_multi(*exp)
|
43
|
-
|
44
|
-
exp.map { |e| compile(e) }
|
45
|
-
else
|
46
|
-
@in_multi = true
|
47
|
-
content = exp.map { |e| compile(e) }
|
48
|
-
content = [preamble, content, postamble].flatten
|
49
|
-
@in_multi = false
|
50
|
-
content
|
51
|
-
end.join(" ; ")
|
55
|
+
exp.map { |e| compile(e) }.join(" ; ")
|
52
56
|
end
|
53
57
|
|
54
58
|
def on_newline
|
@@ -56,23 +60,17 @@ module Temple
|
|
56
60
|
end
|
57
61
|
|
58
62
|
def on_capture(name, block)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@in_multi = true
|
68
|
-
|
69
|
-
if CONCATABLE.include?(block.first)
|
70
|
-
"#{name} = #{content}"
|
71
|
-
else
|
72
|
-
content
|
73
|
-
end
|
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(' ; ')
|
74
71
|
ensure
|
75
|
-
@
|
72
|
+
@capture = before
|
76
73
|
end
|
77
74
|
end
|
78
75
|
end
|
76
|
+
|
data/lib/temple/html/fast.rb
CHANGED
data/temple.gemspec
CHANGED
data/test/test_generator.rb
CHANGED
@@ -11,11 +11,11 @@ class TestTempleGenerator < Test::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def on_static(s)
|
14
|
-
"S:#{s}"
|
14
|
+
concat "S:#{s}"
|
15
15
|
end
|
16
16
|
|
17
17
|
def on_dynamic(s)
|
18
|
-
"D:#{s}"
|
18
|
+
concat "D:#{s}"
|
19
19
|
end
|
20
20
|
|
21
21
|
def on_block(s)
|
@@ -26,9 +26,9 @@ class TestTempleGenerator < Test::Unit::TestCase
|
|
26
26
|
def test_simple_exp
|
27
27
|
simple = Simple.new
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
assert_match(/ << \(S:test\)/, simple.compile([:static, "test"]))
|
30
|
+
assert_match(/ << \(D:test\)/, simple.compile([:dynamic, "test"]))
|
31
|
+
assert_match(/B:test/, simple.compile([:block, "test"]))
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_multi
|
@@ -46,15 +46,16 @@ class TestTempleGenerator < Test::Unit::TestCase
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_capture
|
49
|
-
simple = Simple.new(:buffer => "VAR")
|
49
|
+
simple = Simple.new(:buffer => "VAR", :capture_generator => Simple)
|
50
50
|
str = simple.compile([:capture, "foo", [:static, "test"]])
|
51
51
|
|
52
|
-
assert_match(/foo =
|
52
|
+
assert_match(/foo = BUFFER/, str)
|
53
|
+
assert_match(/foo << \(S:test\)/, str)
|
53
54
|
assert_match(/VAR\Z/, str)
|
54
55
|
end
|
55
56
|
|
56
57
|
def test_capture_with_multi
|
57
|
-
simple = Simple.new(:buffer => "VAR")
|
58
|
+
simple = Simple.new(:buffer => "VAR", :capture_generator => Simple)
|
58
59
|
str = simple.compile([:multi,
|
59
60
|
[:static, "before"],
|
60
61
|
|