tilt 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tilt.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Tilt
2
2
  TOPOBJECT = defined?(BasicObject) ? BasicObject : Object
3
- VERSION = '1.2.1'
3
+ VERSION = '1.2.2'
4
4
 
5
5
  @template_mappings = {}
6
6
 
@@ -164,6 +164,10 @@ module Tilt
164
164
  raise NotImplementedError
165
165
  end
166
166
  end
167
+
168
+ def evaluate(scope, locals, &block)
169
+ cached_evaluate(scope, locals, &block)
170
+ end
167
171
 
168
172
  # Process the template and return the result. The first time this
169
173
  # method is called, the template source is evaluated with instance_eval.
@@ -171,9 +175,9 @@ module Tilt
171
175
  # unbound method which will lead to better performance. In any case,
172
176
  # template executation is guaranteed to be performed in the scope object
173
177
  # with the locals specified and with support for yielding to the block.
174
- def evaluate(scope, locals, &block)
178
+ def cached_evaluate(scope, locals, &block)
175
179
  # Redefine itself to use method compilation the next time:
176
- def self.evaluate(scope, locals, &block)
180
+ def self.cached_evaluate(scope, locals, &block)
177
181
  method = compiled_method(locals.keys)
178
182
  method.bind(scope).call(locals, &block)
179
183
  end
@@ -620,17 +624,24 @@ module Tilt
620
624
  end
621
625
 
622
626
  def prepare; end
623
-
627
+
624
628
  def evaluate(scope, locals, &block)
625
- xml = ::Nokogiri::XML::Builder.new
629
+ block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
630
+
626
631
  if data.respond_to?(:to_str)
627
- locals[:xml] = xml
628
- block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
629
632
  super(scope, locals, &block)
630
- elsif data.kind_of?(Proc)
631
- data.call(xml)
633
+ else
634
+ ::Nokogiri::XML::Builder.new.tap(&data).to_xml
632
635
  end
633
- xml.to_xml
636
+ end
637
+
638
+ def precompiled_preamble(locals)
639
+ return super if locals.include? :xml
640
+ "xml = ::Nokogiri::XML::Builder.new\n#{super}"
641
+ end
642
+
643
+ def precompiled_postamble(locals)
644
+ "xml.to_xml"
634
645
  end
635
646
 
636
647
  def precompiled_template(locals)
@@ -647,20 +658,24 @@ module Tilt
647
658
  require_template_library 'builder'
648
659
  end
649
660
 
650
- def prepare
651
- end
661
+ def prepare; end
652
662
 
653
663
  def evaluate(scope, locals, &block)
664
+ return super(scope, locals, &block) if data.respond_to?(:to_str)
654
665
  xml = ::Builder::XmlMarkup.new(:indent => 2)
655
- if data.respond_to?(:to_str)
656
- locals[:xml] = xml
657
- super(scope, locals, &block)
658
- elsif data.kind_of?(Proc)
659
- data.call(xml)
660
- end
666
+ data.call(xml)
661
667
  xml.target!
662
668
  end
663
669
 
670
+ def precompiled_preamble(locals)
671
+ return super if locals.include? :xml
672
+ "xml = ::Builder::XmlMarkup.new(:indent => 2)\n#{super}"
673
+ end
674
+
675
+ def precompiled_postamble(locals)
676
+ "xml.target!"
677
+ end
678
+
664
679
  def precompiled_template(locals)
665
680
  data.to_str
666
681
  end
@@ -10,14 +10,14 @@ begin
10
10
  Tilt.register('md', Tilt::BlueClothTemplate)
11
11
  Tilt.register('mkd', Tilt::BlueClothTemplate)
12
12
  end
13
-
13
+
14
14
  teardown do
15
15
  # Need to revert to RDiscount, otherwise the RDiscount test will fail
16
16
  Tilt.register('markdown', Tilt::RDiscountTemplate)
17
17
  Tilt.register('md', Tilt::RDiscountTemplate)
18
18
  Tilt.register('mkd', Tilt::RDiscountTemplate)
19
19
  end
20
-
20
+
21
21
  test "registered for '.markdown' files unless RDiscount is loaded" do
22
22
  unless defined?(RDiscount)
23
23
  assert_equal Tilt::BlueClothTemplate, Tilt['test.markdown']
@@ -41,6 +41,11 @@ begin
41
41
  assert_equal "<h1>Hello World!</h1>", template.render
42
42
  end
43
43
 
