tilt 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -16,6 +16,11 @@ end
16
16
 
17
17
  # PACKAGING =================================================================
18
18
 
19
+ begin
20
+ require 'rubygems'
21
+ rescue LoadError
22
+ end
23
+
19
24
  if defined?(Gem)
20
25
  SPEC = eval(File.read('tilt.gemspec'))
21
26
 
@@ -72,26 +77,3 @@ file 'tilt.gemspec' => FileList['{lib,test}/**','Rakefile'] do |f|
72
77
  File.open(f.name, 'w') { |io| io.write(spec) }
73
78
  puts "updated #{f.name}"
74
79
  end
75
-
76
- # DOC =======================================================================
77
-
78
- # requires the hanna gem:
79
- # gem install mislav-hanna --source=http://gems.github.com
80
- desc 'Build API documentation (doc/api)'
81
- task 'rdoc' => 'rdoc/index.html'
82
- file 'rdoc/index.html' => FileList['lib/**/*.rb'] do |f|
83
- rm_rf 'rdoc'
84
- sh((<<-SH).gsub(/[\s\n]+/, ' ').strip)
85
- hanna
86
- --op doc/api
87
- --promiscuous
88
- --charset utf8
89
- --fmt html
90
- --inline-source
91
- --line-numbers
92
- --accessor option_accessor=RW
93
- --main Tilt
94
- --title 'Tilt API Documentation'
95
- #{f.prerequisites.join(' ')}
96
- SH
97
- end
@@ -94,15 +94,16 @@ the extensions as follows:
94
94
 
95
95
  ### Options
96
96
 
97
- #### `:trim => true`
97
+ #### `:engine_class => Erubis::Eruby`
98
98
 
99
- Delete spaces around '<% %>'. (But, spaces around '<%= %>' are preserved.)
99
+ Allows you to specify a custom engine class to use instead of the
100
+ default which is `Erubis::Eruby`.
100
101
 
101
- #### `:pattern => '<% %>'`
102
-
103
- Set pattern for embedded Ruby code.
102
+ #### `:escape_html => false`
104
103
 
