tilt 2.0.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +5 -5
  2. data/COPYING +2 -1
  3. data/bin/tilt +2 -110
  4. data/lib/tilt/_emacs_org.rb +2 -0
  5. data/lib/tilt/_handlebars.rb +2 -0
  6. data/lib/tilt/_jbuilder.rb +2 -0
  7. data/lib/tilt/_org.rb +2 -0
  8. data/lib/tilt/asciidoc.rb +11 -23
  9. data/lib/tilt/babel.rb +8 -0
  10. data/lib/tilt/builder.rb +23 -14
  11. data/lib/tilt/cli.rb +134 -0
  12. data/lib/tilt/coffee.rb +17 -25
  13. data/lib/tilt/commonmarker.rb +95 -0
  14. data/lib/tilt/creole.rb +9 -20
  15. data/lib/tilt/csv.rb +9 -21
  16. data/lib/tilt/erb.rb +27 -19
  17. data/lib/tilt/erubi.rb +55 -0
  18. data/lib/tilt/erubis.rb +20 -12
  19. data/lib/tilt/etanni.rb +5 -4
  20. data/lib/tilt/haml.rb +79 -42
  21. data/lib/tilt/kramdown.rb +8 -28
  22. data/lib/tilt/liquid.rb +11 -9
  23. data/lib/tilt/livescript.rb +11 -0
  24. data/lib/tilt/mapping.rb +242 -95
  25. data/lib/tilt/markaby.rb +5 -7
  26. data/lib/tilt/maruku.rb +6 -18
  27. data/lib/tilt/nokogiri.rb +11 -10
  28. data/lib/tilt/pandoc.rb +39 -0
  29. data/lib/tilt/pipeline.rb +19 -0
  30. data/lib/tilt/plain.rb +4 -19
  31. data/lib/tilt/prawn.rb +28 -0
  32. data/lib/tilt/radius.rb +15 -22
  33. data/lib/tilt/rdiscount.rb +17 -33
  34. data/lib/tilt/rdoc.rb +14 -35
  35. data/lib/tilt/redcarpet.rb +20 -93
  36. data/lib/tilt/redcloth.rb +9 -19
  37. data/lib/tilt/rst-pandoc.rb +10 -0
  38. data/lib/tilt/sass.rb +59 -22
  39. data/lib/tilt/slim.rb +5 -0
  40. data/lib/tilt/string.rb +9 -3
  41. data/lib/tilt/template.rb +246 -78
  42. data/lib/tilt/typescript.rb +19 -0
  43. data/lib/tilt/wikicloth.rb +8 -18
  44. data/lib/tilt/yajl.rb +5 -11
  45. data/lib/tilt.rb +91 -41
  46. metadata +39 -102
  47. data/CHANGELOG.md +0 -61
  48. data/Gemfile +0 -39
  49. data/HACKING +0 -16
  50. data/README.md +0 -206
  51. data/Rakefile +0 -95
  52. data/docs/TEMPLATES.md +0 -523
  53. data/docs/common.css +0 -14
  54. data/lib/tilt/bluecloth.rb +0 -24
  55. data/lib/tilt/less.rb +0 -38
  56. data/test/markaby/locals.mab +0 -1
  57. data/test/markaby/markaby.mab +0 -1
  58. data/test/markaby/markaby_other_static.mab +0 -1
  59. data/test/markaby/render_twice.mab +0 -1
  60. data/test/markaby/scope.mab +0 -1
  61. data/test/markaby/yielding.mab +0 -2
  62. data/test/test_helper.rb +0 -64
  63. data/test/tilt_asciidoctor_test.rb +0 -44
  64. data/test/tilt_blueclothtemplate_test.rb +0 -33
  65. data/test/tilt_buildertemplate_test.rb +0 -59
  66. data/test/tilt_cache_test.rb +0 -32
  67. data/test/tilt_coffeescripttemplate_test.rb +0 -104
  68. data/test/tilt_compilesite_test.rb +0 -51
  69. data/test/tilt_creoletemplate_test.rb +0 -24
  70. data/test/tilt_csv_test.rb +0 -65
  71. data/test/tilt_erbtemplate_test.rb +0 -239
  72. data/test/tilt_erubistemplate_test.rb +0 -151
  73. data/test/tilt_etannitemplate_test.rb +0 -174
  74. data/test/tilt_hamltemplate_test.rb +0 -144
  75. data/test/tilt_kramdown_test.rb +0 -20
  76. data/test/tilt_lesstemplate_test.less +0 -1
  77. data/test/tilt_lesstemplate_test.rb +0 -42
  78. data/test/tilt_liquidtemplate_test.rb +0 -78
  79. data/test/tilt_mapping_test.rb +0 -229
  80. data/test/tilt_markaby_test.rb +0 -88
  81. data/test/tilt_markdown_test.rb +0 -174
  82. data/test/tilt_marukutemplate_test.rb +0 -36
  83. data/test/tilt_metadata_test.rb +0 -42
  84. data/test/tilt_nokogiritemplate_test.rb +0 -87
  85. data/test/tilt_radiustemplate_test.rb +0 -75
  86. data/test/tilt_rdiscounttemplate_test.rb +0 -43
  87. data/test/tilt_rdoctemplate_test.rb +0 -29
  88. data/test/tilt_redcarpettemplate_test.rb +0 -59
  89. data/test/tilt_redclothtemplate_test.rb +0 -36
  90. data/test/tilt_sasstemplate_test.rb +0 -41
  91. data/test/tilt_stringtemplate_test.rb +0 -171
  92. data/test/tilt_template_test.rb +0 -316
  93. data/test/tilt_test.rb +0 -60
  94. data/test/tilt_wikiclothtemplate_test.rb +0 -32
  95. data/test/tilt_yajltemplate_test.rb +0 -101
  96. data/tilt.gemspec +0 -107
