tilt 2.1.0 → 2.2.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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +1 -0
  3. data/bin/tilt +2 -120
  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 +5 -13
  10. data/lib/tilt/builder.rb +18 -13
  11. data/lib/tilt/cli.rb +134 -0
  12. data/lib/tilt/coffee.rb +18 -25
  13. data/lib/tilt/commonmarker.rb +47 -81
  14. data/lib/tilt/creole.rb +9 -20
  15. data/lib/tilt/csv.rb +5 -4
  16. data/lib/tilt/erb.rb +18 -12
  17. data/lib/tilt/erubi.rb +7 -6
  18. data/lib/tilt/erubis.rb +12 -6
  19. data/lib/tilt/etanni.rb +3 -2
  20. data/lib/tilt/haml.rb +12 -9
  21. data/lib/tilt/kramdown.rb +8 -20
  22. data/lib/tilt/liquid.rb +10 -14
  23. data/lib/tilt/livescript.rb +8 -20
  24. data/lib/tilt/mapping.rb +180 -104
  25. data/lib/tilt/markaby.rb +5 -7
  26. data/lib/tilt/maruku.rb +5 -19
  27. data/lib/tilt/nokogiri.rb +11 -10
  28. data/lib/tilt/pandoc.rb +33 -51
  29. data/lib/tilt/pipeline.rb +1 -0
  30. data/lib/tilt/plain.rb +4 -15
  31. data/lib/tilt/prawn.rb +15 -23
  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 +25 -67
  36. data/lib/tilt/redcloth.rb +9 -19
  37. data/lib/tilt/rst-pandoc.rb +6 -19
  38. data/lib/tilt/sass.rb +34 -43
  39. data/lib/tilt/slim.rb +5 -0
  40. data/lib/tilt/string.rb +4 -3
  41. data/lib/tilt/template.rb +146 -56
  42. data/lib/tilt/typescript.rb +11 -18
  43. data/lib/tilt/wikicloth.rb +7 -19
  44. data/lib/tilt/yajl.rb +5 -11
  45. data/lib/tilt.rb +57 -30
  46. metadata +10 -7
  47. data/lib/tilt/bluecloth.rb +0 -26
  48. data/lib/tilt/less.rb +0 -32
  49. data/lib/tilt/sigil.rb +0 -36
data/lib/tilt/sass.rb CHANGED
@@ -1,17 +1,31 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
 
3
4
  module Tilt
4
- # Sass template implementation. See:
5
- # http://haml.hamptoncatlin.com/
5
+ # Sass template implementation for generating CSS. See: https://sass-lang.com/
6
6
  #
7
7
  # Sass templates do not support object scopes, locals, or yield.
8
- class SassTemplate < Template
8
+ class SassTemplate < StaticTemplate
9
9
  self.default_mime_type = 'text/css'
10
10
 
11
11
  begin
12
12
  require 'sass-embedded'
13
+ # :nocov:
13
14
  require 'uri'
