module Tilt

def self.[](file)

extension. Return nil when no implementation is found.
Lookup a template class for the given filename or file
def self.[](file)
  if @template_mappings.key?(pattern = file.to_s.downcase)
    @template_mappings[pattern]
  elsif @template_mappings.key?(pattern = File.basename(pattern))
    @template_mappings[pattern]
  else
    while !pattern.empty?
      if @template_mappings.key?(pattern)
        return @template_mappings[pattern]
      else
        pattern = pattern.sub(/^[^.]*\.?/, '')
      end
    end
    nil
  end
end

def self.mappings

Hash of template path pattern => template implementation class mappings.
def self.mappings
  @template_mappings
end

def self.new(file, line=nil, options={}, &block)

to determine the the template mapping.
Create a new template for the given file using the file's extension
def self.new(file, line=nil, options={}, &block)
  if template_class = self[file]
    template_class.new(file, line, options, &block)
  else
    fail "No template engine registered for #{File.basename(file)}"
  end
end

def self.register(ext, template_class)

Register a template implementation by file extension.
def self.register(ext, template_class)
  ext = ext.to_s.sub(/^\./, '')
  mappings[ext.downcase] = template_class
end