@@ -1,229 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
- require 'tilt/mapping'
4
-
5
- module Tilt
6
-
7
- class MappingTest < Minitest::Test
8
- class Stub
9
- end
10
-
11
- class Stub2
12
- end
13
-
14
- setup do
15
- @mapping = Mapping.new
16
- end
17
-
18
- test "registered?" do
19
- @mapping.register(Stub, 'foo', 'bar')
20
- assert @mapping.registered?('foo')
21
- assert @mapping.registered?('bar')
22
- refute @mapping.registered?('baz')
23
- end
24
-
25
- test "lookups on registered" do
26
- @mapping.register(Stub, 'foo', 'bar')
27
- assert_equal Stub, @mapping['foo']
28
- assert_equal Stub, @mapping['bar']
29
- assert_equal Stub, @mapping['hello.foo']
30
- assert_nil @mapping['foo.baz']
31
- end
32
-
33
- test "can be dup'd" do
34
- @mapping.register(Stub, 'foo')
35
- other = @mapping.dup
36
- assert other.registered?('foo')
37
-
38
- # @mapping doesn't leak to other
39
- @mapping.register(Stub, 'bar')
40
- refute other.registered?('bar')
41
-
42
- # other doesn't leak to @mapping
43
- other.register(Stub, 'baz')
44
- refute @mapping.registered?('baz')
45
- end
46
-
47
- test "#extensions_for" do
48
- @mapping.register(Stub, 'foo', 'bar')
49
- assert_equal ['foo', 'bar'].sort, @mapping.extensions_for(Stub).sort
50
- end
51
-
52
- test "supports old-style #register" do
53
- @mapping.register('foo', Stub)
54
- assert_equal Stub, @mapping['foo']
55
- end
56
-
57
- context "lazy with one template class" do
58
- setup do
59
- @mapping.register_lazy('MyTemplate', 'my_template', 'mt')
60
- end
61
-
62
- teardown do
63
- Object.send :remove_const, :MyTemplate if defined? ::MyTemplate
64
- end
65
-
66
- test "registered?" do
67
- assert @mapping.registered?('mt')
68
- end
69
-
70
- test "#extensions_for" do
71
- assert_equal ['mt'], @mapping.extensions_for('MyTemplate')
72
- end
73
-
74
- test "basic lookup" do
75
- req = proc do |file|
76
- assert_equal 'my_template', file
77
- class ::MyTemplate; end
78
- true
79
- end
80
-
81
- @mapping.stub :require, req do
82
- klass = @mapping['hello.mt']
83
- assert_equal ::MyTemplate, klass
84
- end
85
- end
86
-
87
- test "doesn't require when template class is present" do
88
- class ::MyTemplate; end
89
-
90
- req = proc do |file|
91
- flunk "#require shouldn't be called"
92
- end
93
-
94
- @mapping.stub :require, req do
95
- klass = @mapping['hello.mt']
96
- assert_equal ::MyTemplate, klass
97
- end
98
- end
99
-
100
- test "raises NameError when the class name is defined" do
101
- req = proc do |file|
102
- # do nothing
103
- end
104
-
105
- @mapping.stub :require, req do
106
- assert_raises(NameError) do
107
- @mapping['hello.mt']
108
- end
109
- end
110
- end
111
-
112
- test "warns when there's multiple threads" do
113
- Thread.new do
114
- req = proc do |file|
115
- class ::MyTemplate; end
116
- end
117
-
118
- @mapping.stub :require, req do
119
- assert_output '', /autoloading 'my_template'/ do
120
- @mapping['hello.mt']
121
- end
122
- end
123
- end.join
124
- end
125
- end
126
-
127
- context "lazy with two template classes" do
128
- setup do
129
- @mapping.register_lazy('MyTemplate1', 'my_template1', 'mt')
130
- @mapping.register_lazy('MyTemplate2', 'my_template2', 'mt')
131
- end
132
-
133
- teardown do
134
- Object.send :remove_const, :MyTemplate1 if defined? ::MyTemplate1
135
- Object.send :remove_const, :MyTemplate2 if defined? ::MyTemplate2
136
- end
137
-
138
- test "registered?" do
139
- assert @mapping.registered?('mt')
140
- end
141
-
142
- test "only attempt to load the last template" do
143
- req = proc do |file|
144
- assert_equal 'my_template2', file
145
- class ::MyTemplate2; end
146
- true
147
- end
148
-
149
- @mapping.stub :require, req do
150
- klass = @mapping['hello.mt']
151
- assert_equal ::MyTemplate2, klass
152
- end
153
- end
154
-
155
- test "uses the first template if it's present" do
156
- class ::MyTemplate1; end
157
-
158
- req = proc do |file|
159
- flunk
160
- end
161
-
162
- @mapping.stub :require, req do
163
- klass = @mapping['hello.mt']
164
- assert_equal ::MyTemplate1, klass
165
- end
166
- end
167
-
168
- test "falls back when LoadError is thrown" do
169
- req = proc do |file|
170
- raise LoadError unless file == 'my_template1'
171
- class ::MyTemplate1; end
172
- true
173
- end
174
-
175
- @mapping.stub :require, req do
176
- klass = @mapping['hello.mt']
177
- assert_equal ::MyTemplate1, klass
178
- end
179
- end
180
-
181
- test "raises the first LoadError when everything fails" do
182
- req = proc do |file|
183
- raise LoadError, file
184
- end
185
-
186
- @mapping.stub :require, req do
187
- err = assert_raises(LoadError) do
188
- klass = @mapping['hello.mt']
189
- end
190
-
191
- assert_equal 'my_template2', err.message
192
- end
193
- end
194
-
195
- test "handles autoloaded constants" do
196
- Object.autoload :MyTemplate2, 'my_template2'
197
- class ::MyTemplate1; end
198
-
199
- assert_equal MyTemplate1, @mapping['hello.mt']
200
- end
201
- end
202
-
203
- test "raises NameError on invalid class name" do
204
- @mapping.register_lazy '#foo', 'my_template', 'mt'
205
-
206
- req = proc do |file|
207
- # do nothing
208
- end
209
-
210
- @mapping.stub :require, req do
211
- assert_raises(NameError) do
212
- @mapping['hello.mt']
213
- end
214
- end
215
- end
216
-
217
- context "#templates_for" do
218
- setup do
219
- @mapping.register Stub, 'a'
220
- @mapping.register Stub2, 'b'
221
- end
222
-
223
- test "handles multiple engines" do
224
- assert_equal [Stub2, Stub], @mapping.templates_for('hello/world.a.b')
225
- end
226
- end
227
- end
228
- end
229
-
@@ -1,88 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/markaby'
6
-
7
- class MarkabyTiltTest < Minitest::Test
8
- def setup
9
- @block = lambda do |t|
10
- File.read(File.dirname(__FILE__) + "/#{t.file}")
11
- end
12
- end
13
-
14
- test "should be able to render a markaby template with static html" do
15
- tilt = Tilt::MarkabyTemplate.new("markaby/markaby.mab", &@block)
16
- assert_equal "hello from markaby!", tilt.render
17
- end
18
-
19
- test "should use the contents of the template" do
20
- tilt = ::Tilt::MarkabyTemplate.new("markaby/markaby_other_static.mab", &@block)
21
- assert_equal "_why?", tilt.render
22
- end
23
-
24
- test "should render from a string (given as data)" do
25
- tilt = ::Tilt::MarkabyTemplate.new { "html do; end" }
26
- assert_equal "<html></html>", tilt.render
27
- end
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
-
34
- test "should evaluate a template file in the scope given" do
35
- scope = Object.new
36
- def scope.foo
37
- "bar"
38
- end
39
-
40
- tilt = ::Tilt::MarkabyTemplate.new("markaby/scope.mab", &@block)
41
- assert_equal "<li>bar</li>", tilt.render(scope)
42
- end
43
-
44
- test "should pass locals to the template" do
45
- tilt = ::Tilt::MarkabyTemplate.new("markaby/locals.mab", &@block)
46
- assert_equal "<li>bar</li>", tilt.render(Object.new, { :foo => "bar" })
47
- end
48
-
49
- test "should yield to the block given" do
50
- tilt = ::Tilt::MarkabyTemplate.new("markaby/yielding.mab", &@block)
51
- eval_scope = Markaby::Builder.new
52
-
53
- output = tilt.render(Object.new, {}) do
54
- text("Joe")
55
- end
56
-
57
- assert_equal "Hey Joe", output
58
- end
59
-
60
- test "should be able to render two templates in a row" do
61
- tilt = ::Tilt::MarkabyTemplate.new("markaby/render_twice.mab", &@block)
62
-
63
- assert_equal "foo", tilt.render
64
- assert_equal "foo", tilt.render
65
- end
66
-
67
- test "should retrieve a Tilt::MarkabyTemplate when calling Tilt['hello.mab']" do
68
- assert_equal Tilt::MarkabyTemplate, ::Tilt['./markaby/markaby.mab']
69
- end
70
-
71
- test "should return a new instance of the implementation class (when calling Tilt.new)" do
72
- assert ::Tilt.new(File.dirname(__FILE__) + "/markaby/markaby.mab").kind_of?(Tilt::MarkabyTemplate)
73
- end
74
-
75
- test "should be able to evaluate block style templates" do
76
- tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello World!" }}
77
- assert_equal "<h1>Hello World!</h1>", tilt.render
78
- end
79
-
80
- test "should pass locals to block style templates" do
81
- tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello #{name}!" }}
82
- assert_equal "<h1>Hello _why!</h1>", tilt.render(nil, :name => "_why")
83
- end
84
- end
85
-
86
- rescue LoadError => boom
87
- warn "Tilt::MarkabyTemplate (disabled)"
88
- end
@@ -1,174 +0,0 @@
1
- # coding: UTF-8
2
- require 'tilt'
3
- require 'test_helper'
4
-
5
- begin
6
- require 'nokogiri'
7
-
8
- module MarkdownTests
9
- def self.included(mod)
10
- class << mod
11
- def template(t = nil)
12
- t.nil? ? @template : @template = t
13
- end
14
- end
15
- end
16
-
17
- def render(text, options = {})
18
- self.class.template.new(options) { text }.render
19
- end
20
-
21
- def normalize(html)
22
- Nokogiri::HTML.fragment(html).to_s.strip
23
- end
24
-
25
- def nrender(text, options = {})
26
- html = render(text, options)
27
- html.encode!("UTF-8") if html.respond_to?(:encode)
28
- normalize(html)
29
- end
30
-
31
- def test_escape_html
32
- html = nrender "Hello <b>World</b>"
33
- assert_equal "<p>Hello <b>World</b></p>", html
34
- end
35
-
36
- def test_escape_html_false
37
- html = nrender "Hello <b>World</b>", :escape_html => false
38
- assert_equal "<p>Hello <b>World</b></p>", html
39
- end
40
-
41
- def test_escape_html_true
42
- html = nrender "Hello <b>World</b>", :escape_html => true
43
- assert_equal "<p>Hello &lt;b&gt;World&lt;/b&gt;</p>", html
44
- end
45
-
46
- def test_smart_quotes
47
- html = nrender 'Hello "World"'
48
- assert_equal '<p>Hello "World"</p>', html
49
- end
50
-
51
- def test_smart_quotes_false
52
- html = nrender 'Hello "World"', :smartypants => false
53
- assert_equal '<p>Hello "World"</p>', html
54
- end
55
-
56
- def test_smart_quotes_true
57
- html = nrender 'Hello "World"', :smartypants => true
58
- assert_equal '<p>Hello “World”</p>', html
59
- end
60
-
61
- def test_smarty_pants
62
- html = nrender "Hello ``World'' -- This is --- a test ..."
63
- assert_equal "<p>Hello ``World'' -- This is --- a test ...</p>", html
64
- end
65
-
66
- def test_smarty_pants_false
67
- html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => false
68
- assert_equal "<p>Hello ``World'' -- This is --- a test ...</p>", html
69
- end
70
- end
71
-
72
- begin
73
- require 'tilt/rdiscount'
74
-
75
- class MarkdownRDiscountTest < Minitest::Test
76
- include MarkdownTests
77
- template Tilt::RDiscountTemplate
78
-
79
- def test_smarty_pants_true
80
- html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true
81
- assert_equal "<p>Hello “World” – This is — a test …</p>", html
82
- end
83
- end
84
- rescue LoadError => boom
85
- # It should already be warned in the main tests
86
- end
87
-
88
- begin
89
- require 'tilt/redcarpet'
90
-
91
- class MarkdownRedcarpetTest < Minitest::Test
92
- include MarkdownTests
93
- template Tilt::RedcarpetTemplate
94
- # Doesn't support escaping
95
- undef test_escape_html_true
96
-
97
- def test_smarty_pants_true
98
- # Various versions of Redcarpet support various versions of Smart pants.
99
- html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true
100
- assert_match /<p>Hello “World(''|”) – This is — a test …<\/p>/, html
101
- end
102
-
103
- def test_fenced_code_blocks_with_lang
104
- code = <<-COD.gsub(/^\s+/,"")
105
- ```ruby
106
- puts "hello world"
107
- ```
108
- COD
109
-
110
- html = nrender code, :fenced_code_blocks => true
111
- assert_equal %Q{<pre><code class="ruby">puts "hello world"\n</code></pre>}, html
112
- end
113
- end
114
- rescue LoadError => boom
115
- # It should already be warned in the main tests
116
- end
117
-
118
- begin
119
- require 'tilt/bluecloth'
120
-
121
- class MarkdownBlueClothTest < Minitest::Test
122
- include MarkdownTests
123
- template Tilt::BlueClothTemplate
124
-
125
- def test_smarty_pants_true
126
- html = nrender "Hello ``World'' -- This is --- a test ...", :smartypants => true
127
- assert_equal "<p>Hello “World” — This is —– a test …</p>", html
128
- end
129
- end
130
- rescue LoadError => boom
131
- # It should already be warned in the main tests
132
- end
133
-
134
- begin
135
- require 'tilt/kramdown'
136
-
137
- class MarkdownKramdownTest < Minitest::Test
138
- include MarkdownTests
139
- template Tilt::KramdownTemplate
140
- # Doesn't support escaping
141
- undef test_escape_html_true
142
- # Smarty Pants is *always* on, but doesn't support it fully
143
- undef test_smarty_pants
144
- undef test_smarty_pants_false
145
- end
146
- rescue LoadError => boom
147
- # It should already be warned in the main tests
148
- end
149
-
150
-
151
- begin
152
- require 'tilt/maruku'
153
-
154
- class MarkdownMarukuTest < Minitest::Test
155
- include MarkdownTests
156
- template Tilt::MarukuTemplate
157
- # Doesn't support escaping
158
- undef test_escape_html_true
159
- # Doesn't support Smarty Pants, and even fails on ``Foobar''
160
- undef test_smarty_pants
161
- undef test_smarty_pants_false
162
- # Smart Quotes is always on
163
- undef test_smart_quotes
164
- undef test_smart_quotes_false
165
- end
166
- rescue LoadError => boom
167
- # It should already be warned in the main tests
168
- end
169
-
170
- rescue LoadError
171
- warn "Markdown tests need Nokogiri"
172
- end
173
-
174
-
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/maruku'
6
-
7
- class MarukuTemplateTest < Minitest::Test
8
- test "registered below Kramdown" do
9
- %w[md mkd markdown].each do |ext|
10
- lazy = Tilt.lazy_map[ext]
11
- kram_idx = lazy.index { |klass, file| klass == 'Tilt::KramdownTemplate' }
12
- maru_idx = lazy.index { |klass, file| klass == 'Tilt::MarukuTemplate' }
13
- assert maru_idx > kram_idx,
14
- "#{maru_idx} should be higher than #{kram_idx}"
15
- end
16
- end
17
-
18
- test "preparing and evaluating templates on #render" do
19
- template = Tilt::MarukuTemplate.new { |t| "# Hello World!" }
20
- assert_equal "<h1 id=\"hello_world\">Hello World!</h1>", template.render.strip
21
- end
22
-
23
- test "can be rendered more than once" do
24
- template = Tilt::MarukuTemplate.new { |t| "# Hello World!" }
25
- 3.times { assert_equal "<h1 id=\"hello_world\">Hello World!</h1>", template.render.strip }
26
- end
27
-
28
- test "removes HTML when :filter_html is set" do
29
- template = Tilt::MarukuTemplate.new(:filter_html => true) { |t|
30
- "HELLO <blink>WORLD</blink>" }
31
- assert_equal "<p>HELLO </p>", template.render.strip
32
- end
33
- end
34
- rescue LoadError => boom
35
- warn "Tilt::MarukuTemplate (disabled)"
36
- end
@@ -1,42 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt/template'
3
-
4
- module Tilt
5
- class TemplateMetadataTest < Minitest::Test
6
- class MyTemplate < Template
7
- metadata[:global] = 1
8
- self.default_mime_type = 'text/html'
9
-
10
- def prepare
11
- end
12
-
13
- def allows_script?
14
- true
15
- end
16
- end
17
-
18
- test "global metadata" do
19
- assert MyTemplate.metadata
20
- assert_equal 1, MyTemplate.metadata[:global]
21
- end
22
-
23
- test "instance metadata" do
24
- tmpl = MyTemplate.new { '' }
25
- assert_equal 1, tmpl.metadata[:global]
26
- end
27
-
28
- test "gracefully handles default_mime_type" do
29
- assert_equal 'text/html', MyTemplate.metadata[:mime_type]
30
- end
31
-
32
- test "still allows .default_mime_type" do
33
- assert_equal 'text/html', MyTemplate.default_mime_type
34
- end
35
-
36
- test "gracefully handles allows_script?" do
37
- tmpl = MyTemplate.new { '' }
38
- assert_equal true, tmpl.metadata[:allows_script]
39
- end
40
- end
41
- end
42
-
@@ -1,87 +0,0 @@
1
- require 'test_helper'
2
- require 'tilt'
3
-
4
- begin
5
- require 'tilt/nokogiri'
6
- class NokogiriTemplateTest < Minitest::Test
7
- test "registered for '.nokogiri' files" do
8
- assert_equal Tilt::NokogiriTemplate, Tilt['test.nokogiri']
9
- assert_equal Tilt::NokogiriTemplate, Tilt['test.xml.nokogiri']
10
- end
11
-
12
- test "preparing and evaluating the template on #render" do
13
- template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" }
14
- doc = Nokogiri.XML template.render
15
- assert_equal 'Hello World!', doc.root.text
16
- assert_equal 'em', doc.root.name
17
- end
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
-
28
- test "passing locals" do
29
- template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + name + '!')" }
30
- doc = Nokogiri.XML template.render(Object.new, :name => 'Joe')
31
- assert_equal 'Hey Joe!', doc.root.text
32
- assert_equal 'em', doc.root.name
33
- end
34
-
35
- test "evaluating in an object scope" do
36
- template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + @name + '!')" }
37
- scope = Object.new
38
- scope.instance_variable_set :@name, 'Joe'
39
- doc = Nokogiri.XML template.render(scope)
40
- assert_equal 'Hey Joe!', doc.root.text
41
- assert_equal 'em', doc.root.name
42
- end
43
-
44
- test "passing a block for yield" do
45
- template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + yield + '!')" }
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
51
- end
52
-
53
- test "block style templates" do
54
- template =
55
- Tilt::NokogiriTemplate.new do |t|
56
- lambda { |xml| xml.em('Hey Joe!') }
57
- end
58
- doc = Nokogiri.XML template.render
59
- assert_equal 'Hey Joe!', doc.root.text
60
- assert_equal 'em', doc.root.name
61
- end
62
-
63
- test "allows nesting raw XML, API-compatible to Builder" do
64
- subtemplate = Tilt::NokogiriTemplate.new { "xml.em 'Hello World!'" }
65
- template = Tilt::NokogiriTemplate.new { "xml.strong { xml << yield }" }
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
72
- end
73
-
74
- test "doesn't modify self when template is a string" do
75
- template = Tilt::NokogiriTemplate.new { "xml.root { xml.child @hello }" }
76
- scope = Object.new
77
- scope.instance_variable_set(:@hello, "Hello World!")
78
-
79
- 3.times do
80
- doc = Nokogiri.XML(template.render(scope))
81
- assert_equal "Hello World!", doc.text.strip
82
- end
83
- end
84
- end
85
- rescue LoadError
86
- warn "Tilt::NokogiriTemplate (disabled)"
87
- end