14
- Engine = nil
15
+
16
+ private
17
+
18
+ def _prepare_output
19
+ ::Sass.compile_string(@data, **sass_options).css
20
+ end
21
+
22
+ def sass_options
23
+ path = File.absolute_path(eval_file)
24
+ path = '/' + path unless path.start_with?('/')
25
+ @options[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s
26
+ @options[:syntax] = :indented
27
+ @options
28
+ end
15
29
  rescue LoadError => err
16
30
  begin
17
31
  require 'sassc'
@@ -24,55 +38,32 @@ module Tilt
24
38
  raise err
25
39
  end
26
40
  end
27
- end
28
-
29
- def prepare
30
- @engine = unless Engine.nil?
31
- Engine.new(data, sass_options)
32
- end
33
- end
34
-
35
- def evaluate(scope, locals, &block)
36
- @output ||= if @engine.nil?
37
- ::Sass.compile_string(data, **sass_embedded_options).css
38
- else
39
- @engine.render
40
- end
41
- end
42
41
 
43
- def allows_script?
44
- false
45
- end
46
-
47
- private
48
- def eval_file_url
49
- path = File.absolute_path(eval_file)
50
- path = '/' + path unless path.start_with?('/')
51
- ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s
52
- end
42
+ private
53
43
 
54
- def sass_embedded_options
55
- options.merge(:url => eval_file_url, :syntax => :indented)
56
- end
44
+ def _prepare_output
45
+ Engine.new(@data, sass_options).render
46
+ end
57
47
 
58
- def sass_options
59
- options.merge(:filename => eval_file, :line => line, :syntax => :sass)
48
+ def sass_options
49
+ @options[:filename] = eval_file
50
+ @options[:line] = @line
51
+ @options[:syntax] = :sass
52
+ @options
53
+ end
54
+ # :nocov:
60
55
  end
61
56
  end
62
57
 
63
- # Sass's new .scss type template implementation.
64
58
  class ScssTemplate < SassTemplate
65
59
  self.default_mime_type = 'text/css'
66
60
 
67
- private
68
- def sass_embedded_options
69
- options.merge(:url => eval_file_url, :syntax => :scss)
70
- end
61
+ private
71
62
 
72
63
  def sass_options
73
- options.merge(:filename => eval_file, :line => line, :syntax => :scss)
64
+ super
65
+ @options[:syntax] = :scss
66
+ @options
74
67
  end
75
68
  end
76
-
77
69
  end
78
-
data/lib/tilt/slim.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
3
+ require 'slim'
4
+
5
+ Tilt::SlimTemplate = Slim::Template
data/lib/tilt/string.rb CHANGED
@@ -1,12 +1,13 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
 
3
4
  module Tilt
4
5
  # The template source is evaluated as a Ruby string. The #{} interpolation
5
6
  # syntax can be used to generated dynamic output.
6
7
  class StringTemplate < Template
7
8
  def prepare
8
- hash = "TILT#{data.hash.abs}"
9
- @code = String.new("<<#{hash}.chomp\n#{data}\n#{hash}")
9
+ hash = "TILT#{@data.hash.abs}"
10
+ @code = String.new("<<#{hash}.chomp\n#{@data}\n#{hash}")
10
11
  end
11
12
 
12
13
  def precompiled_template(locals)
data/lib/tilt/template.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Tilt
2
3
  # @private
3
4
  module CompiledTemplates
@@ -40,12 +41,12 @@ module Tilt
40
41
  @metadata ||= {}
41
42
  end
42
43
 
43
- # @deprecated Use `.metadata[:mime_type]` instead.
44
+ # Use `.metadata[:mime_type]` instead.
44
45
  def default_mime_type
45
46
  metadata[:mime_type]
46
47
  end
47
48
 
48
- # @deprecated Use `.metadata[:mime_type] = val` instead.
49
+ # Use `.metadata[:mime_type] = val` instead.
49
50
  def default_mime_type=(value)
50
51
  metadata[:mime_type] = value
51
52
  end
@@ -57,33 +58,28 @@ module Tilt
57
58
  # a block is required.
58
59
  #
59
60
  # All arguments are optional.
60
- def initialize(file=nil, line=1, options={}, &block)
61
- @file, @line, @options = nil, 1, {}
61
+ def initialize(file=nil, line=nil, options=nil)
62
+ @file, @line, @options = nil, 1, nil
62
63
 
63
- [options, line, file].compact.each do |arg|
64
- case
65
- when arg.respond_to?(:to_str) ; @file = arg.to_str
66
- when arg.respond_to?(:to_int) ; @line = arg.to_int
67
- when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
68
- when arg.respond_to?(:path) ; @file = arg.path
69
- when arg.respond_to?(:to_path) ; @file = arg.to_path
70
- else raise TypeError, "Can't load the template file. Pass a string with a path " +
71
- "or an object that responds to 'to_str', 'path' or 'to_path'"
72
- end
73
- end
64
+ process_arg(options)
65
+ process_arg(line)
66
+ process_arg(file)
74
67
 
75
- raise ArgumentError, "file or block required" if (@file || block).nil?
68
+ raise ArgumentError, "file or block required" unless @file || block_given?
76
69
 
77
- # used to hold compiled template methods
78
- @compiled_method = {}
70
+ @options ||= {}
71
+
72
+ set_compiled_method_cache
79
73
 
80
- # used on 1.9 to set the encoding if it is not set elsewhere (like a magic comment)
81
- # currently only used if template compiles to ruby
74
+ # Force the encoding of the input data
82
75
  @default_encoding = @options.delete :default_encoding
83
76
 
77
+ # Skip encoding detection from magic comments and forcing that encoding
78
+ # for compiled templates
79
+ @skip_compiled_encoding_detection = @options.delete :skip_compiled_encoding_detection
80
+
84
81
  # load template data and prepare (uses binread to avoid encoding issues)
85
- @reader = block || lambda { |t| read_template_file }
86
- @data = @reader.call(self)
82
+ @data = block_given? ? yield(self) : read_template_file
87
83
 
88
84
  if @data.respond_to?(:force_encoding)
89
85
  if default_encoding
@@ -102,28 +98,29 @@ module Tilt
102
98
  # Render the template in the given scope with the locals specified. If a
103
99
  # block is given, it is typically available within the template via
104
100
  # +yield+.
105
- def render(scope=nil, locals={}, &block)
106
- scope ||= Object.new
101
+ def render(scope=nil, locals=nil, &block)
107
102
  current_template = Thread.current[:tilt_current_template]
108
103
  Thread.current[:tilt_current_template] = self
109
- evaluate(scope, locals || {}, &block)
104
+ evaluate(scope || Object.new, locals || EMPTY_HASH, &block)
110
105
  ensure
111
106
  Thread.current[:tilt_current_template] = current_template
112
107
  end
113
108
 
114
109
  # The basename of the template file.
115
110
  def basename(suffix='')
116
- File.basename(file, suffix) if file
111
+ File.basename(@file, suffix) if @file
117
112
  end
118
113
 
119
114
  # The template file's basename with all extensions chomped off.
120
115
  def name
121
- basename.split('.', 2).first if basename
116
+ if bname = basename
117
+ bname.split('.', 2).first
118
+ end
122
119
  end
123
120
 
124
121
  # The filename used in backtraces to describe the template.
125
122
  def eval_file
126
- file || '(__TEMPLATE__)'
123
+ @file || '(__TEMPLATE__)'
127
124
  end
128
125
 
129
126
  # An empty Hash that the template engine can populate with various
@@ -147,6 +144,24 @@ module Tilt
147
144
  @compiled_path = path
148
145
  end
149
146
 
147
+ # The compiled method for the locals keys and scope_class provided.
148
+ # Returns an UnboundMethod, which can be used to define methods
149
+ # directly on the scope class, which are much faster to call than
150
+ # Tilt's normal rendering.
151
+ def compiled_method(locals_keys, scope_class=nil)
152
+ key = [scope_class, locals_keys].freeze
153
+ LOCK.synchronize do
154
+ if meth = @compiled_method[key]
155
+ return meth
156
+ end
157
+ end
158
+ meth = compile_template_method(locals_keys, scope_class)
159
+ LOCK.synchronize do
160
+ @compiled_method[key] = meth
161
+ end
162
+ meth
163
+ end
164
+
150
165
  protected
151
166
 
152
167
  # @!group For template implementations
@@ -157,13 +172,16 @@ module Tilt
157
172
  # encoding.
158
173
  attr_reader :default_encoding
159
174
 
175
+ def skip_compiled_encoding_detection?
176
+ @skip_compiled_encoding_detection
177
+ end
178
+
160
179
  # Do whatever preparation is necessary to setup the underlying template
161
180
  # engine. Called immediately after template data is loaded. Instance
162
181
  # variables set in this method are available when #evaluate is called.
163
182
  #
164
- # Subclasses must provide an implementation of this method.
183
+ # Empty by default as some subclasses do not need separate preparation.
165
184
  def prepare
166
- raise NotImplementedError
167
185
  end
168
186
 
169
187
  CLASS_METHOD = Kernel.instance_method(:class)
@@ -183,14 +201,18 @@ module Tilt
183
201
  when Object
184
202
  scope_class = Module === scope ? scope : scope.class
185
203
  else
204
+ # :nocov:
186
205
  scope_class = USE_BIND_CALL ? CLASS_METHOD.bind_call(scope) : CLASS_METHOD.bind(scope).call
206
+ # :nocov:
187
207
  end
188
208
  method = compiled_method(locals_keys, scope_class)
189
209
 
190
210
  if USE_BIND_CALL
191
211
  method.bind_call(scope, locals, &block)
212
+ # :nocov:
192
213
  else
193
214
  method.bind(scope).call(locals, &block)
215
+ # :nocov:
194
216
  end
195
217
  end
196
218
 
@@ -209,15 +231,19 @@ module Tilt
209
231
  postamble = precompiled_postamble(local_keys)
210
232
  source = String.new
211
233
 
212
- # Ensure that our generated source code has the same encoding as the
213
- # the source code generated by the template engine.
214
- if source.respond_to?(:force_encoding)
215
- template_encoding = extract_encoding(template)
234
+ unless skip_compiled_encoding_detection?
235
+ # Ensure that our generated source code has the same encoding as the
236
+ # the source code generated by the template engine.
237
+ template_encoding = extract_encoding(template){|t| template = t}
216
238
 
217
- source.force_encoding(template_encoding)
218
- template.force_encoding(template_encoding)
239
+ if template.encoding != template_encoding
240
+ # template should never be frozen here. If it was frozen originally,
241
+ # then extract_encoding should yield a dup.
242
+ template.force_encoding(template_encoding)
243
+ end
219
244
  end
220
245
 
246
+ source.force_encoding(template.encoding)
221
247
  source << preamble << "\n" << template << "\n" << postamble
222
248
 
223
249
  [source, preamble.count("\n")+1]
@@ -245,30 +271,52 @@ module Tilt
245
271
 
246
272
  private
247
273
 
248
- def read_template_file
249
- data = File.open(file, 'rb') { |io| io.read }
250
- if data.respond_to?(:force_encoding)
251
- # Set it to the default external (without verifying)
252
- data.force_encoding(Encoding.default_external) if Encoding.default_external
274
+ def process_arg(arg)
275
+ if arg
276
+ case
277
+ when arg.respond_to?(:to_str) ; @file = arg.to_str
278
+ when arg.respond_to?(:to_int) ; @line = arg.to_int
279
+ when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
280
+ when arg.respond_to?(:path) ; @file = arg.path
281
+ when arg.respond_to?(:to_path) ; @file = arg.to_path
282
+ else raise TypeError, "Can't load the template file. Pass a string with a path " +
283
+ "or an object that responds to 'to_str', 'path' or 'to_path'"
284
+ end
253
285
  end
286
+ end
287
+
288
+ def read_template_file
289
+ data = File.binread(file)
290
+ # Set it to the default external (without verifying)
291
+ # :nocov:
292
+ data.force_encoding(Encoding.default_external) if Encoding.default_external
293
+ # :nocov:
254
294
  data
255
295
  end
256
296
 
257
- # The compiled method for the locals keys provided.
258
- def compiled_method(locals_keys, scope_class=nil)
259
- LOCK.synchronize do
260
- @compiled_method[[scope_class, locals_keys]] ||= compile_template_method(locals_keys, scope_class)
261
- end
297
+ def set_compiled_method_cache
298
+ @compiled_method = {}
262
299
  end
263
300
 
264
301
  def local_extraction(local_keys)
265
- local_keys.map do |k|
302
+ assignments = local_keys.map do |k|
266
303
  if k.to_s =~ /\A[a-z_][a-zA-Z_0-9]*\z/
267
304
  "#{k} = locals[#{k.inspect}]"
268
305
  else
269
306
  raise "invalid locals key: #{k.inspect} (keys must be variable names)"
270
307
  end
271
- end.join("\n")
308
+ end
309
+
310
+ s = "locals = locals[:locals]"
311
+ if assignments.delete(s)
312
+ # If there is a locals key itself named `locals`, delete it from the ordered keys so we can
313
+ # assign it last. This is important because the assignment of all other locals depends on the
314
+ # `locals` local variable still matching the `locals` method argument given to the method
315
+ # created in `#compile_template_method`.
316
+ assignments << s
317
+ end
318
+
319
+ assignments.join("\n")
272
320
  end
273
321
 
274
322
  def compile_template_method(local_keys, scope_class=nil)
@@ -277,10 +325,7 @@ module Tilt
277
325
 
278
326
  method_name = "__tilt_#{Thread.current.object_id.abs}"
279
327
  method_source = String.new
280
-
281
- if method_source.respond_to?(:force_encoding)
282
- method_source.force_encoding(source.encoding)
283
- end
328
+ method_source.force_encoding(source.encoding)
284
329
 
285
330
  if freeze_string_literals?
286
331
  method_source << "# frozen-string-literal: true\n"
@@ -293,11 +338,11 @@ module Tilt
293
338
  method_source << source
294
339
  method_source << "\nend;end;"
295
340
 
296
- bind_compiled_method(method_source, offset, scope_class, local_keys)
341
+ bind_compiled_method(method_source, offset, scope_class)
297
342
  unbind_compiled_method(method_name)
298
343
  end
299
344
 
300
- def bind_compiled_method(method_source, offset, scope_class, local_keys)
345
+ def bind_compiled_method(method_source, offset, scope_class)
301
346
  path = compiled_path
302
347
  if path && scope_class.name
303
348
  path = path.dup
@@ -340,11 +385,16 @@ module Tilt
340
385
  method
341
386
  end
342
387
 
343
- def extract_encoding(script)
344
- extract_magic_comment(script) || script.encoding
388
+ def extract_encoding(script, &block)
389
+ extract_magic_comment(script, &block) || script.encoding
345
390
  end
346
391
 
347
392
  def extract_magic_comment(script)
393
+ if script.frozen?
394
+ script = script.dup
395
+ yield script
396
+ end
397
+
348
398
  binary(script) do
349
399
  script[/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/n, 1]
350
400
  end
@@ -362,4 +412,44 @@ module Tilt
362
412
  string.force_encoding(original_encoding)
363
413
  end
364
414
  end
415
+
416
+ class StaticTemplate < Template
417
+ def self.subclass(mime_type: 'text/html', &block)
418
+ Class.new(self) do
419
+ self.default_mime_type = mime_type
420
+
421
+ private
422
+
423
+ define_method(:_prepare_output, &block)
424
+ end
425
+ end
426
+
427
+ # Static templates always return the prepared output.
428
+ def render(scope=nil, locals=nil)
429
+ @output
430
+ end
431
+
432
+ # Raise NotImplementedError, since static templates
433
+ # do not support compiled methods.
434
+ def compiled_method(locals_keys, scope_class=nil)
435
+ raise NotImplementedError
436
+ end
437
+
438
+ # Static templates never allow script.
439
+ def allows_script?
440
+ false
441
+ end
442
+
443
+ protected
444
+
445
+ def prepare
446
+ @output = _prepare_output
447
+ end
448
+
449
+ private
450
+
451
+ # Do nothing, since compiled method cache is not used.
452
+ def set_compiled_method_cache
453
+ end
454
+ end
365
455
  end
@@ -1,26 +1,19 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'typescript-node'
3
4
 
4
- module Tilt
5
- class TypeScriptTemplate < Template
6
- self.default_mime_type = 'application/javascript'
5
+ Tilt::TypeScriptTemplate = Tilt::StaticTemplate.subclass(mime_type: 'application/javascript') do
6
+ option_args = []
7
7
 
8
- def prepare
9
- @option_args = []
8
+ @options.each do |key, value|
9
+ next unless value
10
10
 
11
- options.each do |key, value|
12
- next unless value
11
+ option_args << "--#{key}"
13
12
 
14
- @option_args << "--#{key}"
15
-
16
- if value != true
17
- @option_args << value.to_s
18
- end
19
- end
20
- end
21
-
22
- def evaluate(scope, locals, &block)
23
- @output ||= TypeScript::Node.compile(data, *@option_args)
13
+ if value != true
14
+ option_args << value.to_s
24
15
  end
25
16
  end
17
+
18
+ TypeScript::Node.compile(@data, *option_args)
26
19
  end
@@ -1,22 +1,10 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'wikicloth'
3
4
 
4
- module Tilt
5
- # WikiCloth implementation. See:
6
- # http://redcloth.org/
7
- class WikiClothTemplate < Template
8
- def prepare
9
- @parser = options.delete(:parser) || WikiCloth::Parser
10
- @engine = @parser.new options.merge(:data => data)
11
- @output = nil
12
- end
13
-
14
- def evaluate(scope, locals, &block)
15
- @output ||= @engine.to_html
16
- end
17
-
18
- def allows_script?
19
- false
20
- end
21
- end
5
+ # WikiCloth implementation. See: https://github.com/nricciar/wikicloth
6
+ Tilt::WikiClothTemplate = Tilt::StaticTemplate.subclass do
7
+ parser = @options.delete(:parser) || WikiCloth::Parser
8
+ @options[:data] = @data
9
+ parser.new(@options).to_html
22
10
  end
data/lib/tilt/yajl.rb CHANGED
@@ -1,8 +1,8 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'yajl'
3
4
 
4
5
  module Tilt
5
-
6
6
  # Yajl Template implementation
7
7
  #
8
8
  # Yajl is a fast JSON parsing and encoding library for Ruby
@@ -40,14 +40,10 @@ module Tilt
40
40
  # template.render(self)
41
41
  #
42
42
  class YajlTemplate < Template
43
-
44
43
  self.default_mime_type = 'application/json'
45
44
 
46
- def prepare
47
- end
48
-
49
45
  def evaluate(scope, locals, &block)
50
- decorate super(scope, locals, &block)
46
+ decorate(super)
51
47
  end
52
48
 
53
49
  def precompiled_preamble(locals)
@@ -60,10 +56,9 @@ module Tilt
60
56
  end
61
57
 
62
58
  def precompiled_template(locals)
63
- data.to_str
59
+ @data.to_str
64
60
  end
65
61
 
66
-
67
62
  # Decorates the +json+ input according to given +options+.
68
63
  #
69
64
  # json - The json String to decorate.
@@ -71,7 +66,7 @@ module Tilt
71
66
  #
72
67
  # Returns the decorated String.
73
68
  def decorate(json)
74
- callback, variable = options[:callback], options[:variable]
69
+ callback, variable = @options[:callback], @options[:variable]
75
70
  if callback && variable
76
71
  "var #{variable} = #{json}; #{callback}(#{variable});"
77
72
  elsif variable
@@ -83,5 +78,4 @@ module Tilt
83
78
  end
84
79
  end
85
80
  end
86
-
87
81
  end