Tilt

Constants

VERSION
TOPOBJECT

Public Class Methods

[](file) click to toggle source

Lookup a template class for the given filename or file extension. Return nil when no implementation is found.

     # File lib/tilt.rb, line 69
 69:   def self.[](file)
 70:     pattern = file.to_s.downcase
 71:     until pattern.empty? || registered?(pattern)
 72:       pattern = File.basename(pattern)
 73:       pattern.sub!(/^[^.]*\.?/, '')
 74:     end
 75: 
 76:     # Try to find a preferred engine.
 77:     preferred_klass = @preferred_mappings[pattern]
 78:     return preferred_klass if preferred_klass
 79: 
 80:     # Fall back to the general list of mappings.
 81:     klasses = @template_mappings[pattern]
 82: 
 83:     # Try to find an engine which is already loaded.
 84:     template = klasses.detect do |klass|
 85:       if klass.respond_to?(:engine_initialized?)
 86:         klass.engine_initialized?
 87:       end
 88:     end
 89: 
 90:     return template if template
 91: 
 92:     # Try each of the classes until one succeeds. If all of them fails,
 93:     # we'll raise the error of the first class.
 94:     first_failure = nil
 95: 
 96:     klasses.each do |klass|
 97:       begin
 98:         klass.new { '' }
 99:       rescue Exception => ex
100:         first_failure ||= ex
101:         next
102:       else
103:         return klass
104:       end
105:     end
106: 
107:     raise first_failure if first_failure
108:   end
mappings() click to toggle source

Hash of template path pattern => template implementation class mappings.

    # File lib/tilt.rb, line 8
 8:   def self.mappings
 9:     @template_mappings
10:   end
new(file, line=nil, options={}, &block) click to toggle source

Create a new template for the given file using the file’s extension to determine the the template mapping.

    # File lib/tilt.rb, line 59
59:   def self.new(file, line=nil, options={}, &block)
60:     if template_class = self[file]
61:       template_class.new(file, line, options, &block)
62:     else
63:       fail "No template engine registered for #{File.basename(file)}"
64:     end
65:   end
normalize(ext) click to toggle source
    # File lib/tilt.rb, line 12
12:   def self.normalize(ext)
13:     ext.to_s.downcase.sub(/^\./, '')
14:   end
prefer(template_class, *extensions) click to toggle source

Makes a template class preferred for the given file extensions. If you don’t provide any extensions, it will be preferred for all its already registered extensions:

  # Prefer RDiscount for its registered file extensions:
  Tilt.prefer(Tilt::RDiscountTemplate)

  # Prefer RDiscount only for the .md extensions:
  Tilt.prefer(Tilt::RDiscountTemplate, '.md')
    # File lib/tilt.rb, line 38
38:   def self.prefer(template_class, *extensions)
39:     if extensions.empty?
40:       mappings.each do |ext, klasses|
41:         @preferred_mappings[ext] = template_class if klasses.include? template_class
42:       end
43:     else
44:       extensions.each do |ext|
45:         ext = normalize(ext)
46:         register(template_class, ext)
47:         @preferred_mappings[ext] = template_class
48:       end
49:     end
50:   end
register(template_class, *extensions) click to toggle source

Register a template implementation by file extension.

    # File lib/tilt.rb, line 17
17:   def self.register(template_class, *extensions)
18:     if template_class.respond_to?(:to_str)
19:       # Support register(ext, template_class) too
20:       extensions, template_class = [template_class], extensions[0]
21:     end
22: 
23:     extensions.each do |ext|
24:       ext = normalize(ext)
25:       mappings[ext].unshift(template_class).uniq!
26:     end
27:   end
registered?(ext) click to toggle source

Returns true when a template exists on an exact match of the provided file extension

    # File lib/tilt.rb, line 53
53:   def self.registered?(ext)
54:     mappings.key?(ext.downcase) && !mappings[ext.downcase].empty?
55:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.