middleman/middleman-core/lib/middleman-core/renderers/haml.rb

61 lines
1.4 KiB
Ruby
Raw Normal View History

# Require gem
require 'haml'
# Require padrino-helpers now so that we get a chance to replace their renderer with ours in Tilt.
require 'padrino-helpers'
2014-02-19 03:30:29 +01:00
module SafeTemplate
def render(*)
super.html_safe
end
end
class Tilt::HamlTemplate
include SafeTemplate
end
2012-04-27 01:15:35 +02:00
module Middleman
module Renderers
2014-01-02 06:19:05 +01:00
# Haml precompiles filters before the scope is even available,
# thus making it impossible to pass our Middleman instance
# in. So we have to resort to heavy hackery :(
class HamlTemplate < ::Tilt::HamlTemplate
def prepare
end
def evaluate(scope, locals, &block)
::Middleman::Renderers::Haml.last_haml_scope = scope
2014-04-29 19:50:21 +02:00
options = @options.merge(filename: eval_file, line: line)
2014-01-02 06:19:05 +01:00
@engine = ::Haml::Engine.new(data, options)
output = @engine.render(scope, locals, &block)
::Middleman::Renderers::Haml.last_haml_scope = nil
output
end
end
2012-04-27 01:15:35 +02:00
# Haml Renderer
class Haml < ::Middleman::Extension
cattr_accessor :last_haml_scope
def initialize(app, options={}, &block)
super
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
# Add haml helpers to context
::Middleman::TemplateContext.send :include, ::Haml::Helpers
2011-11-28 07:04:19 +01:00
end
def add_exposed_to_context(context)
super
context.init_haml_helpers if context.respond_to?(:init_haml_helpers)
end
end
end
end