105
- See the [ERB](#erb) template documentation for examples, usage, and options.
104
+ When `true`, `Erubis::EscapedEruby` will be used as the engine class
105
+ instead of the default. All content within `<%= %>` blocks will be
106
+ automatically html escaped.
106
107
 
107
108
  #### `:outvar => '_erbout'`
108
109
 
@@ -110,6 +111,16 @@ The name of the variable used to accumulate template output. This can be
110
111
  any valid Ruby expression but must be assignable. By default a local
111
112
  variable named `_erbout` is used.
112
113
 
114
+ #### `:pattern => '<% %>'`
115
+
116
+ Set pattern for embedded Ruby code.
117
+
118
+ See the [ERB](#erb) template documentation for examples, usage, and options.
119
+
120
+ #### `:trim => true`
121
+
122
+ Delete spaces around '<% %>'. (But, spaces around '<%= %>' are preserved.)
123
+
113
124
  ### See also
114
125
 
115
126
  * [Erubis Home](http://www.kuwata-lab.com/erubis/)
@@ -1,7 +1,7 @@
1
1
  require 'digest/md5'
2
2
 
3
3
  module Tilt
4
- VERSION = '0.8'
4
+ VERSION = '0.9'
5
5
 
6
6
  @template_mappings = {}
7
7
 
@@ -16,6 +16,11 @@ module Tilt
16
16
  mappings[ext.downcase] = template_class
17
17
  end
18
18
 
19
+ # Returns true when a template exists on an exact match of the provided file extension
20
+ def self.registered?(ext)
21
+ mappings.key?(ext.downcase)
22
+ end
23
+
19
24
  # Create a new template for the given file using the file's extension
20
25
  # to determine the the template mapping.
21
26
  def self.new(file, line=nil, options={}, &block)
@@ -29,20 +34,12 @@ module Tilt
29
34
  # Lookup a template class for the given filename or file
30
35
  # extension. Return nil when no implementation is found.
31
36
  def self.[](file)
32
- if @template_mappings.key?(pattern = file.to_s.downcase)
33
- @template_mappings[pattern]
34
- elsif @template_mappings.key?(pattern = File.basename(pattern))
35
- @template_mappings[pattern]
36
- else
37
- while !pattern.empty?
38
- if @template_mappings.key?(pattern)
39
- return @template_mappings[pattern]
40
- else
41
- pattern = pattern.sub(/^[^.]*\.?/, '')
42
- end
43
- end
44
- nil
37
+ pattern = file.to_s.downcase
38
+ unless registered?(pattern)
39
+ pattern = File.basename(pattern)
40
+ pattern.sub!(/^[^.]*\.?/, '') until (pattern.empty? || registered?(pattern))
45
41
  end
42
+ @template_mappings[pattern]
46
43
  end
47
44
 
48
45
  # Mixin allowing template compilation on scope objects.
@@ -100,7 +97,7 @@ module Tilt
100
97
  case
101
98
  when arg.respond_to?(:to_str) ; @file = arg.to_str
102
99
  when arg.respond_to?(:to_int) ; @line = arg.to_int
103
- when arg.respond_to?(:to_hash) ; @options = arg.to_hash
100
+ when arg.respond_to?(:to_hash) ; @options = arg.to_hash.dup
104
101
  else raise TypeError
105
102
  end
106
103
  end
@@ -399,6 +396,16 @@ module Tilt
399
396
 
400
397
  # Erubis template implementation. See:
401
398
  # http://www.kuwata-lab.com/erubis/
399
+ #
400
+ # ErubisTemplate supports the following additional options, which are not
401
+ # passed down to the Erubis engine:
402
+ #
403
+ # :engine_class allows you to specify a custom engine class to use
404
+ # instead of the default (which is ::Erubis::Eruby).
405
+ #
406
+ # :escape_html when true, ::Erubis::EscapedEruby will be used as
407
+ # the engine class instead of the default. All content
408
+ # within <%= %> blocks will be automatically html escaped.
402
409
  class ErubisTemplate < ERBTemplate
403
410
  def initialize_engine
404
411
  return if defined? ::Erubis
@@ -408,7 +415,9 @@ module Tilt
408
415
  def prepare
409
416
  @options.merge!(:preamble => false, :postamble => false)
410
417
  @outvar = (options.delete(:outvar) || '_erbout').to_s
411
- @engine = ::Erubis::Eruby.new(data, options)
418
+ engine_class = options.delete(:engine_class)
419
+ engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
420
+ @engine = (engine_class || ::Erubis::Eruby).new(data, options)
412
421
  end
413
422
 
414
423
  def precompiled_preamble(locals)
@@ -77,6 +77,32 @@ begin
77
77
  assert_equal '2', template.render(scope)
78
78
  assert_equal 'original value', scope.instance_variable_get(:@buf)
79
79
  end
80
+
81
+ test "using Erubis::EscapedEruby subclass via :engine_class option" do
82
+ template = Tilt::ErubisTemplate.new(nil, :engine_class => ::Erubis::EscapedEruby) { |t| %(<%= "<p>Hello World!</p>" %>) }
83
+ assert_equal "&lt;p&gt;Hello World!&lt;/p&gt;", template.render
84
+ end
85
+
86
+ test "using :escape_html => true option" do
87
+ template = Tilt::ErubisTemplate.new(nil, :escape_html => true) { |t| %(<%= "<p>Hello World!</p>" %>) }
88
+ assert_equal "&lt;p&gt;Hello World!&lt;/p&gt;", template.render
89
+ end
90
+
91
+ test "using :escape_html => false option" do
92
+ template = Tilt::ErubisTemplate.new(nil, :escape_html => false) { |t| %(<%= "<p>Hello World!</p>" %>) }
93
+ assert_equal "<p>Hello World!</p>", template.render
94
+ end
95
+
96
+ test "erubis default does not escape html" do
97
+ template = Tilt::ErubisTemplate.new { |t| %(<%= "<p>Hello World!</p>" %>) }
98
+ assert_equal "<p>Hello World!</p>", template.render
99
+ end
100
+
101
+ test "does not modify options argument" do
102
+ options_hash = {:escape_html => true}
103
+ template = Tilt::ErubisTemplate.new(nil, options_hash) { |t| "Hello World!" }
104
+ assert_equal({:escape_html => true}, options_hash)
105
+ end
80
106
  end
81
107
  rescue LoadError => boom
82
108
  warn "Tilt::ErubisTemplate (disabled)\n"
@@ -14,15 +14,23 @@ class TiltTest < Test::Unit::TestCase
14
14
  Tilt.register('mock', MockTemplate)
15
15
  end
16
16
 
17
+ test "an extension is registered if explicit handle is found" do
18
+ Tilt.register('mock', MockTemplate)
19
+ assert Tilt.registered?('mock')
20
+ end
21
+
17
22
  test "registering template classes by symbol file extension" do
18
23
  Tilt.register(:mock, MockTemplate)
19
24
  end
20
25
 
21
- test "looking up template classes by file extension" do
26
+ test "looking up template classes by exact file extension" do
22
27
  Tilt.register('mock', MockTemplate)
23
28
  impl = Tilt['mock']
24
29
  assert_equal MockTemplate, impl
30
+ end
25
31
 
32
+ test "looking up template classes by implicit file extension" do
33
+ Tilt.register('mock', MockTemplate)
26
34
  impl = Tilt['.mock']
27
35
  assert_equal MockTemplate, impl
28
36
  end
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'tilt'
6
- s.version = '0.8'
7
- s.date = '2010-03-07'
6
+ s.version = '0.9'
7
+ s.date = '2010-04-11'
8
8
 
9
9
  s.description = "Generic interface to multiple Ruby template engines"
10
10
  s.summary = s.description
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 8
8
- version: "0.8"
7
+ - 9
8
+ version: "0.9"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ryan Tomayko
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-03-07 00:00:00 -08:00
16
+ date: 2010-04-11 00:00:00 -07:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency