tilt 2.9.0 → 2.10.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07376d5da37cfc37ba1364a962859139ab42e2c6453aea4bd5610c2daccee019
4
- data.tar.gz: dc64be5c7fbf26c8f6def8e40c143a61de7e46ed69157d0b0a0c1a558b9e4d93
3
+ metadata.gz: 0202adaed9df38f951dba8a08ae37c6b0e4794027e07f8f1073763503da2f21c
4
+ data.tar.gz: c387711421b7d97fbbddda360bf4e3ca0b336277a538922640657485ae4d1d6a
5
5
  SHA512:
6
- metadata.gz: 20bc55f1b6cdceb99068e2a40ac0df35006b6f61c27a66f31606ee79e32df7431bef7e2d37ca6ecef5cc30ef131f7f19f340258d12a1836ce4aee2a1ad0a4b23
7
- data.tar.gz: 409ef4a5ba49ec95425d810987b892d694aa3794ce937256c7673caea67dc9ef93f64fb1b5b399de0a0ada44d13424d305d63f3e9b96a6f346739f8a7491c2b3
6
+ metadata.gz: e5ccd9b03732da4200837fa39c6c02d424778d31dd8bd0aec8d467508b1b54362c2c0eddde2def26ddd989f64df4508bb8431d9c6a1e913b6d8b06b653bb96b8
7
+ data.tar.gz: eb7b6ea7dbb9f556efc85e5c26056bacee34dd57c0dbce24e796acb4e2023843ff5852b4b56b93c12c92bd6099d9957b15621768178259ff31e5588d2798842c
@@ -105,7 +105,7 @@ if defined?(::Commonmarker)
105
105
 
106
106
  Commonmarker.to_html(@data, options: { parse: parse_options, render: render_options, extension: extensions })
107
107
  end
108
- # :nocov:
108
+ # simplecov:disable
109
109
  else
110
110
  aliases = {
111
111
  :smartypants => :SMART
@@ -157,4 +157,4 @@ else
157
157
  CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions)
158
158
  end
159
159
  end
160
- # :nocov:
160
+ # simplecov:enable
data/lib/tilt/erb.rb CHANGED
@@ -86,10 +86,10 @@ module Tilt
86
86
  end
87
87
  @engine = if SUPPORTS_KVARGS
88
88
  ::ERB.new(@data, trim_mode: trim, eoutvar: @outvar)
89
- # :nocov:
89
+ # simplecov:disable
90
90
  else
91
91
  ::ERB.new(@data, options[:safe], trim, @outvar)
92
- # :nocov:
92
+ # simplecov:enable
93
93
  end
94
94
  end
95
95
 
data/lib/tilt/mapping.rb CHANGED
@@ -138,6 +138,7 @@ module Tilt
138
138
  LOCK.synchronize do
139
139
  @template_map = other.template_map.dup
140
140
  @lazy_map = other.lazy_map.dup
141
+ @lazy_map.each {|ext, choices| @lazy_map[ext] = choices.dup}
141
142
  end
142
143
  end
143
144
 
@@ -149,12 +150,7 @@ module Tilt
149
150
  # not attempt to load additional files (useful when restricting
150
151
  # file system access after template libraries in use are loaded).
151
152
  def finalized
152
- LOCK.synchronize{@lazy_map.dup}.each do |pattern, classes|
153
- register_defined_classes(LOCK.synchronize{classes.map(&:first)}, pattern)
154
- end
155
-
156
- # Check if a template class is already present
157
- FinalizedMapping.new(LOCK.synchronize{@template_map.dup}.freeze)
153
+ dup.finalized!
158
154
  end
159
155
 
160
156
  # Registers a lazy template implementation by file extension. You
@@ -182,7 +178,7 @@ module Tilt
182
178
 
183
179
  v = [class_name, file].freeze
184
180
  extensions.each do |ext|
