tilt 2.0.11 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) 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 +6 -18
  16. data/lib/tilt/erb.rb +32 -16
  17. data/lib/tilt/erubi.rb +29 -6
  18. data/lib/tilt/erubis.rb +20 -11
  19. data/lib/tilt/etanni.rb +3 -2
  20. data/lib/tilt/haml.rb +73 -65
  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 +228 -109
  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 +19 -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 +7 -20
  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 +225 -78
  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 +61 -30
  46. metadata +21 -12
  47. data/lib/tilt/bluecloth.rb +0 -24
  48. data/lib/tilt/dummy.rb +0 -3
  49. data/lib/tilt/less.rb +0 -30
  50. data/lib/tilt/sigil.rb +0 -34
data/lib/tilt/csv.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'tilt/template'
2
-
3
- if RUBY_VERSION >= '1.9.0'
4
- require 'csv'
5
- else
6
- require 'fastercsv'
7
- end
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
3
+ require 'csv'
8
4
 
9
5
  module Tilt
10
6
 
@@ -36,22 +32,14 @@ module Tilt
36
32
  class CSVTemplate < Template
37
33
  self.default_mime_type = 'text/csv'
38
34
 
39
- def self.engine
40
- if RUBY_VERSION >= '1.9.0' && defined? ::CSV
41
- ::CSV
42
- elsif defined? ::FasterCSV
43
- ::FasterCSV
44
- end
45
- end
46
-
47
35
  def prepare
48
- @outvar = options.delete(:outvar) || '_csvout'
36
+ @outvar = @options.delete(:outvar) || '_csvout'
49
37
  end
50
38
 
51
39
  def precompiled_template(locals)
52
40
  <<-RUBY
53
- #{@outvar} = #{self.class.engine}.generate(**#{options}) do |csv|
54
- #{data}
41
+ #{@outvar} = CSV.generate(**#{@options}) do |csv|
42
+ #{@data}
55
43
  end
56
44
  RUBY
57
45
  end
data/lib/tilt/erb.rb CHANGED
@@ -1,30 +1,44 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'erb'
3
4
 
4
5
  module Tilt
5
6
  # ERB template implementation. See:
6
7
  # http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
7
8
  class ERBTemplate < Template
8
- @@default_output_variable = '_erbout'
9
-
10
9
  SUPPORTS_KVARGS = ::ERB.instance_method(:initialize).parameters.assoc(:key) rescue false
11
10
 
11
+ # Remove in Tilt 2.3
12
+ @default_output_variable = nil
13
+ def self._default_output_variable
14
+ @default_output_variable
15
+ end
12
16
  def self.default_output_variable
13
- @@default_output_variable
17
+ warn "#{self}.default_output_variable is deprecated and will be removed in Tilt 2.3.", uplevel: 1
18
+ @default_output_variable
14
19
  end
15
-
16
20
  def self.default_output_variable=(name)
17
- warn "#{self}.default_output_variable= has been replaced with the :outvar-option"
18
- @@default_output_variable = name
21
+ warn "#{self}.default_output_variable= is deprecated and will be removed in Tilt 2.3. Switch to using the :outvar option.", uplevel: 1
22
+ @default_output_variable = name
19
23
  end
20
24
 
21
25
  def prepare
22
- @outvar = options[:outvar] || self.class.default_output_variable
23
- options[:trim] = '<>' if !(options[:trim] == false) && (options[:trim].nil? || options[:trim] == true)
26
+ @freeze_string_literals = !!@options[:freeze]
27
+ @outvar = @options[:outvar] || self.class._default_output_variable || '_erbout'
28
+ trim = case @options[:trim]
29
+ when false
30
+ nil
31
+ when nil, true
32
+ '<>'
33
+ else
34
+ @options[:trim]
35
+ end
24
36
  @engine = if SUPPORTS_KVARGS
25
- ::ERB.new(data, trim_mode: options[:trim], eoutvar: @outvar)
37
+ ::ERB.new(@data, trim_mode: trim, eoutvar: @outvar)
38
+ # :nocov:
26
39
  else
