tilt 1.3.3 → 1.4.0

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.
Files changed (53) hide show
  1. data/CHANGELOG.md +40 -0
  2. data/COPYING +1 -1
  3. data/Gemfile +31 -1
  4. data/HACKING +16 -0
  5. data/README.md +26 -2
  6. data/Rakefile +25 -1
  7. data/TEMPLATES.md +13 -13
  8. data/bin/tilt +8 -6
  9. data/lib/tilt/asciidoc.rb +34 -0
  10. data/lib/tilt/coffee.rb +4 -0
  11. data/lib/tilt/css.rb +10 -2
  12. data/lib/tilt/csv.rb +71 -0
  13. data/lib/tilt/erb.rb +2 -2
  14. data/lib/tilt/etanni.rb +27 -0
  15. data/lib/tilt/liquid.rb +4 -0
  16. data/lib/tilt/markdown.rb +35 -11
  17. data/lib/tilt/nokogiri.rb +4 -4
  18. data/lib/tilt/plain.rb +20 -0
  19. data/lib/tilt/radius.rb +4 -0
  20. data/lib/tilt/rdoc.rb +20 -5
  21. data/lib/tilt/template.rb +96 -92
  22. data/lib/tilt/textile.rb +5 -0
  23. data/lib/tilt/wiki.rb +8 -0
  24. data/lib/tilt.rb +15 -1
  25. data/test/tilt_asciidoctor_test.rb +44 -0
  26. data/test/tilt_blueclothtemplate_test.rb +1 -1
  27. data/test/tilt_coffeescripttemplate_test.rb +64 -11
  28. data/test/tilt_creoletemplate_test.rb +1 -1
  29. data/test/tilt_csv_test.rb +69 -0
  30. data/test/tilt_erbtemplate_test.rb +5 -0
  31. data/test/tilt_erubistemplate_test.rb +1 -1
  32. data/test/tilt_etannitemplate_test.rb +173 -0
  33. data/test/tilt_fallback_test.rb +6 -6
  34. data/test/tilt_hamltemplate_test.rb +1 -1
  35. data/test/tilt_kramdown_test.rb +1 -1
  36. data/test/tilt_lesstemplate_test.less +1 -0
  37. data/test/tilt_lesstemplate_test.rb +19 -3
  38. data/test/tilt_liquidtemplate_test.rb +1 -1
  39. data/test/tilt_markaby_test.rb +1 -1
  40. data/test/tilt_markdown_test.rb +16 -5
  41. data/test/tilt_marukutemplate_test.rb +1 -1
  42. data/test/tilt_radiustemplate_test.rb +1 -1
  43. data/test/tilt_rdiscounttemplate_test.rb +1 -1
  44. data/test/tilt_rdoctemplate_test.rb +10 -3
  45. data/test/tilt_redcarpettemplate_test.rb +15 -3
  46. data/test/tilt_redclothtemplate_test.rb +13 -1
  47. data/test/tilt_sasstemplate_test.rb +1 -1
  48. data/test/tilt_stringtemplate_test.rb +1 -1
  49. data/test/tilt_template_test.rb +122 -0
  50. data/test/tilt_wikiclothtemplate_test.rb +1 -1
  51. data/test/tilt_yajltemplate_test.rb +13 -4
  52. data/tilt.gemspec +28 -17
  53. metadata +204 -75
data/lib/tilt/plain.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'tilt/template'
2
+
3
+
4
+ module Tilt
5
+ # Raw text (no template functionality).
6
+ class PlainTemplate < Template
7
+ self.default_mime_type = 'text/html'
8
+
9
+ def self.engine_initialized?
10
+ true
11
+ end
12
+
13
+ def prepare
14
+ end
15
+
16
+ def evaluate(scope, locals, &block)
17
+ @output ||= data
18
+ end
19
+ end
20
+ end
data/lib/tilt/radius.rb CHANGED
@@ -47,5 +47,9 @@ module Tilt
47
47
  parser = Radius::Parser.new(context, options)