44
+ test "can be rendered more than once" do
45
+ template = Tilt::BlueClothTemplate.new { |t| "# Hello World!" }
46
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
47
+ end
48
+
44
49
  test "smartypants when :smart is set" do
45
50
  template = Tilt::BlueClothTemplate.new(:smartypants => true) { |t|
46
51
  "OKAY -- 'Smarty Pants'" }
@@ -14,6 +14,11 @@ begin
14
14
  assert_equal "<em>Hello World!</em>\n", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
19
+ 3.times { assert_equal "<em>Hello World!</em>\n", template.render }
20
+ end
21
+
17
22
  test "passing locals" do
18
23
  template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
19
24
  assert_equal "<em>Hey Joe!</em>\n", template.render(Object.new, :name => 'Joe')
@@ -28,7 +33,7 @@ begin
28
33
 
29
34
  test "passing a block for yield" do
30
35
  template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
31
- assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }
36
+ 3.times { assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }}
32
37
  end
33
38
 
34
39
  test "block style templates" do
@@ -38,6 +43,16 @@ begin
38
43
  end
39
44
  assert_equal "<em>Hey Joe!</em>\n", template.render
40
45
  end
46
+
47
+ test "allows nesting raw XML" do
48
+ subtemplate = Tilt::BuilderTemplate.new { "xml.em 'Hello World!'" }
49
+ template = Tilt::BuilderTemplate.new { "xml.strong { xml << yield }" }
50
+ 3.times do
51
+ options = { :xml => Builder::XmlMarkup.new }
52
+ assert_equal "<strong>\n<em>Hello World!</em>\n</strong>\n",
53
+ template.render(options) { subtemplate.render(options) }
54
+ end
55
+ end
41
56
  end
42
57
  rescue LoadError
43
58
  warn "Tilt::BuilderTemplate (disabled)"
@@ -14,6 +14,11 @@ begin
14
14
  assert_match "puts('Hello, World!');", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
19
+ 3.times { assert_match "puts('Hello, World!');", template.render }
20
+ end
21
+
17
22
  test "disabling coffee-script wrapper" do
18
23
  template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| "puts 'Hello, World!'\n" }
19
24
  assert_equal "puts('Hello, World!');", template.render
@@ -19,6 +19,11 @@ class ERBTemplateTest < Test::Unit::TestCase
19
19
  assert_equal "Hello World!", template.render
20
20
  end
21
21
 
22
+ test "can be rendered more than once" do
23
+ template = Tilt::ERBTemplate.new { |t| "Hello World!" }
24
+ 3.times { assert_equal "Hello World!", template.render }
25
+ end
26
+
22
27
  test "passing locals" do
23
28
  template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
24
29
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
@@ -14,6 +14,11 @@ begin
14
14
  assert_equal "Hello World!", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
19
+ 3.times { assert_equal "Hello World!", template.render }
20
+ end
21
+
17
22
  test "passing locals" do
18
23
  template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' }
19
24
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
@@ -17,6 +17,11 @@ begin
17
17
  assert_equal "<p>Hello World!</p>\n", template.render
18
18
  end
19
19
 
20
+ test "can be rendered more than once" do
21
+ template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
22
+ 3.times { assert_equal "<p>Hello World!</p>\n", template.render }
23
+ end
24
+
20
25
  test "passing locals" do
21
26
  template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
22
27
  assert_equal "<p>Hey Joe!</p>\n", template.render(Object.new, :name => 'Joe')
@@ -13,6 +13,11 @@ begin
13
13
  template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" }
14
14
  assert_equal ".bg, #main { background-color: #0000ff; }\n", template.render
15
15
  end
16
+
17
+ test "can be rendered more than once" do
18
+ template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" }
19
+ 3.times { assert_equal ".bg, #main { background-color: #0000ff; }\n", template.render }
20
+ end
16
21
  end
17
22
 
18
23
  rescue LoadError => boom
@@ -14,6 +14,11 @@ begin
14
14
  assert_equal "Hello World!", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
19
+ 3.times { assert_equal "Hello World!", template.render }
20
+ end
21
+
17
22
  test "passing locals" do
18
23
  template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" }
19
24
  assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
@@ -26,6 +26,11 @@ begin
26
26
  assert_equal "<html></html>", tilt.render
27
27
  end
28
28
 
29
+ test "can be rendered more than once" do
30
+ tilt = ::Tilt::MarkabyTemplate.new { "html do; end" }
31
+ 3.times { assert_equal "<html></html>", tilt.render }
32
+ end
33
+
29
34
  test "should evaluate a template file in the scope given" do
30
35
  scope = Object.new
31
36
  def scope.foo