27
- ::ERB.new(data, options[:safe], options[:trim], @outvar)
40
+ ::ERB.new(@data, options[:safe], trim, @outvar)
41
+ # :nocov:
28
42
  end
29
43
  end
30
44
 
@@ -52,11 +66,13 @@ module Tilt
52
66
 
53
67
  # ERB generates a line to specify the character coding of the generated
54
68
  # source in 1.9. Account for this in the line offset.
55
- if RUBY_VERSION >= '1.9.0'
56
- def precompiled(locals)
57
- source, offset = super
58
- [source, offset + 1]
59
- end
69
+ def precompiled(locals)
70
+ source, offset = super
71
+ [source, offset + 1]
72
+ end
73
+
74
+ def freeze_string_literals?
75
+ @freeze_string_literals
60
76
  end
61
77
  end
62
78
  end
data/lib/tilt/erubi.rb CHANGED
@@ -1,4 +1,5 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'erubi'
3
4
 
4
5
  module Tilt
@@ -12,15 +13,33 @@ module Tilt
12
13
  # instead of the default (which is ::Erubi::Engine).
13
14
  class ErubiTemplate < Template
14
15
  def prepare
15
- @options.merge!(:preamble => false, :postamble => false, :ensure=>true)
16
+ @options[:preamble] = false
17
+ @options[:postamble] = false
18
+ @options[:ensure] = true
16
19
 
17
20
  engine_class = @options[:engine_class] || Erubi::Engine
18
21
 
19
- @engine = engine_class.new(data, @options)
20
- @outvar = @engine.bufvar
22
+ # If :freeze option is given, the intent is to setup frozen string
23
+ # literals in the template. So enable frozen string literals in the
24
+ # code Tilt generates if the :freeze option is given.
25
+ if @freeze_string_literals = !!@options[:freeze]
26
+ # Passing the :freeze option to Erubi sets the
27
+ # frozen-string-literal magic comment, which doesn't have an effect
28
+ # with Tilt as Tilt wraps the resulting code. Worse, the magic
29
+ # comment appearing not at the top of the file can cause a warning.
30
+ # So remove the :freeze option before passing to Erubi.
31
+ @options.delete(:freeze)
32
+
33
+ # Erubi by default appends .freeze to template literals on Ruby 2.1+,
34
+ # but that is not necessary and slows down code when Tilt is using
35
+ # frozen string literals, so pass the :freeze_template_literals
36
+ # option to not append .freeze.
37
+ @options[:freeze_template_literals] = false
38
+ end
21
39
 
22
- # Remove dup after tilt supports frozen source.
23
- @src = @engine.src.dup
40
+ @engine = engine_class.new(@data, @options)
41
+ @outvar = @engine.bufvar
42
+ @src = @engine.src
24
43
 
25
44
  @engine
26
45
  end
@@ -28,5 +47,9 @@ module Tilt
28
47
  def precompiled_template(locals)
29
48
  @src
30
49
  end
50
+
51
+ def freeze_string_literals?
52
+ @freeze_string_literals
53
+ end
31
54
  end
32
55
  end
data/lib/tilt/erubis.rb CHANGED
@@ -1,4 +1,5 @@
1
- require 'tilt/erb'
1
+ # frozen_string_literal: true
2
+ require_relative 'erb'
2
3
  require 'erubis'
3
4
 
4
5
  module Tilt
@@ -15,12 +16,18 @@ module Tilt
15
16
  # the engine class instead of the default. All content
16
17
  # within <%= %> blocks will be automatically html escaped.
17
18
  class ErubisTemplate < ERBTemplate
19
+ # Remove in Tilt 2.3
20
+ @default_output_variable = nil
21
+
18
22
  def prepare
19
- @outvar = options.delete(:outvar) || self.class.default_output_variable
20
- @options.merge!(:preamble => false, :postamble => false, :bufvar => @outvar)
21
- engine_class = options.delete(:engine_class)
22
- engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
23
- @engine = (engine_class || ::Erubis::Eruby).new(data, options)
23
+ @freeze_string_literals = !!@options.delete(:freeze)
24
+ @outvar = @options.delete(:outvar) || self.class._default_output_variable || '_erbout'
25
+ @options[:preamble] = false
26
+ @options[:postamble] = false
27
+ @options[:bufvar] = @outvar
28
+ engine_class = @options.delete(:engine_class)
29
+ engine_class = ::Erubis::EscapedEruby if @options.delete(:escape_html)
30
+ @engine = (engine_class || ::Erubis::Eruby).new(@data, @options)
24
31
  end