48
48
  parser.parse(data)
49
49
  end
50
+
51
+ def allows_script?
52
+ false
53
+ end
50
54
  end
51
55
  end
data/lib/tilt/rdoc.rb CHANGED
@@ -4,23 +4,34 @@ module Tilt
4
4
  # RDoc template. See:
5
5
  # http://rdoc.rubyforge.org/
6
6
  #
7
- # It's suggested that your program require 'rdoc/markup' and
8
- # 'rdoc/markup/to_html' at load time when using this template
9
- # engine.
7
+ # It's suggested that your program `require 'rdoc/markup'` and
8
+ # `require 'rdoc/markup/to_html'` at load time when using this template
9
+ # engine in a threaded environment.
10
10
  class RDocTemplate < Template
11
11
  self.default_mime_type = 'text/html'
12
12
 
13
13
  def self.engine_initialized?
14
- defined? ::RDoc::Markup
14
+ defined? ::RDoc::Markup::ToHtml
15
15
  end
16
16
 
17
17
  def initialize_engine
18
+ require_template_library 'rdoc'
18
19
  require_template_library 'rdoc/markup'
19
20
  require_template_library 'rdoc/markup/to_html'
20
21
  end
21
22
 
23
+ def markup
24
+ begin
25
+ # RDoc 4.0
26
+ require 'rdoc/options'
27
+ RDoc::Markup::ToHtml.new(RDoc::Options.new, nil)
28
+ rescue ArgumentError
29
+ # RDoc < 4.0
30
+ RDoc::Markup::ToHtml.new
31
+ end
32
+ end
33
+
22
34
  def prepare
23
- markup = RDoc::Markup::ToHtml.new
24
35
  @engine = markup.convert(data)
25
36
  @output = nil
26
37
  end
@@ -28,5 +39,9 @@ module Tilt
28
39
  def evaluate(scope, locals, &block)
29
40
  @output ||= @engine.to_s
30
41
  end
42
+
43
+ def allows_script?
44
+ false
45
+ end
31
46
  end
32
47
  end
data/lib/tilt/template.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Tilt
2
- TOPOBJECT = defined?(BasicObject) ? BasicObject : Object
2
+ TOPOBJECT = Object.superclass || Object
3
3
 
4
4
  # Base class for template implementations. Subclasses must implement
5
5
  # the #prepare method and one of the #evaluate or #precompiled_template
@@ -43,6 +43,7 @@ module Tilt
43
43
  when arg.respond_to?(:to_str) ; @file = arg.to_str
44
44
  when arg.respond_to?(:to_int) ; @line = arg.to_int
45
45
  when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
46
+ when arg.respond_to?(:path) ; @file = arg.path
46
47
  else raise TypeError
47
48
  end
48
49
  end
@@ -64,11 +65,37 @@ module Tilt
64
65
  @default_encoding = @options.delete :default_encoding
65
66
 
66
67
  # load template data and prepare (uses binread to avoid encoding issues)
67
- @reader = block || lambda { |t| File.respond_to?(:binread) ? File.binread(@file) : File.read(@file) }
68
+ @reader = block || lambda { |t| read_template_file }
68
69
  @data = @reader.call(self)
70
+
71
+ if @data.respond_to?(:force_encoding)
72
+ @data.force_encoding(default_encoding) if default_encoding
73
+
74
+ if !@data.valid_encoding?
75
+ raise Encoding::InvalidByteSequenceError, "#{eval_file} is not valid #{@data.encoding}"
76
+ end
77
+ end
78
+
69
79
  prepare
70
80
  end
71
81
 
82
+ # The encoding of the source data. Defaults to the
83
+ # default_encoding-option if present. You may override this method
84
+ # in your template class if you have a better hint of the data's
85
+ # encoding.
86
+ def default_encoding
87
+ @default_encoding
88
+ end
89
+
90
+ def read_template_file
91
+ data = File.open(file, 'rb') { |io| io.read }
92
+ if data.respond_to?(:force_encoding)
93
+ # Set it to the default external (without verifying)
94
+ data.force_encoding(Encoding.default_external) if Encoding.default_external
95
+ end
96
+ data
97
+ end
98
+
72
99
  # Render the template in the given scope with the locals specified. If a
73
100
  # block is given, it is typically available within the template via
74
101
  # +yield+.
@@ -91,6 +118,15 @@ module Tilt
91
118
  file || '(__TEMPLATE__)'
92
119
  end
93
120
 
121
+ # Whether or not this template engine allows executing Ruby script
122
+ # within the template. If this is false, +scope+ and +locals+ will
123
+ # generally not be used, nor will the provided block be avaiable
124
+ # via +yield+.
125
+ # This should be overridden by template subclasses.
126
+ def allows_script?
127
+ true
128
+ end
129
+
94
130
  protected
95
131
  # Called once and only once for each template subclass the first time
96
132
  # the template class is initialized. This should be used to require the
@@ -98,7 +134,7 @@ module Tilt
98
134
  def initialize_engine
99
135
  end
100
136
 
101
- # Like Kernel::require but issues a warning urging a manual require when
137
+ # Like Kernel#require but issues a warning urging a manual require when
102
138
  # running under a threaded environment.
103
139
  def require_template_library(name)
104
140
  if Thread.list.size > 1
@@ -123,25 +159,15 @@ module Tilt
123
159
  end
124
160
  end
125
161
 
162
+ # Execute the compiled template and return the result string. Template
163
+ # evaluation is guaranteed to be performed in the scope object with the
164
+ # locals specified and with support for yielding to the block.
165
+ #
166
+ # This method is only used by source generating templates. Subclasses that
167
+ # override render() may not support all features.
126
168
  def evaluate(scope, locals, &block)
127
- cached_evaluate(scope, locals, &block)
128
- end
129
-
130
- # Process the template and return the result. The first time this
131
- # method is called, the template source is evaluated with instance_eval.
132
- # On the sequential method calls it will compile the template to an
133
- # unbound method which will lead to better performance. In any case,
134
- # template executation is guaranteed to be performed in the scope object
135
- # with the locals specified and with support for yielding to the block.
136
- def cached_evaluate(scope, locals, &block)
137
- # Redefine itself to use method compilation the next time:
138
- def self.cached_evaluate(scope, locals, &block)
139
- method = compiled_method(locals.keys)
140
- method.bind(scope).call(locals, &block)
141
- end
142
-
143
- # Use instance_eval the first time:
144
- evaluate_source(scope, locals, &block)
169
+ method = compiled_method(locals.keys)
170
+ method.bind(scope).call(locals, &block)
145
171
  end
146
172
 
147
173
  # Generates all template source by combining the preamble, template, and
@@ -156,26 +182,29 @@ module Tilt
156
182
  def precompiled(locals)
157
183
  preamble = precompiled_preamble(locals)
158
184
  template = precompiled_template(locals)
159
- magic_comment = extract_magic_comment(template)
160
- if magic_comment
161
- # Magic comment e.g. "# coding: utf-8" has to be in the first line.
162
- # So we copy the magic comment to the first line.
163
- preamble = magic_comment + "\n" + preamble
185
+ postamble = precompiled_postamble(locals)
186
+ source = ''
187
+
188
+ # Ensure that our generated source code has the same encoding as the
189
+ # the source code generated by the template engine.
190
+ if source.respond_to?(:force_encoding)
191
+ template_encoding = extract_encoding(template)
192
+
193
+ source.force_encoding(template_encoding)
194
+ template.force_encoding(template_encoding)
164
195
  end
165
- parts = [
166
- preamble,
167
- template,
168
- precompiled_postamble(locals)
169
- ]
170
- [parts.join("\n"), preamble.count("\n") + 1]
196
+
197
+ source << preamble << "\n" << template << "\n" << postamble
198
+
199
+ [source, preamble.count("\n")+1]
171
200
  end
172
201
 
173
202
  # A string containing the (Ruby) source code for the template. The
