2013-10-20 22:04:01 +02:00
|
|
|
class Renderer
|
|
|
|
COMPONENT_MATCHER = %r{/?(?<template>[^\.]+)\.(?<type>[^\.]+)\.(?<engine>.+)}
|
|
|
|
DEFAULT_TYPE = :default_type
|
|
|
|
|
|
|
|
def self.call(session)
|
2013-10-27 19:45:12 +01:00
|
|
|
template = do_checks(session)
|
|
|
|
return nil unless template
|
|
|
|
type = get_type(session, template)
|
|
|
|
session.response.body = render(template, type, session.options)
|
|
|
|
session.response.content_type = type_map[type]
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.render(template, type, object)
|
|
|
|
layout = templates['layout']
|
|
|
|
if layout && layout.has_key?(type)
|
|
|
|
return layout[type].render(object) do
|
|
|
|
template[type].render(object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
template[type].render(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.do_checks(session)
|
2013-10-20 22:04:01 +02:00
|
|
|
unless session.options.has_key? :render
|
|
|
|
session.response.body = ':render not set!'
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
to_render = session.options[:render]
|
|
|
|
unless templates.has_key? to_render
|
|
|
|
session.response.body = 'Template does not exist!'
|
|
|
|
return nil
|
|
|
|
end
|
2013-10-27 19:45:12 +01:00
|
|
|
return templates[to_render]
|
2013-10-20 22:04:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# initialize the template cache
|
|
|
|
def self.find_templates(path)
|
2013-10-27 19:45:12 +01:00
|
|
|
templates = {}
|
2013-10-20 22:04:01 +02:00
|
|
|
Dir[path + '/**/*'].each do |file|
|
|
|
|
parts = file.gsub(path, '').match(COMPONENT_MATCHER)
|
2013-10-27 19:45:12 +01:00
|
|
|
next unless File.file?(file) && parts
|
|
|
|
templates[parts[:template]] ||= {}
|
|
|
|
templates[parts[:template]].merge!({parts[:type].to_sym => Tilt.new(file)})
|
2013-10-20 22:04:01 +02:00
|
|
|
end
|
2013-10-27 19:45:12 +01:00
|
|
|
templates
|
2013-10-20 22:04:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.templates
|
2013-10-27 19:45:12 +01:00
|
|
|
@@templates ||= find_templates 'templates'
|
2013-10-20 22:04:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_types(types)
|
2013-10-27 19:45:12 +01:00
|
|
|
@@type_map = types.invert
|
2013-10-20 22:04:01 +02:00
|
|
|
@@types = types
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.types
|
|
|
|
@@types ||= {
|
|
|
|
'*/*' => DEFAULT_TYPE,
|
|
|
|
'text/css' => :css,
|
|
|
|
'text/html' => :html,
|
|
|
|
'application/atom+xml' => :atom
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-10-26 12:33:30 +02:00
|
|
|
def self.type_map
|
2013-10-27 19:45:12 +01:00
|
|
|
@@type_map ||= types.invert
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_type(session, template)
|
|
|
|
type = get_preferred_type(session.request, template)
|
|
|
|
return template.first[0] if type == DEFAULT_TYPE
|
|
|
|
type
|
2013-10-26 12:33:30 +02:00
|
|
|
end
|
|
|
|
|
2013-10-27 19:45:12 +01:00
|
|
|
def self.get_preferred_type(request, template)
|
|
|
|
request.accept.types.each do |type|
|
2013-10-20 22:04:01 +02:00
|
|
|
next unless types.has_key? type
|
|
|
|
return types[type] if template.has_key? types[type]
|
|
|
|
return DEFAULT_TYPE if types[type] == DEFAULT_TYPE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|