25
32
 
26
33
  def precompiled_preamble(locals)
@@ -33,11 +40,13 @@ module Tilt
33
40
 
34
41
  # Erubis doesn't have ERB's line-off-by-one under 1.9 problem.
35
42
  # Override and adjust back.
36
- if RUBY_VERSION >= '1.9.0'
37
- def precompiled(locals)
38
- source, offset = super
39
- [source, offset - 1]
40
- end
43
+ def precompiled(locals)
44
+ source, offset = super
45
+ [source, offset - 1]
46
+ end
47
+
48
+ def freeze_string_literals?
49
+ @freeze_string_literals
41
50
  end
42
51
  end
43
52
  end
data/lib/tilt/etanni.rb CHANGED
@@ -1,4 +1,5 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
 
3
4
  module Tilt
4
5
  class EtanniTemplate < Template
@@ -9,7 +10,7 @@ module Tilt
9
10
  stop = "\n#{separator}\n"
10
11
  replacement = "#{stop}\\1#{start}"
11
12
 
12
- temp = data.strip
13
+ temp = @data.strip
13
14
  temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, replacement)
14
15
 
15
16
  @code = "_out_ = [<<#{separator}.chomp!]\n#{temp}#{stop}_out_.join"
data/lib/tilt/haml.rb CHANGED
@@ -1,84 +1,92 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'haml'
3
4
 
4
5
  module Tilt
5
6
  # Haml template implementation. See:
6
7
  # http://haml.hamptoncatlin.com/
7
- class HamlTemplate < Template
8
- self.default_mime_type = 'text/html'
8
+ if defined?(Haml::Template) && Haml::Template < Tilt::Template
9
+ # Haml >= 6 ships its own template, prefer it when available.
10
+ HamlTemplate = Haml::Template
11
+ else
12
+ class HamlTemplate < Template
13
+ self.default_mime_type = 'text/html'
9
14
 
10
- # `Gem::Version.correct?` may return false because of Haml::VERSION #=> "3.1.8 (Separated Sally)". After Haml 4, it's always correct.
11
- if Gem::Version.correct?(Haml::VERSION) && Gem::Version.new(Haml::VERSION) >= Gem::Version.new('5.0.0.beta.2')
12
- def prepare
13
- options = {}.update(@options).update(filename: eval_file, line: line)
14
- if options.include?(:outvar)
15
- options[:buffer] = options.delete(:outvar)
16
- options[:save_buffer] = true
15
+ # `Gem::Version.correct?` may return false because of Haml::VERSION #=> "3.1.8 (Separated Sally)". After Haml 4, it's always correct.
16
+ if Gem::Version.correct?(Haml::VERSION) && Gem::Version.new(Haml::VERSION) >= Gem::Version.new('5.0.0.beta.2')
17
+ def prepare
18
+ @options[:filename] = eval_file
19
+ @options[:line] = @line
20
+ if @options.include?(:outvar)
21
+ @options[:buffer] = @options.delete(:outvar)
22
+ @options[:save_buffer] = true
23
+ end
24
+ @engine = ::Haml::TempleEngine.new(@options)
25
+ @engine.compile(@data)
17
26
  end
18
- @engine = ::Haml::TempleEngine.new(options)
19
- @engine.compile(data)
20
- end
21
27
 
22
- def evaluate(scope, locals, &block)
23
- raise ArgumentError, 'invalid scope: must not be frozen' if scope.frozen?
24
- super
25
- end
28
+ def evaluate(scope, locals, &block)
29
+ raise ArgumentError, 'invalid scope: must not be frozen' if scope.frozen?
30
+ super
31
+ end
26
32
 