@@ -16,6 +16,15 @@ begin
16
16
  assert_equal 'em', doc.root.name
17
17
  end
18
18
 
19
+ test "can be rendered more than once" do
20
+ template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" }
21
+ 3.times do
22
+ doc = Nokogiri.XML template.render
23
+ assert_equal 'Hello World!', doc.root.text
24
+ assert_equal 'em', doc.root.name
25
+ end
26
+ end
27
+
19
28
  test "passing locals" do
20
29
  template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + name + '!')" }
21
30
  doc = Nokogiri.XML template.render(Object.new, :name => 'Joe')
@@ -34,9 +43,11 @@ begin
34
43
 
35
44
  test "passing a block for yield" do
36
45
  template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + yield + '!')" }
37
- doc = Nokogiri.XML template.render { 'Joe' }
38
- assert_equal 'Hey Joe!', doc.root.text
39
- assert_equal 'em', doc.root.name
46
+ 3.times do
47
+ doc = Nokogiri.XML template.render { 'Joe' }
48
+ assert_equal 'Hey Joe!', doc.root.text
49
+ assert_equal 'em', doc.root.name
50
+ end
40
51
  end
41
52
 
42
53
  test "block style templates" do
@@ -52,10 +63,12 @@ begin
52
63
  test "allows nesting raw XML, API-compatible to Builder" do
53
64
  subtemplate = Tilt::NokogiriTemplate.new { "xml.em 'Hello World!'" }
54
65
  template = Tilt::NokogiriTemplate.new { "xml.strong { xml << yield }" }
55
- options = { :xml => Nokogiri::XML::Builder.new }
56
- doc = Nokogiri.XML(template.render(options) { subtemplate.render(options) })
57
- assert_equal 'Hello World!', doc.root.text.strip
58
- assert_equal 'strong', doc.root.name
66
+ 3.times do
67
+ options = { :xml => Nokogiri::XML::Builder.new }
68
+ doc = Nokogiri.XML(template.render(options) { subtemplate.render(options) })
69
+ assert_equal 'Hello World!', doc.root.text.strip
70
+ assert_equal 'strong', doc.root.name
71
+ end
59
72
  end
60
73
  end
61
74
  rescue LoadError
@@ -18,6 +18,11 @@ begin
18
18
  assert_equal "Hello World!", template.render
19
19
  end
20
20
 
21
+ test "can be rendered more than once" do
22
+ template = Tilt::RadiusTemplate.new { |t| "Hello World!" }
23
+ 3.times { assert_equal "Hello World!", template.render }
24
+ end
25
+
21
26
  test "passing locals" do
22
27
  template = Tilt::RadiusTemplate.new { "Hey <r:name />!" }
23
28
  assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
@@ -22,6 +22,11 @@ begin
22
22
  assert_equal "<h1>Hello World!</h1>\n", template.render
23
23
  end
24
24
 
25
+ test "can be rendered more than once" do
26
+ template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
27
+ 3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
28
+ end
29
+
25
30
  test "smartypants when :smart is set" do
26
31
  template = Tilt::RDiscountTemplate.new(:smart => true) { |t|
27
32
  "OKAY -- 'Smarty Pants'" }
@@ -11,7 +11,12 @@ begin
11
11
 
12
12
  test "preparing and evaluating the template with #render" do
13
13
  template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
14
- assert_equal "<h1>Hello World!</h1>\n", template.render
14
+ assert_equal "<h1>Hello World!</h1>", template.render.strip
15
+ end
16
+
17
+ test "can be rendered more than once" do
18
+ template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
19
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render.strip }
15
20
  end
16
21
  end
17
22
  rescue LoadError => boom
@@ -14,6 +14,10 @@ begin
14
14
  assert_equal "<h1>Hello World!</h1>", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" }
19
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
20
+ end
17
21
  end
18
22
  rescue LoadError => boom
19
23
  warn "Tilt::RedClothTemplate (disabled)\n"
@@ -14,6 +14,11 @@ begin
14
14
  template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
15
15
  assert_equal "#main {\n background-color: #0000f1; }\n", template.render
16
16
  end
17
+
18
+ test "can be rendered more than once" do
19
+ template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
20
+ 3.times { assert_equal "#main {\n background-color: #0000f1; }\n", template.render }
21
+ end
17
22
  end
18
23
 
19
24
  class ScssTemplateTest < Test::Unit::TestCase
@@ -25,6 +30,11 @@ begin
25
30
  template = Tilt::ScssTemplate.new { |t| "#main {\n background-color: #0000f1;\n}" }