174
- # default Template#evaluate implementation requires either this method
175
- # or the #precompiled method be overridden. When defined, the base
176
- # Template guarantees correct file/line handling, locals support, custom
177
- # scopes, and support for template compilation when the scope object
178
- # allows it.
203
+ # default Template#evaluate implementation requires either this
204
+ # method or the #precompiled method be overridden. When defined,
205
+ # the base Template guarantees correct file/line handling, locals
206
+ # support, custom scopes, proper encoding, and support for template
207
+ # compilation.
179
208
  def precompiled_template(locals)
180
209
  raise NotImplementedError
181
210
  end
@@ -186,7 +215,13 @@ module Tilt
186
215
  # source line offset, so adding code to the preamble does not effect line
187
216
  # reporting in Kernel::caller and backtraces.
188
217
  def precompiled_preamble(locals)
189
- locals.map { |k,v| "#{k} = locals[#{k.inspect}]" }.join("\n")
218
+ locals.map do |k,v|
219
+ if k.to_s =~ /\A[a-z_][a-zA-Z_0-9]*\z/
220
+ "#{k} = locals[#{k.inspect}]"
221
+ else
222
+ raise "invalid locals key: #{k.inspect} (keys must be variable names)"
223
+ end
224
+ end.join("\n")
190
225
  end
191
226
 
192
227
  # Generates postamble code for the precompiled template source. The
@@ -203,47 +238,27 @@ module Tilt
203
238
  end
204
239
 
205
240
  private
206
- # Evaluate the template source in the context of the scope object.
207
- def evaluate_source(scope, locals, &block)
241
+ def compile_template_method(locals)
208
242
  source, offset = precompiled(locals)
209
- scope.instance_eval(source, eval_file, line - offset)
210
- end
243
+ method_name = "__tilt_#{Thread.current.object_id.abs}"
244
+ method_source = ""
211
245
 
212
- # JRuby doesn't allow Object#instance_eval to yield to the block it's
213
- # closed over. This is by design and (ostensibly) something that will
214
- # change in MRI, though no current MRI version tested (1.8.6 - 1.9.2)
215
- # exhibits the behavior. More info here:
216
- #
217
- # http://jira.codehaus.org/browse/JRUBY-2599
218
- #
219
- # We redefine evaluate_source to work around this issues.
220
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
221
- undef evaluate_source
222
- def evaluate_source(scope, locals, &block)
223
- source, offset = precompiled(locals)
224
- file, lineno = eval_file, (line - offset)
225
- scope.instance_eval { Kernel::eval(source, binding, file, lineno) }
246
+ if method_source.respond_to?(:force_encoding)
247
+ method_source.force_encoding(source.encoding)
226
248
  end
227
- end
228
249
 
229
- def compile_template_method(locals)
230
- source, offset = precompiled(locals)
231
- offset += 5
232
- method_name = "__tilt_#{Thread.current.object_id.abs}"
233
- Object.class_eval <<-RUBY, eval_file, line - offset
234
- #{extract_magic_comment source}
250
+ method_source << <<-RUBY
235
251
  TOPOBJECT.class_eval do
236
252
  def #{method_name}(locals)
237
253
  Thread.current[:tilt_vars] = [self, locals]
238
254
  class << self
239
255
  this, locals = Thread.current[:tilt_vars]
240
256
  this.instance_eval do
241
- #{source}
242
- end
243
- end
244
- end
245
- end
246
257
  RUBY
258
+ offset += method_source.count("\n")
259
+ method_source << source
260
+ method_source << "\nend;end;end;end"
261
+ Object.class_eval method_source, eval_file, line - offset
247
262
  unbind_compiled_method(method_name)
248
263
  end
249
264
 
@@ -253,33 +268,22 @@ module Tilt
253
268
  method
254
269
  end
255
270
 
256
- def extract_magic_comment(script)
257
- comment = script.slice(/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/)
258
- return comment if comment and not %w[ascii-8bit binary].include?($1.downcase)
259
- "# coding: #{@default_encoding}" if @default_encoding
271
+ def extract_encoding(script)
272
+ extract_magic_comment(script) || script.encoding
260
273
  end