185
- LOCK.synchronize{@lazy_map[ext].unshift(v)}
181
+ LOCK.synchronize{@lazy_map[ext.to_s.downcase].unshift(v)}
186
182
  end
187
183
  end
188
184
 
@@ -204,7 +200,7 @@ module Tilt
204
200
  end
205
201
 
206
202
  extensions.each do |ext|
207
- ext = ext.to_s
203
+ ext = ext.to_s.downcase
208
204
  LOCK.synchronize do
209
205
  @template_map[ext] = template_class
210
206
  end
@@ -259,7 +255,7 @@ module Tilt
259
255
  # mapping['index.mt'] # => nil
260
256
  def unregister(*extensions)
261
257
  extensions.each do |ext|
262
- ext = ext.to_s
258
+ ext = ext.to_s.downcase
263
259
  LOCK.synchronize do
264
260
  @template_map.delete(ext)
265
261
  @lazy_map.delete(ext)
@@ -296,6 +292,19 @@ module Tilt
296
292
  res
297
293
  end
298
294
 
295
+ protected
296
+
297
+ # Internals of finalized
298
+ def finalized!
299
+ @lazy_map.each do |pattern, classes|
300
+ unless @template_map[pattern]
301
+ register_defined_classes(classes.map(&:first), pattern)
302
+ end
303
+ end
304
+
305
+ FinalizedMapping.new(@template_map.freeze)
306
+ end
307
+
299
308
  private
300
309
 
301
310
  def lazy?(ext)
@@ -331,9 +340,9 @@ module Tilt
331
340
  choices.each do |class_name, file|
332
341
  begin
333
342
  require file
334
- # It's safe to eval() here because constant_defined? will
335
- # raise NameError on invalid constant names
336
- template_class = eval(class_name)
343
+ unless template_class = constant_defined?(class_name)
344
+ raise NameError, "constant not defined: #{class_name.inspect}"
345
+ end
337
346
  rescue LoadError => ex
338
347
  first_failure ||= ex
339
348
  else
@@ -360,7 +369,9 @@ module Tilt
360
369
  def constant_defined?(name)
361
370
  name.split('::').inject(Object) do |scope, n|
362
371
  return false if scope.autoload?(n) || !scope.const_defined?(n)
363
- scope.const_get(n)
372
+ v = scope.const_get(n)
373
+ raise TypeError, "expected template constant to be a module" unless Module === v
374
+ v
364
375
  end
365
376
  end
366
377
  end
data/lib/tilt/pandoc.rb CHANGED
@@ -4,27 +4,27 @@
4
4
  #
5
5
  # {Markdown}[http://daringfireball.net/projects/markdown/syntax] is a
6
6
  # lightweight markup language, created by John Gruber and Aaron Swartz.
7
- # For any markup that is not covered by Markdown’s syntax, HTML is used.
7
+ # For any markup that is not covered by Markdown's syntax, HTML is used.
8
8
  # Marking up plain text with Markdown markup is easy and Markdown
9
9
  # formatted texts are readable.
10
10
  #
11
11
  # === Example
12
12
  #
13
- # Hello Markdown Templates
14
- # ========================
13
+ # Hello Markdown Templates
14
+ # ========================
15
15
  #
16
- # Hello World. This is a paragraph.
16
+ # Hello World. This is a paragraph.
17
17
  #
18
18
  # === Usage
19
19
  #
20
20
  # To wrap a Markdown formatted document with a layout:
21
21
  #
22
- # layout = Tilt['erb'].new do
23
- # "<!doctype html><title></title><%= yield %>"
24
- # end
25
- # data = Tilt['md'].new { "# hello tilt" }
26
- # layout.render { data.render }
27
- # # => "<!doctype html><title></title><h1>hello tilt</h1>\n"
22
+ # layout = Tilt['erb'].new do
23
+ # "<!doctype html><title></title><%= yield %>"
24
+ # end
25
+ # data = Tilt['md'].new { "# hello tilt" }
26
+ # layout.render { data.render }
27
+ # # => "<!doctype html><title></title><h1>hello tilt</h1>\n"
28
28
  #