26
31
  assert_equal "#main {\n background-color: #0000f1; }\n", template.render
27
32
  end
33
+
34
+ test "can be rendered more than once" do
35
+ template = Tilt::ScssTemplate.new { |t| "#main {\n background-color: #0000f1;\n}" }
36
+ 3.times { assert_equal "#main {\n background-color: #0000f1; }\n", template.render }
37
+ end
28
38
  end
29
39
 
30
40
  rescue LoadError => boom
@@ -11,6 +11,11 @@ class StringTemplateTest < Test::Unit::TestCase
11
11
  assert_equal "Hello World!", template.render
12
12
  end
13
13
 
14
+ test "can be rendered more than once" do
15
+ template = Tilt::StringTemplate.new { |t| "Hello World!" }
16
+ 3.times { assert_equal "Hello World!", template.render }
17
+ end
18
+
14
19
  test "passing locals" do
15
20
  template = Tilt::StringTemplate.new { 'Hey #{name}!' }
16
21
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
data/tilt.gemspec CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'tilt'
6
- s.version = '1.2.1'
7
- s.date = '2010-12-24'
6
+ s.version = '1.2.2'
7
+ s.date = '2011-01-17'
8
8
 
9
9
  s.description = "Generic interface to multiple Ruby template engines"
10
10
  s.summary = s.description
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tilt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
7
  - 2
9
- - 1
10
- version: 1.2.1
8
+ - 2
9
+ version: 1.2.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Ryan Tomayko
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-12-24 00:00:00 -08:00
17
+ date: 2011-01-17 00:00:00 -08:00
19
18
  default_executable: tilt
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -40,7 +38,6 @@ dependencies:
40
38
  requirements:
41
39
  - - ">="
42
40
  - !ruby/object:Gem::Version
43
- hash: 3
44
41
  segments:
45
42
  - 0
46
43
  version: "0"
@@ -54,7 +51,6 @@ dependencies:
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
- hash: 3
58
54
  segments:
59
55
  - 0
60
56
  version: "0"
@@ -68,7 +64,6 @@ dependencies:
68
64
  requirements:
69
65
  - - ">="
70
66
  - !ruby/object:Gem::Version
71
- hash: 17
72
67
  segments:
73
68
  - 2
74
69
  - 2
@@ -84,7 +79,6 @@ dependencies:
84
79
  requirements:
85
80
  - - ">="
86
81
  - !ruby/object:Gem::Version
87
- hash: 3
88
82
  segments:
89
83
  - 0
90
84
  version: "0"
@@ -98,7 +92,6 @@ dependencies:
98
92
  requirements:
99
93
  - - ">="
100
94
  - !ruby/object:Gem::Version
101
- hash: 3
102
95
  segments:
103
96
  - 0
104
97
  version: "0"
@@ -112,7 +105,6 @@ dependencies:
112
105
  requirements:
113
106
  - - ">="
114
107
  - !ruby/object:Gem::Version
115
- hash: 3
116
108
  segments:
117
109
  - 0
118
110
  version: "0"
@@ -126,7 +118,6 @@ dependencies:
126
118
  requirements:
127
119
  - - ">="
128
120
  - !ruby/object:Gem::Version
129
- hash: 3
130
121
  segments:
131
122
  - 0
132
123
  version: "0"
@@ -140,7 +131,6 @@ dependencies:
140
131
  requirements:
141
132
  - - ">="
142
133
  - !ruby/object:Gem::Version
143
- hash: 3
144
134
  segments:
145
135
  - 0
146
136
  version: "0"
@@ -154,7 +144,6 @@ dependencies:
154
144
  requirements:
155
145
  - - ">="
156
146
  - !ruby/object:Gem::Version
157
- hash: 3
158
147
  segments:
159
148
  - 0
160
149
  version: "0"
@@ -168,7 +157,6 @@ dependencies:
168
157
  requirements:
169
158
  - - ">="
170
159
  - !ruby/object:Gem::Version
171
- hash: 3
172
160
  segments:
173
161
  - 0
174
162
  version: "0"
@@ -236,7 +224,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
236
224
  requirements:
237
225
  - - ">="
238
226
  - !ruby/object:Gem::Version
239
- hash: 3
240
227
  segments:
241
228
  - 0
242
229
  version: "0"
@@ -245,7 +232,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
245
232
  requirements:
246
233
  - - ">="
247
234
  - !ruby/object:Gem::Version
248
- hash: 3
249
235
  segments:
250
236
  - 0
251
237
  version: "0"