27
- def precompiled_template(locals)
28
- @engine.precompiled_with_ambles(
29
- [],
30
- after_preamble: <<-RUBY
31
- __in_erb_template = true
32
- _haml_locals = locals
33
- RUBY
34
- )
35
- end
36
- else # Following definitions are for Haml <= 4 and deprecated.
37
- def prepare
38
- options = @options.merge(:filename => eval_file, :line => line)
39
- @engine = ::Haml::Engine.new(data, options)
40
- end
33
+ def precompiled_template(locals)
34
+ @engine.precompiled_with_ambles(
35
+ [],
36
+ after_preamble: <<-RUBY
37
+ __in_erb_template = true
38
+ _haml_locals = locals
39
+ RUBY
40
+ )
41
+ end
42
+ else # Following definitions are for Haml <= 4 and deprecated.
43
+ def prepare
44
+ @options[:filename] = eval_file
45
+ @options[:line] = @line
46
+ @engine = ::Haml::Engine.new(@data, @options)
47
+ end
41
48
 
42
- def evaluate(scope, locals, &block)
43
- raise ArgumentError, 'invalid scope: must not be frozen' if scope.frozen?
49
+ def evaluate(scope, locals, &block)
50
+ raise ArgumentError, 'invalid scope: must not be frozen' if scope.frozen?
44
51
 
45
- if @engine.respond_to?(:precompiled_method_return_value, true)
46
- super
47
- else
48
- @engine.render(scope, locals, &block)
52
+ if @engine.respond_to?(:precompiled_method_return_value, true)
53
+ super
54
+ else
55
+ @engine.render(scope, locals, &block)
56
+ end
49
57
  end
50
- end
51
58
 
52
- # Precompiled Haml source. Taken from the precompiled_with_ambles
53
- # method in Haml::Precompiler:
54
- # http://github.com/nex3/haml/blob/master/lib/haml/precompiler.rb#L111-126
55
- def precompiled_template(locals)
56
- @engine.precompiled
57
- end
59
+ # Precompiled Haml source. Taken from the precompiled_with_ambles
60
+ # method in Haml::Precompiler:
61
+ # http://github.com/nex3/haml/blob/master/lib/haml/precompiler.rb#L111-126
62
+ def precompiled_template(locals)
63
+ @engine.precompiled
64
+ end
58
65
 