29
29
  # === Options
30
30
  #
@@ -37,6 +37,19 @@
37
37
  # Set <tt>true</tt> disallow raw HTML in Markdown contents. HTML is converted to
38
38
  # literal text by escaping <tt><</tt> characters.
39
39
  #
40
+ # ==== <tt>:commonmark => true|false</tt>
41
+ #
42
+ # Set <tt>true</tt> to use commonmark as the format.
43
+ #
44
+ # ==== <tt>:markdown_strict => true|false</tt>
45
+ #
46
+ # Set <tt>true</tt> to use markdown_strict as the format.
47
+ #
48
+ # ==== Other options
49
+ #
50
+ # <tt>true</tt> values pass the key as an argument. <tt>false</tt> values are ignored.
51
+ # Other values pass the key as a long option, with the value as the option value.
52
+ #
40
53
  # === See also
41
54
  #
42
55
  # * {Markdown Syntax Documentation}[http://daringfireball.net/projects/markdown/syntax]
@@ -53,13 +66,14 @@ Tilt::PandocTemplate = Tilt::StaticTemplate.subclass do
53
66
  # Leave other hash keys untouched
54
67
  pandoc_options = []
55
68
  from = "markdown"
69
+ escape_html = false
56
70
  smart_extension = "-smart"
57
71
  @options.each do |k,v|
58
72
  case k
59
73
  when :smartypants
60
74
  smart_extension = "+smart" if v
61
75
  when :escape_html
62
- from = "markdown-raw_html" if v
76
+ escape_html = true if v
63
77
  when :commonmark
64
78
  from = "commonmark" if v
65
79
  when :markdown_strict
@@ -75,6 +89,11 @@ Tilt::PandocTemplate = Tilt::StaticTemplate.subclass do
75
89
  end
76
90
  end
77
91
  end
92
+ if escape_html
93
+ # pandoc's commonmark reader ignores -raw_html, so it cannot honor :escape_html
94
+ raise ArgumentError, ":escape_html is not supported with :commonmark" if from == "commonmark"
95
+ from += "-raw_html-raw_attribute"
96
+ end
78
97
  pandoc_options << { :f => from + smart_extension }
79
98
 
80
99
  PandocRuby.new(@data, *pandoc_options).to_html.strip
data/lib/tilt/pipeline.rb CHANGED
@@ -8,11 +8,11 @@ module Tilt
8
8
  def prepare
9
9
  @pipeline = self.class::TEMPLATES.inject(proc{|*| data}) do |data, (klass, ext, options)|
10
10
  proc do |s,l,&sb|
11
- options = options
11
+ render_options = options
12
12
  if ext_opts = @options[ext]
13
- options = options.merge(ext_opts)
13
+ render_options = render_options.merge(ext_opts)
14
14
  end
15
- klass.new(file, line, options, &proc{|*| data.call(s, l, &sb)}).render(s, l, &sb)
15
+ klass.new(file, line, render_options, &proc{|*| data.call(s, l, &sb)}).render(s, l, &sb)
16
16
  end
17
17
  end
18
18
  end
data/lib/tilt/radius.rb CHANGED
@@ -45,6 +45,15 @@
45
45
  # </body>
46
46
  # </html>
47
47
  #
48
+ # === Options
49
+ #
50
+ # ==== <tt>:restrict_scope_methods => true|false</tt>
51
+ #
52
+ # Set <tt>true</tt> to make tags that are not defined as locals only call
53
+ # scope methods that are not public methods of Object (such as +inspect+,
54
+ # +instance_variables+, and +freeze+). Use this when template authors are
55
+ # not trusted. Default is false.
56
+ #
48
57
  # === See also
49
58
  #
50
59
  # * {Radius}[http://radius.rubyforge.org]
@@ -65,7 +74,7 @@ module Tilt
65
74
  attr_accessor :tilt_scope
66
75
 
