tilt 1.2.2 → 1.3.2

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.
@@ -0,0 +1,28 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Creole implementation. See:
5
+ # http://www.wikicreole.org/
6
+ class CreoleTemplate < Template
7
+ def self.engine_initialized?
8
+ defined? ::Creole
9
+ end
10
+
11
+ def initialize_engine
12
+ require_template_library 'creole'
13
+ end
14
+
15
+ def prepare
16
+ opts = {}
17
+ [:allowed_schemes, :extensions, :no_escape].each do |k|
18
+ opts[k] = options[k] if options[k]
19
+ end
20
+ @engine = Creole::Parser.new(data, opts)
21
+ @output = nil
22
+ end
23
+
24
+ def evaluate(scope, locals, &block)
25
+ @output ||= @engine.to_html
26
+ end
27
+ end
28
+ end
data/lib/tilt/css.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Sass template implementation. See:
5
+ # http://haml.hamptoncatlin.com/
6
+ #
7
+ # Sass templates do not support object scopes, locals, or yield.
8
+ class SassTemplate < Template
9
+ self.default_mime_type = 'text/css'
10
+
11
+ def self.engine_initialized?
12
+ defined? ::Sass::Engine
13
+ end
14
+
15
+ def initialize_engine
16
+ require_template_library 'sass'
17
+ end
18
+
19
+ def prepare
20
+ @engine = ::Sass::Engine.new(data, sass_options)
21
+ end
22
+
23
+ def evaluate(scope, locals, &block)
24
+ @output ||= @engine.render
25
+ end
26
+
27
+ private
28
+ def sass_options
29
+ options.merge(:filename => eval_file, :line => line, :syntax => :sass)
30
+ end
31
+ end
32
+
33
+ # Sass's new .scss type template implementation.
34
+ class ScssTemplate < SassTemplate
35
+ self.default_mime_type = 'text/css'
36
+
37
+ private
38
+ def sass_options
39
+ options.merge(:filename => eval_file, :line => line, :syntax => :scss)
40
+ end
41
+ end
42
+
43
+ # Lessscss template implementation. See:
44
+ # http://lesscss.org/
45
+ #
46
+ # Less templates do not support object scopes, locals, or yield.
47
+ class LessTemplate < Template
48
+ self.default_mime_type = 'text/css'
49
+
50
+ def self.engine_initialized?
51
+ defined? ::Less::Engine
52
+ end
53
+
54
+ def initialize_engine
55
+ require_template_library 'less'
56
+ end
57
+
58
+ def prepare
59
+ @engine = ::Less::Engine.new(data)
60
+ end
61
+
62
+ def evaluate(scope, locals, &block)
63
+ @engine.to_css
64
+ end
65
+ end
66
+ end
67
+
data/lib/tilt/erb.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # ERB template implementation. See:
5
+ # http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
6
+ class ERBTemplate < Template
7
+ @@default_output_variable = '_erbout'
8
+
9
+ def self.default_output_variable
10
+ @@default_output_variable
11
+ end
12
+
13
+ def self.default_output_variable=(name)
14
+ @@default_output_variable = name
15
+ end
16
+
17
+ def self.engine_initialized?
18
+ defined? ::ERB
19
+ end
20
+
21
+ def initialize_engine
22
+ require_template_library 'erb'
23
+ end
24
+
25
+ def prepare
26
+ @outvar = options[:outvar] || self.class.default_output_variable
27
+ options[:trim] = '<>' if options[:trim].nil? || options[:trim] == true
28
+ @engine = ::ERB.new(data, options[:safe], options[:trim], @outvar)
29
+ end
30
+
31
+ def precompiled_template(locals)
32
+ source = @engine.src
33
+ source
34
+ end
35
+
36
+ def precompiled_preamble(locals)
37
+ <<-RUBY
38
+ begin
39
+ __original_outvar = #{@outvar} if defined?(#{@outvar})
40
+ #{super}
41
+ RUBY
42
+ end
43
+
44
+ def precompiled_postamble(locals)
45
+ <<-RUBY
46
+ #{super}
47
+ ensure
48
+ #{@outvar} = __original_outvar
49
+ end
50
+ RUBY
51
+ end
52
+
53
+ # ERB generates a line to specify the character coding of the generated
54
+ # 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
60
+ end
61
+ end
62
+
63
+ # Erubis template implementation. See:
64
+ # http://www.kuwata-lab.com/erubis/
65
+ #
66
+ # ErubisTemplate supports the following additional options, which are not
67
+ # passed down to the Erubis engine:
68
+ #
69
+ # :engine_class allows you to specify a custom engine class to use
70
+ # instead of the default (which is ::Erubis::Eruby).
71
+ #
72
+ # :escape_html when true, ::Erubis::EscapedEruby will be used as
73
+ # the engine class instead of the default. All content
74
+ # within <%= %> blocks will be automatically html escaped.
75
+ class ErubisTemplate < ERBTemplate
76
+ def self.engine_initialized?
77
+ defined? ::Erubis
78
+ end
79
+
80
+ def initialize_engine
81
+ require_template_library 'erubis'
82
+ end
83
+
84
+ def prepare
85
+ @outvar = options.delete(:outvar) || self.class.default_output_variable
86
+ @options.merge!(:preamble => false, :postamble => false, :bufvar => @outvar)
87
+ engine_class = options.delete(:engine_class)
88
+ engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
89
+ @engine = (engine_class || ::Erubis::Eruby).new(data, options)
90
+ end
91
+
92
+ def precompiled_preamble(locals)
93
+ [super, "#{@outvar} = _buf = ''"].join("\n")
94
+ end
95
+
96
+ def precompiled_postamble(locals)
97
+ [@outvar, super].join("\n")
98
+ end
99
+
100
+ # Erubis doesn't have ERB's line-off-by-one under 1.9 problem.
101
+ # Override and adjust back.
102
+ if RUBY_VERSION >= '1.9.0'
103
+ def precompiled(locals)
104
+ source, offset = super
105
+ [source, offset - 1]
106
+ end
107
+ end
108
+ end
109
+ end
110
+
data/lib/tilt/haml.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Haml template implementation. See:
5
+ # http://haml.hamptoncatlin.com/
6
+ class HamlTemplate < Template
7
+ self.default_mime_type = 'text/html'
8
+
9
+ def self.engine_initialized?
10
+ defined? ::Haml::Engine
11
+ end
12
+
13
+ def initialize_engine
14
+ require_template_library 'haml'
15
+ end
16
+
17
+ def prepare
18
+ options = @options.merge(:filename => eval_file, :line => line)
19
+ @engine = ::Haml::Engine.new(data, options)
20
+ end
21
+
22
+ def evaluate(scope, locals, &block)
23
+ if @engine.respond_to?(:precompiled_method_return_value, true)
24
+ super
25
+ else
26
+ @engine.render(scope, locals, &block)
27
+ end
28
+ end
29
+
30
+ # Precompiled Haml source. Taken from the precompiled_with_ambles
31
+ # method in Haml::Precompiler:
32
+ # http://github.com/nex3/haml/blob/master/lib/haml/precompiler.rb#L111-126
33
+ def precompiled_template(locals)
34
+ @engine.precompiled
35
+ end
36
+
37
+ def precompiled_preamble(locals)
38
+ local_assigns = super
39
+ @engine.instance_eval do
40
+ <<-RUBY
41
+ begin
42
+ extend Haml::Helpers
43
+ _hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
44
+ _erbout = _hamlout.buffer
45
+ __in_erb_template = true
46
+ _haml_locals = locals
47
+ #{local_assigns}
48
+ RUBY
49
+ end
50
+ end
51
+
52
+ def precompiled_postamble(locals)
53
+ @engine.instance_eval do
54
+ <<-RUBY
55
+ #{precompiled_method_return_value}
56
+ ensure
57
+ @haml_buffer = @haml_buffer.upper
58
+ end
59
+ RUBY
60
+ end
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,41 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Liquid template implementation. See:
5
+ # http://liquid.rubyforge.org/
6
+ #
7
+ # Liquid is designed to be a *safe* template system and threfore
8
+ # does not provide direct access to execuatable scopes. In order to
9
+ # support a +scope+, the +scope+ must be able to represent itself
10
+ # as a hash by responding to #to_h. If the +scope+ does not respond
11
+ # to #to_h it will be ignored.
12
+ #
13
+ # LiquidTemplate does not support yield blocks.
14
+ #
15
+ # It's suggested that your program require 'liquid' at load
16
+ # time when using this template engine.
17
+ class LiquidTemplate < Template
18
+ def self.engine_initialized?
19
+ defined? ::Liquid::Template
20
+ end
21
+
22
+ def initialize_engine
23
+ require_template_library 'liquid'
24
+ end
25
+
26
+ def prepare
27
+ @engine = ::Liquid::Template.parse(data)
28
+ end
29
+
30
+ def evaluate(scope, locals, &block)
31
+ locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
32
+ if scope.respond_to?(:to_h)
33
+ scope = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
34
+ locals = scope.merge(locals)
35
+ end
36
+ locals['yield'] = block.nil? ? '' : yield
37
+ locals['content'] = locals['yield']
38
+ @engine.render(locals)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Markaby
5
+ # http://github.com/markaby/markaby
6
+ class MarkabyTemplate < Template
7
+ def self.builder_class
8
+ @builder_class ||= Class.new(Markaby::Builder) do
9
+ def __capture_markaby_tilt__(&block)
10
+ __run_markaby_tilt__ do
11
+ text capture(&block)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.engine_initialized?
18
+ defined? ::Markaby
19
+ end
20
+
21
+ def initialize_engine
22
+ require_template_library 'markaby'
23
+ end
24
+
25
+ def prepare
26
+ end
27
+
28
+ def evaluate(scope, locals, &block)
29
+ builder = self.class.builder_class.new({}, scope)
30
+ builder.locals = locals
31
+
32
+ if data.kind_of? Proc
33
+ (class << builder; self end).send(:define_method, :__run_markaby_tilt__, &data)
34
+ else
35
+ builder.instance_eval <<-CODE, __FILE__, __LINE__
36
+ def __run_markaby_tilt__
37
+ #{data}
38
+ end
39
+ CODE
40
+ end
41
+
42
+ if block
43
+ builder.__capture_markaby_tilt__(&block)
44
+ else
45
+ builder.__run_markaby_tilt__
46
+ end
47
+
48
+ builder.to_s
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,131 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Discount Markdown implementation. See:
5
+ # http://github.com/rtomayko/rdiscount
6
+ #
7
+ # RDiscount is a simple text filter. It does not support +scope+ or
8
+ # +locals+. The +:smart+ and +:filter_html+ options may be set true
9
+ # to enable those flags on the underlying RDiscount object.
10
+ class RDiscountTemplate < Template
11
+ self.default_mime_type = 'text/html'
12
+
13
+ ALIAS = {
14
+ :escape_html => :filter_html,
15
+ :smartypants => :smart
16
+ }
17
+
18
+ FLAGS = [:smart, :filter_html, :smartypants, :escape_html]
19
+
20
+ def flags
21
+ FLAGS.select { |flag| options[flag] }.map { |flag| ALIAS[flag] || flag }
22
+ end
23
+
24
+ def self.engine_initialized?
25
+ defined? ::RDiscount
26
+ end
27
+
28
+ def initialize_engine
29
+ require_template_library 'rdiscount'
30
+ end
31
+
32
+ def prepare
33
+ @engine = RDiscount.new(data, *flags)
34
+ @output = nil
35
+ end
36
+
37
+ def evaluate(scope, locals, &block)
38
+ @output ||= @engine.to_html
39
+ end
40
+ end
41
+
42
+ # Upskirt Markdown implementation. See:
43
+ # https://github.com/tanoku/redcarpet
44
+ #
45
+ # Compatible to RDiscount
46
+ class RedcarpetTemplate < RDiscountTemplate
47
+ self.default_mime_type = 'text/html'
48
+
49
+ def self.engine_initialized?
50
+ defined? ::RedcarpetCompat
51
+ end
52
+
53
+ def initialize_engine
54
+ require_template_library 'redcarpet'
55
+ end
56
+
57
+ def prepare
58
+ @engine = RedcarpetCompat.new(data, *flags)
59
+ @output = nil
60
+ end
61
+ end
62
+
63
+ # BlueCloth Markdown implementation. See:
64
+ # http://deveiate.org/projects/BlueCloth/
65
+ class BlueClothTemplate < Template
66
+ self.default_mime_type = 'text/html'
67
+
68
+ def self.engine_initialized?
69
+ defined? ::BlueCloth
70
+ end
71
+
72
+ def initialize_engine
73
+ require_template_library 'bluecloth'
74
+ end
75
+
76
+ def prepare
77
+ @engine = BlueCloth.new(data, options)
78
+ @output = nil
79
+ end
80
+
81
+ def evaluate(scope, locals, &block)
82
+ @output ||= @engine.to_html
83
+ end
84
+ end
85
+
86
+ # Maruku markdown implementation. See:
87
+ # http://maruku.rubyforge.org/
88
+ class MarukuTemplate < Template
89
+ def self.engine_initialized?
90
+ defined? ::Maruku
91
+ end
92
+
93
+ def initialize_engine
94
+ require_template_library 'maruku'
95
+ end
96
+
97
+ def prepare
98
+ @engine = Maruku.new(data, options)
99
+ @output = nil
100
+ end
101
+
102
+ def evaluate(scope, locals, &block)
103
+ @output ||= @engine.to_html
104
+ end
105
+ end
106
+
107
+ # Kramdown Markdown implementation. See:
108
+ # http://kramdown.rubyforge.org/
109
+ class KramdownTemplate < Template
110
+ DUMB_QUOTES = [39, 39, 34, 34]
111
+
112
+ def self.engine_initialized?
113
+ defined? ::Kramdown
114
+ end
115
+
116
+ def initialize_engine
117
+ require_template_library 'kramdown'
118
+ end
119
+
120
+ def prepare
121
+ options[:smart_quotes] = DUMB_QUOTES unless options[:smartypants]
122
+ @engine = Kramdown::Document.new(data, options)
123
+ @output = nil
124
+ end
125
+
126
+ def evaluate(scope, locals, &block)
127
+ @output ||= @engine.to_html
128
+ end
129
+ end
130
+ end
131
+
@@ -0,0 +1,43 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Nokogiri template implementation. See:
5
+ # http://nokogiri.org/
6
+ class NokogiriTemplate < Template
7
+ self.default_mime_type = 'text/xml'
8
+
9
+ def self.engine_initialized?
10
+ defined? ::Nokogiri
11
+ end
12
+
13
+ def initialize_engine
14
+ require_template_library 'nokogiri'
15
+ end
16
+
17
+ def prepare; end
18
+
19
+ def evaluate(scope, locals, &block)
20
+ block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
21
+
22
+ if data.respond_to?(:to_str)
23
+ super(scope, locals, &block)
24
+ else
25
+ ::Nokogiri::XML::Builder.new.tap(&data).to_xml
26
+ end
27
+ end
28
+
29
+ def precompiled_preamble(locals)
30
+ return super if locals.include? :xml
31
+ "xml = ::Nokogiri::XML::Builder.new { |xml| }\n#{super}"
32
+ end
33
+
34
+ def precompiled_postamble(locals)
35
+ "xml.to_xml"
36
+ end
37
+
38
+ def precompiled_template(locals)
39
+ data.to_str
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,51 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # Radius Template
5
+ # http://github.com/jlong/radius/
6
+ class RadiusTemplate < Template
7
+ def self.engine_initialized?
8
+ defined? ::Radius
9
+ end
10
+
11
+ def self.context_class
12
+ @context_class ||= Class.new(Radius::Context) do
13
+ attr_accessor :tilt_scope
14
+
15
+ def tag_missing(name, attributes)
16
+ tilt_scope.__send__(name)
17
+ end
18
+
19
+ def dup
20
+ i = super
21
+ i.tilt_scope = tilt_scope
22
+ i
23
+ end
24
+ end
25
+ end
26
+
27
+ def initialize_engine
28
+ require_template_library 'radius'
29
+ end
30
+
31
+ def prepare
32
+ end
33
+
34
+ def evaluate(scope, locals, &block)
35
+ context = self.class.context_class.new
36
+ context.tilt_scope = scope
37
+ context.define_tag("yield") do
38
+ block.call
39
+ end
40
+ locals.each do |tag, value|
41
+ context.define_tag(tag) do
42
+ value
43
+ end
44
+ end
45
+
46
+ options = {:tag_prefix => 'r'}.merge(@options)
47
+ parser = Radius::Parser.new(context, options)
48
+ parser.parse(data)
49
+ end
50
+ end
51
+ end
data/lib/tilt/rdoc.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # RDoc template. See:
5
+ # http://rdoc.rubyforge.org/
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.
10
+ class RDocTemplate < Template
11
+ self.default_mime_type = 'text/html'
12
+
13
+ def self.engine_initialized?
14
+ defined? ::RDoc::Markup
15
+ end
16
+
17
+ def initialize_engine
18
+ require_template_library 'rdoc/markup'
19
+ require_template_library 'rdoc/markup/to_html'
20
+ end
21
+
22
+ def prepare
23
+ markup = RDoc::Markup::ToHtml.new
24
+ @engine = markup.convert(data)
25
+ @output = nil
26
+ end
27
+
28
+ def evaluate(scope, locals, &block)
29
+ @output ||= @engine.to_s
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # The template source is evaluated as a Ruby string. The #{} interpolation
5
+ # syntax can be used to generated dynamic output.
6
+ class StringTemplate < Template
7
+ def prepare
8
+ hash = "TILT#{data.hash.abs}"
9
+ @code = "<<#{hash}.chomp\n#{data}\n#{hash}"
10
+ end
11
+
12
+ def precompiled_template(locals)
13
+ @code
14
+ end
15
+
16
+ def precompiled(locals)
17
+ source, offset = super
18
+ [source, offset + 1]
19
+ end
20
+ end
21
+ end