59
- def precompiled_preamble(locals)
60
- local_assigns = super
61
- @engine.instance_eval do
62
- <<-RUBY
63
- begin
64
- extend Haml::Helpers
65
- _hamlout = @haml_buffer = Haml::Buffer.new(haml_buffer, #{options_for_buffer.inspect})
66
- _erbout = _hamlout.buffer
67
- __in_erb_template = true
68
- _haml_locals = locals
69
- #{local_assigns}
70
- RUBY
66
+ def precompiled_preamble(locals)
67
+ local_assigns = super
68
+ @engine.instance_eval do
69
+ <<-RUBY
70
+ begin
71
+ extend Haml::Helpers
72
+ _hamlout = @haml_buffer = Haml::Buffer.new(haml_buffer, #{options_for_buffer.inspect})
73
+ _erbout = _hamlout.buffer
74
+ __in_erb_template = true
75
+ _haml_locals = locals
76
+ #{local_assigns}
77
+ RUBY
78
+ end
71
79
  end
72
- end
73
80
 
74
- def precompiled_postamble(locals)
75
- @engine.instance_eval do
76
- <<-RUBY
77
- #{precompiled_method_return_value}
78
- ensure
79
- @haml_buffer = @haml_buffer.upper if haml_buffer
80
- end
81
- RUBY
81
+ def precompiled_postamble(locals)
82
+ @engine.instance_eval do
83
+ <<-RUBY
84
+ #{precompiled_method_return_value}
85
+ ensure
86
+ @haml_buffer = @haml_buffer.upper if haml_buffer
87
+ end
88
+ RUBY
89
+ end
82
90
  end
83
91
  end
84
92
  end
data/lib/tilt/kramdown.rb CHANGED
@@ -1,25 +1,13 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'kramdown'
3
4
 
4
- module Tilt
5
- # Kramdown Markdown implementation. See:
6
- # http://kramdown.rubyforge.org/
7
- class KramdownTemplate < Template
8
- DUMB_QUOTES = [39, 39, 34, 34]
5
+ dumb_quotes = [39, 39, 34, 34].freeze
9
6
 
10
- def prepare
11
- options[:smart_quotes] = DUMB_QUOTES unless options[:smartypants]
12
- @engine = Kramdown::Document.new(data, options)
13
- @output = nil
14
- end
7
+ # Kramdown Markdown implementation. See: https://kramdown.gettalong.org/
8
+ Tilt::KramdownTemplate = Tilt::StaticTemplate.subclass do
9
+ # dup as Krawmdown modifies the passed option with map!
10
+ @options[:smart_quotes] = dumb_quotes.dup unless @options[:smartypants]
15
11
 
16
- def evaluate(scope, locals, &block)
17
- @output ||= @engine.to_html
18
- end
19
-
20
- def allows_script?
21
- false
22
- end
23
- end
12
+ Kramdown::Document.new(@data, @options).to_html
24
13
  end
25
-
data/lib/tilt/liquid.rb CHANGED
@@ -1,11 +1,12 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'liquid'
3
4
 
4
5
  module Tilt
5
6
  # Liquid template implementation. See:
6
7
  # http://liquidmarkup.org/
7
8
  #
8
- # Liquid is designed to be a *safe* template system and threfore
9
+ # Liquid is designed to be a *safe* template system and therefore
9
10
  # does not provide direct access to execuatable scopes. In order to
10
11
  # support a +scope+, the +scope+ must be able to represent itself
11
12
  # as a hash by responding to #to_h. If the +scope+ does not respond
@@ -17,16 +18,17 @@ module Tilt
17
18
  # time when using this template engine.
18
19
  class LiquidTemplate < Template
19
20
  def prepare
20
- @engine = ::Liquid::Template.parse(data, liquid_options)
21
+ @options[:line_numbers] = true unless @options.has_key?(:line_numbers)
22
+ @engine = ::Liquid::Template.parse(@data, @options)
21
23
  end
22
24
 
23
- def evaluate(scope, locals, &block)
24
- locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
25
+ def evaluate(scope, locs)
26
+ locals = {}
25
27
  if scope.respond_to?(:to_h)
26
- scope = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
27
- locals = scope.merge(locals)
28
+ scope.to_h.each{|k, v| locals[k.to_s] = v}
28
29
  end
29
- locals['yield'] = block.nil? ? '' : yield
30
+ locs.each{|k, v| locals[k.to_s] = v}
31
+ locals['yield'] = block_given? ? yield : ''
30
32
  locals['content'] = locals['yield']
31
33
  @engine.render(locals)
32
34
  end
@@ -34,11 +36,5 @@ module Tilt
34
36
  def allows_script?
35
37
  false
36
38
  end
37
-
38
- private
39
-
40
- def liquid_options
41
- { line_numbers: true }.merge options
42
- end
43
39
  end
44
40
  end
@@ -1,23 +1,11 @@
1
- require 'tilt/template'
1
+ # frozen_string_literal: true
2
+ require_relative 'template'
2
3
  require 'livescript'
3
4
 
4
- module Tilt
5
- # LiveScript template implementation. See:
6
- # http://livescript.net/
7
- #
8
- # LiveScript templates do not support object scopes, locals, or yield.
9
- class LiveScriptTemplate < Template
10
- self.default_mime_type = 'application/javascript'
11
-
12
- def prepare
13
- end
14
-
15
- def evaluate(scope, locals, &block)
16
- @output ||= LiveScript.compile(data, options)
17
- end
18
-
19
- def allows_script?
20
- false
21
- end
22
- end
5
+ # LiveScript template implementation. See:
6
+ # http://livescript.net/
7
+ #
8
+ # LiveScript templates do not support object scopes, locals, or yield.
9
+ Tilt::LiveScriptTemplate = Tilt::StaticTemplate.subclass(mime_type: 'application/javascript') do
10
+ LiveScript.compile(@data, @options)
23
11
  end