67
76
  def tag_missing(name, attributes)
68
- tilt_scope.__send__(name)
77
+ tilt_scope.public_send(name)
69
78
  end
70
79
 
71
80
  def dup
@@ -75,8 +84,16 @@ module Tilt
75
84
  end
76
85
  end
77
86
 
87
+ # Context used with the :restrict_scope_methods option.
88
+ class RestrictedContextClass < ContextClass
89
+ def tag_missing(name, attributes)
90
+ raise Radius::UndefinedTagError, name if Object.public_method_defined?(name)
91
+ super
92
+ end
93
+ end
94
+
78
95
  def evaluate(scope, locals, &block)
79
- context = ContextClass.new
96
+ context = (@options[:restrict_scope_methods] ? RestrictedContextClass : ContextClass).new
80
97
  context.tilt_scope = scope
81
98
  context.define_tag("yield", &block) if block
82
99
  locals.each do |tag, value|
data/lib/tilt/rdoc.rb CHANGED
@@ -32,10 +32,10 @@ Tilt::RDocTemplate = if defined?(RDoc::VERSION) && RDoc::VERSION >= "8"
32
32
  Tilt::StaticTemplate.subclass do
33
33
  RDoc::Markup::ToHtml.new.convert(@data).to_s
34
34
  end
35
- # :nocov:
35
+ # simplecov:disable
36
36
  else
37
37
  Tilt::StaticTemplate.subclass do
38
38
  RDoc::Markup::ToHtml.new(RDoc::Options.new, nil).convert(@data).to_s
39
39
  end
40
40
  end
41
- # :nocov:
41
+ # simplecov:enable
@@ -11,6 +11,9 @@
11
11
  # is an application written in Haskell, with a Ruby wrapper provided by the
12
12
  # {pandoc-ruby}[https://github.com/alphabetum/pandoc-ruby] gem.
13
13
  #
14
+ # If the +:sandbox+ option is given, Pandoc is run with <tt>--sandbox</tt>, so
15
+ # directives such as <tt>include</tt> cannot read local files. This requires Pandoc 2.15+.
16
+ #
14
17
  # === Example
15
18
  #
16
19
  # Hello Rst Templates
@@ -29,5 +32,7 @@ require_relative 'pandoc'
29
32
  rst = {:f => "rst"}.freeze
30
33
 
31
34
  Tilt::RstPandocTemplate = Tilt::StaticTemplate.subclass do
32
- PandocRuby.new(@data, rst).to_html.strip
35
+ args = [@data, rst]
36
+ args << :sandbox if @options[:sandbox]
37
+ PandocRuby.new(*args).to_html.strip
33
38
  end
data/lib/tilt/sass.rb CHANGED
@@ -23,7 +23,7 @@ module Tilt
23
23
 
24
24
  begin
25
25
  require 'sass-embedded'
26
- # :nocov:
26
+ # simplecov:disable
27
27
  require 'uri'
28
28
 
29
29
  ALLOWED_KEYS = (defined?(::Sass::Compiler) ? ::Sass::Compiler : ::Sass::Embedded).
@@ -43,7 +43,7 @@ module Tilt
43
43
  path = File.absolute_path(eval_file)
44
44
  path = '/' + path unless path.start_with?('/')
45
45
  opts = @options.dup
