temple 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/temple.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Temple
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
 
4
4
  autoload :Core, 'temple/core'
5
5
  autoload :Engine, 'temple/engine'
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
- if @options[:check_literal] && Utils.literal_string?(code)
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
@@ -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
- @in_multi = false
9
+ @compiling = false
10
+ @in_capture = nil
12
11
  end
13
-
14
- def compile(exp)
15
- res = send("on_#{exp.first}", *exp[1..-1])
16
-
17
- if @in_multi && CONCATABLE.include?(exp.first)
18
- concat(res)
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
- res
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
- if @in_multi
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
- unless @in_multi
60
- # We always need the preamble/postamble in capture.
61
- return compile([:multi, [:capture, name, block]])
62
- end
63
-
64
- @in_multi = false
65
- prev_buffer, @options[:buffer] = @options[:buffer], name.to_s
66
- content = compile(block)
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
- @options[:buffer] = prev_buffer
72
+ @capture = before
76
73
  end
77
74
  end
78
75
  end
76
+
@@ -46,6 +46,10 @@ module Temple
46
46
  def on_multi(*exp)
47
47
  [:multi, *exp.map { |e| compile(e) }]
48
48
  end
49
+
50
+ def on_capture(name, exp)
51
+ [:capture, name, compile(exp)]
52
+ end
49
53
 
50
54
  def on_doctype(type)
51
55
  trailing_newlines = type[/(\A|[^\r])(\n+)\Z/, 2].to_s
data/temple.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{temple}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Magnus Holm"]
@@ -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
- assert_equal("S:test", simple.compile([:static, "test"]))
30
- assert_equal("D:test", simple.compile([:dynamic, "test"]))
31
- assert_equal("B:test", simple.compile([:block, "test"]))
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 = S:test/, str)
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
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Magnus Holm