| TOPOBJECT | = | defined?(BasicObject) ? BasicObject : Object |
| VERSION | = | '1.3.2' |
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
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
# File lib/tilt.rb, line 12
12: def self.normalize(ext)
13: ext.to_s.downcase.sub(/^\./, '')
14: end
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 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