46
- opts[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s
46
+ opts[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path, /[^a-zA-Z0-9\-._~!$&'()*+,;=:@\/]/)]).to_s
47
47
  opts[:syntax] = :indented
48
48
  opts.delete_if{|k| !ALLOWED_KEYS.include?(k)} if ALLOWED_KEYS
49
49
  opts
@@ -73,7 +73,7 @@ module Tilt
73
73
  @options[:syntax] = :sass
74
74
  @options
75
75
  end
76
- # :nocov:
76
+ # simplecov:enable
77
77
  end
78
78
  end
79
79
 
data/lib/tilt/string.rb CHANGED
@@ -16,7 +16,8 @@ module Tilt
16
16
  def prepare
17
17
  hash = "TILT#{@data.hash.abs}"
18
18
  @freeze_string_literals = !!@options[:freeze]
19
- @code = String.new("<<#{hash}.chomp\n#{@data}\n#{hash}")
19
+ # Heredoc changes \r\n to \n, chomp removes \r\n
20
+ @code = String.new("<<#{hash}.chomp\n#{@data}\r\r\n#{hash}")
20
21
  end
21
22
 
22
23
  def precompiled_template(locals)
data/lib/tilt/template.rb CHANGED
@@ -31,7 +31,8 @@ module Tilt
31
31
  # A path ending in .rb that the template code will be written to, then
32
32
  # required, instead of being evaled. This is useful for determining
33
33
  # coverage of compiled template code, or to use static analysis tools
34
- # on the compiled template code.
34
+ # on the compiled template code. The directory containing the path must
35
+ # not be writable by untrusted users.
35
36
  attr_reader :compiled_path
36
37
 
37
38
  class << self
@@ -196,15 +197,26 @@ module Tilt
196
197
  key = scope_class
197
198
  end
198
199
  elsif @scope_class
199
- key = locals_keys.dup.freeze
200
+ key = locals_keys
200
201
  else
201
202
  key = [scope_class, locals_keys].freeze
202
203
  end
203
204
 
204
- LOCK.synchronize do
205
- if meth = @compiled_method[key]
206
- return meth
207
- end
205
+ if meth = LOCK.synchronize{@compiled_method[key]}
206
+ return meth
207
+ end
208
+ unless @fixed_locals
209
+ keys = locals_keys.map do |k|
210
+ case k
211
+ when String
212
+ "#{k}".freeze
213
+ when Symbol
214
+ k
215
+ else
216
+ raise TypeError, "invalid local key given: #{k.inspect}"
217
+ end
218
+ end.freeze
219
+ key = @scope_class ? keys : [scope_class, keys].freeze
208
220
  end
209
221
  meth = compile_template_method(locals_keys, scope_class)
210
222
  LOCK.synchronize do
@@ -257,9 +269,9 @@ module Tilt
257
269
  when Object
258
270
  Module === scope ? scope : scope.class
259
271
  else
260
- # :nocov:
272
+ # simplecov:disable
261
273
  USE_BIND_CALL ? CLASS_METHOD.bind_call(scope) : CLASS_METHOD.bind(scope).call
262
- # :nocov:
274
+ # simplecov:enable
263
275
  end
264
276
  end
265
277
 
@@ -325,13 +337,13 @@ module Tilt
325
337
  def _dup_string_if_frozen(string)
326
338
  +string
327
339
  end
328
- # :nocov:
340
+ # simplecov:disable
329
341
  else
330
342
  def _dup_string_if_frozen(string)
331
343
  string.frozen? ? string.dup : string
332
344
  end
333
345
  end
334
- # :nocov:
346
+ # simplecov:enable
335
347
 
336
348
  def process_arg(arg)
337
349
  if arg
@@ -350,9 +362,9 @@ module Tilt
350
362
  def read_template_file
351
363
  data = File.binread(file)
352
364
  # Set it to the default external (without verifying)
353
- # :nocov:
365
+ # simplecov:disable
354
366
  data.force_encoding(Encoding.default_external) if Encoding.default_external
355
- # :nocov:
367
+ # simplecov:enable
356
368
  data
357
369
  end
358
370
 
@@ -366,22 +378,21 @@ module Tilt
366
378
  end
367
379
 
368
380
  def local_extraction(local_keys)
369
- assignments = local_keys.map do |k|
381
+ assignments = []
382
+ locals_assignment = nil
383
+ local_keys.each do |k|
370
384
  if k.to_s =~ /\A[a-z_][a-zA-Z_0-9]*\z/
371
- "#{k} = locals[#{k.inspect}]"
385
+ assignment = "#{k} = locals[#{k.inspect}]"
386
+ if k.to_s == 'locals'
387
+ locals_assignment = assignment
388
+ else
389
+ assignments << assignment
390
+ end
372
391
  else
373
392
  raise "invalid locals key: #{k.inspect} (keys must be variable names)"
374
393
  end
375
394
  end
376
-
377
- s = "locals = locals[:locals]"
378
- if assignments.delete(s)
379
- # If there is a locals key itself named <tt>locals</tt>, delete it from the ordered keys so we can
380
- # assign it last. This is important because the assignment of all other locals depends on the
381
- # <tt>locals</tt> local variable still matching the <tt>locals</tt> method argument given to the method
382
- # created in <tt>#compile_template_method</tt>.
383
- assignments << s
384
- end
395
+ assignments << locals_assignment if locals_assignment
385
396
 
386
397
  assignments.join("\n")
387
398
  end
@@ -394,7 +405,7 @@ module Tilt
394
405
  method.bind_call(scope, locals, &block)
395
406
  end
396
407
  end
397
- # :nocov:
408
+ # simplecov:disable
398
409
  else
399
410
  def evaluate_method(method, scope, locals, &block)
400
411
  if @fixed_locals
@@ -409,7 +420,7 @@ module Tilt
409
420
  end
410
421
  end
411
422
  end
412
- # :nocov:
423
+ # simplecov:enable
413
424
 
414
425
  def compile_template_method(local_keys, scope_class=nil)
415
426
  source, offset = precompiled(local_keys)
@@ -456,7 +467,7 @@ module Tilt
456
467
  method_source_prefix = "# frozen-string-literal: true\n"
457
468
  method_source = method_source.sub(/\A# frozen-string-literal: true\n/, '')
458
469
  end
459
- method_source = "#{method_source_prefix}class #{scope_class.name}\n#{method_source}\nend"
470
+ method_source = "#{method_source_prefix}#{scope_class.is_a?(Class) ? 'class' : 'module'} #{scope_class.name}\n#{method_source}\nend"
460
471
 
461
472
  load_compiled_method(path, method_source)
462
473
  else
@@ -537,22 +548,12 @@ module Tilt
537
548
  yield script
538
549
  end
539
550
 
540
- binary(script) do
541
- script[/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/n, 1]
542
- end
551
+ script.b[/\A[ \t]*\#.*coding\s*[=:]\s*([[:alnum:]\-_]+).*$/n, 1]
543
552
  end
544
553
 
545
554
  def freeze_string_literals?
546
555
  false
547
556
  end
548
-
549
- def binary(string)
550
- original_encoding = string.encoding
551
- string.force_encoding(Encoding::BINARY)
552
- yield
553
- ensure
554
- string.force_encoding(original_encoding)
555
- end
556
557
  end
557
558
 
558
559
  # Static templates are templates that return the same output for every render
data/lib/tilt.rb CHANGED
@@ -5,7 +5,7 @@ require_relative 'tilt/template'
5
5
  # Namespace for Tilt. This module is not intended to be included anywhere.
6
6
  module Tilt
7
7
  # Current version.
8
- VERSION = '2.9.0'
8
+ VERSION = '2.10.0'
9
9
 
10
10
  EMPTY_ARRAY = [].freeze
11
11
  private_constant :EMPTY_ARRAY
@@ -138,10 +138,10 @@ module Tilt
138
138
  @cache = {}
139
139
  end
140
140
  end
141
- # :nocov:
141
+ # simplecov:disable
142
142
  # TILT3: Remove Tilt::Cache
143
143
  deprecate_constant :Cache if respond_to?(:deprecate_constant, true)
144
- # :nocov:
144
+ # simplecov:enable
145
145
 
146
146
  # Template Implementations ================================================
147
147
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tilt
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Tomayko
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 4.0.16
91
+ rubygems_version: 4.0.20
92
92
  specification_version: 4
93
93
  summary: Generic interface to multiple Ruby template engines
94
94
  test_files: []