261
274
 
262
- # Special case Ruby 1.9.1's broken yield.
263
- #
264
- # http://github.com/rtomayko/tilt/commit/20c01a5
265
- # http://redmine.ruby-lang.org/issues/show/3601
266
- #
267
- # Remove when 1.9.2 dominates 1.9.1 installs in the wild.
268
- if RUBY_VERSION =~ /^1.9.1/
269
- undef compile_template_method
270
- def compile_template_method(locals)
271
- source, offset = precompiled(locals)
272
- offset += 1
273
- method_name = "__tilt_#{Thread.current.object_id}"
274
- Object.class_eval <<-RUBY, eval_file, line - offset
275
- TOPOBJECT.class_eval do
276
- def #{method_name}(locals)
277
- #{source}
278
- end
279
- end
280
- RUBY
281
- unbind_compiled_method(method_name)
275
+ def extract_magic_comment(script)
276
+ binary script do
277
+ script[/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/n, 1]
282
278
  end
283
279
  end
280
+
281
+ def binary(string)
282
+ original_encoding = string.encoding
283
+ string.force_encoding(Encoding::BINARY)
284
+ yield
285
+ ensure
286
+ string.force_encoding(original_encoding)
287
+ end
284
288
  end
285
289
  end
data/lib/tilt/textile.rb CHANGED
@@ -14,12 +14,17 @@ module Tilt
14
14
 
15
15
  def prepare
16
16
  @engine = RedCloth.new(data)
17
+ options.each {|k, v| @engine.send("#{k}=", v) if @engine.respond_to? "#{k}="}
17
18
  @output = nil
18
19
  end
19
20
 
20
21
  def evaluate(scope, locals, &block)
21
22
  @output ||= @engine.to_html
22
23
  end
24
+
25
+ def allows_script?
26
+ false
27
+ end
23
28
  end
24
29
  end
25
30
 
data/lib/tilt/wiki.rb CHANGED
@@ -24,6 +24,10 @@ module Tilt
24
24
  def evaluate(scope, locals, &block)
25
25
  @output ||= @engine.to_html
26
26
  end
27
+
28
+ def allows_script?
29
+ false
30
+ end
27
31
  end
28
32
 
29
33
  # WikiCloth implementation. See:
@@ -46,5 +50,9 @@ module Tilt
46
50
  def evaluate(scope, locals, &block)
47
51
  @output ||= @engine.to_html
48
52
  end
53
+
54
+ def allows_script?
55
+ false
56
+ end
49
57
  end
50
58
  end
data/lib/tilt.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Tilt
2
- VERSION = '1.3.3'
2
+ VERSION = '1.4.0'
3
3
 
4
4
  @preferred_mappings = Hash.new
5
5
  @template_mappings = Hash.new { |h, k| h[k] = [] }
@@ -142,6 +142,9 @@ module Tilt
142
142
  register ERBTemplate, 'erb', 'rhtml'
143
143
  register ErubisTemplate, 'erb', 'rhtml', 'erubis'
144
144
 
145
+ require 'tilt/etanni'
146
+ register EtanniTemplate, 'etn', 'etanni'
147
+
145
148
  require 'tilt/haml'
146
149
  register HamlTemplate, 'haml'
147
150
 
@@ -150,6 +153,9 @@ module Tilt
150
153
  register ScssTemplate, 'scss'
151
154
  register LessTemplate, 'less'
152
155
 
156
+ require 'tilt/csv'
157
+ register CSVTemplate, 'rcsv'
158
+
153
159
  require 'tilt/coffee'
154
160
  register CoffeeScriptTemplate, 'coffee'
155
161
 
@@ -173,6 +179,8 @@ module Tilt
173
179
  register KramdownTemplate, 'markdown', 'mkd', 'md'
174
180
  register BlueClothTemplate, 'markdown', 'mkd', 'md'
175
181
  register RDiscountTemplate, 'markdown', 'mkd', 'md'
182
+ register RedcarpetTemplate::Redcarpet1, 'markdown', 'mkd', 'md'
183
+ register RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
176
184
  register RedcarpetTemplate, 'markdown', 'mkd', 'md'
177
185
 
178
186
  require 'tilt/textile'
@@ -187,4 +195,10 @@ module Tilt
187
195
 
188
196
  require 'tilt/yajl'
189
197
  register YajlTemplate, 'yajl'
198
+
199
+ require 'tilt/asciidoc'
200
+ register AsciidoctorTemplate, 'ad', 'adoc', 'asciidoc'
201
+
202
+ require 'tilt/plain'
203
+ register PlainTemplate, 'html'
190
204
  end
@@ -0,0 +1,44 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'asciidoctor'
6
+
7
+ class AsciidoctorTemplateTest < Test::Unit::TestCase
8
+ HTML5_OUTPUT = "<div class=\"sect1\"><h2 id=\"_hello_world\">Hello World!</h2><div class=\"sectionbody\"></div></div>"
9
+ DOCBOOK_OUTPUT = "<section id=\"_hello_world\"><title>Hello World!</title></section>"
10
+
11
+ def strip_space(str)
12
+ str.gsub(/>\s+</, '><').strip
13
+ end
14
+
15
+ test "registered for '.ad' files" do
16
+ assert Tilt.mappings['ad'].include?(Tilt::AsciidoctorTemplate)
17
+ end
18
+
19
+ test "registered for '.adoc' files" do
20
+ assert Tilt.mappings['adoc'].include?(Tilt::AsciidoctorTemplate)
21
+ end
22
+
23
+ test "registered for '.asciidoc' files" do
24
+ assert Tilt.mappings['asciidoc'].include?(Tilt::AsciidoctorTemplate)
25
+ end
26
+
27
+ test "preparing and evaluating html5 templates on #render" do
28
+ template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" }
29
+ assert_equal HTML5_OUTPUT, strip_space(template.render)
30
+ end
31
+
32
+ test "preparing and evaluating docbook templates on #render" do
33
+ template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'docbook'}) { |t| "== Hello World!" }
34
+ assert_equal DOCBOOK_OUTPUT, strip_space(template.render)
35
+ end
36
+
37
+ test "can be rendered more than once" do
38
+ template = Tilt::AsciidoctorTemplate.new(:attributes => {"backend" => 'html5'}) { |t| "== Hello World!" }
39
+ 3.times { assert_equal HTML5_OUTPUT, strip_space(template.render) }
40
+ end
41
+ end
42
+ rescue LoadError => boom
43
+ warn "Tilt::AsciidoctorTemplate (disabled)"
44
+ end
@@ -41,5 +41,5 @@ begin
41
41
  end
42
42
  end
43
43
  rescue LoadError => boom
44
- warn "Tilt::BlueClothTemplate (disabled)\n"
44
+ warn "Tilt::BlueClothTemplate (disabled)"
45
45
  end
@@ -5,10 +5,25 @@ begin
5
5
  require 'coffee_script'
6
6
 
7
7
  class CoffeeScriptTemplateTest < Test::Unit::TestCase
8
+
9
+ unless method_defined?(:assert_not_match)
10
+ # assert_not_match is missing on 1.8.7, which uses assert_no_match
11
+ def assert_not_match(a, b)
12
+ unless a.kind_of?(Regexp)
13
+ a = Regexp.new(Regexp.escape(a))
14
+ end
15
+ assert_no_match(a,b)
16
+ end
17
+ end
18
+
8
19
  test "is registered for '.coffee' files" do
9
20
  assert_equal Tilt::CoffeeScriptTemplate, Tilt['test.coffee']
10
21
  end
11
22
 
23
+ test "bare is disabled by default" do
24
+ assert_equal false, Tilt::CoffeeScriptTemplate.default_bare
25
+ end
26
+
12
27
  test "compiles and evaluates the template on #render" do
13
28
  template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
14
29
  assert_match "puts('Hello, World!');", template.render
@@ -20,18 +35,25 @@ begin
20
35
  end
21
36
 
22
37
  test "disabling coffee-script wrapper" do
23
- str = "puts 'Hello, World!'\n"
38
+ str = 'name = "Josh"; puts "Hello #{name}"'
39
+
40
+ template = Tilt::CoffeeScriptTemplate.new { str }
41
+ assert_match "(function() {", template.render
42
+ assert_match "puts(\"Hello \" + name);\n", template.render
24
43
 
25
44
  template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
26
- assert_equal "puts('Hello, World!');", template.render
45
+ assert_not_match "(function() {", template.render
46
+ assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
27
47
 
28
48
  template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
29
- assert_equal "puts('Hello, World!');", template.render
49
+ assert_not_match "(function() {", template.render
50
+ assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
30
51
  end
31
52
 
32
- context "disabling coffee-script wrapper globally" do
53
+ context "wrapper globally enabled" do
33
54
  setup do
34
55
  @bare = Tilt::CoffeeScriptTemplate.default_bare
56
+ Tilt::CoffeeScriptTemplate.default_bare = false
35
57
  end
36
58
 
37
59
  teardown do
@@ -39,23 +61,54 @@ begin
39
61
  end
40
62
 
41
63
  test "no options" do
42
- template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
43
- assert_match "puts('Hello, World!');", template.render
64
+ template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
65
+ assert_match "puts(\"Hello, \" + name);", template.render
44
66
  assert_match "(function() {", template.render
45
67
  end
46
68
 
47
69
  test "overridden by :bare" do
48
- template = Tilt::CoffeeScriptTemplate.new(:bare => false) { "puts 'Hello, World!'\n" }
49
- assert_not_equal "puts('Hello, World!');", template.render
70
+ template = Tilt::CoffeeScriptTemplate.new(:bare => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
71
+ assert_match "puts(\"Hello, \" + name);", template.render
72
+ assert_not_match "(function() {", template.render
50
73
  end
51
74
 
52
75
  test "overridden by :no_wrap" do
53
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { "puts 'Hello, World!'\n" }
54
- assert_not_equal "puts('Hello, World!');", template.render
76
+ template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
77
+ assert_match "puts(\"Hello, \" + name);", template.render
78
+ assert_not_match "(function() {", template.render
79
+ end
80
+ end
81
+
82
+ context "wrapper globally disabled" do
83
+ setup do
84
+ @bare = Tilt::CoffeeScriptTemplate.default_bare
85
+ Tilt::CoffeeScriptTemplate.default_bare = true
86
+ end
87
+
88
+ teardown do
89
+ Tilt::CoffeeScriptTemplate.default_bare = @bare
90
+ end
91
+
92
+ test "no options" do
93
+ template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
94
+ assert_match "puts(\"Hello, \" + name);", template.render
95
+ assert_not_match "(function() {", template.render
96
+ end
97
+
98
+ test "overridden by :bare" do
99
+ template = Tilt::CoffeeScriptTemplate.new(:bare => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
100
+ assert_match "puts(\"Hello, \" + name);", template.render
101
+ assert_match "(function() {", template.render
102
+ end
103
+
104
+ test "overridden by :no_wrap" do
105
+ template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
106
+ assert_match "puts(\"Hello, \" + name);", template.render
107
+ assert_match "(function() {", template.render
55
108
  end
56
109
  end
57
110
  end
58
111
 
59
112
  rescue LoadError => boom
60
- warn "Tilt::CoffeeScriptTemplate (disabled)\n"
113
+ warn "Tilt::CoffeeScriptTemplate (disabled)"
61
114
  end
@@ -24,5 +24,5 @@ begin
24
24
  end
25
25
  end
26
26
  rescue LoadError => boom
27
- warn "Tilt::CreoleTemplate (disabled)\n"
27
+ warn "Tilt::CreoleTemplate (disabled)"
28
28
  end