1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# frozen_string_literal: true
require 'temple'
require 'hamlit/engine'
require 'hamlit/rails_helpers'
require 'hamlit/parser/haml_helpers'
require 'hamlit/parser/haml_util'
module Hamlit
class RailsTemplate
# Compatible with: https://github.com/judofyr/temple/blob/v0.7.7/lib/temple/mixins/options.rb#L15-L24
class << self
def options
@options ||= {
generator: Temple::Generators::RailsOutputBuffer,
use_html_safe: true,
streaming: true,
buffer_class: 'ActionView::OutputBuffer',
}
end
def set_options(opts)
options.update(opts)
end
end
def call(template, source = nil)
source ||= template.source
options = RailsTemplate.options
# https://github.com/haml/haml/blob/4.0.7/lib/haml/template/plugin.rb#L19-L20
# https://github.com/haml/haml/blob/4.0.7/lib/haml/options.rb#L228
if template.respond_to?(:type) && template.type == 'text/xml'
options = options.merge(format: :xhtml)
end
if ActionView::Base.try(:annotate_rendered_view_with_filenames) && template.format == :html
options[:preamble] = "<!-- BEGIN #{template.short_identifier} -->\n"
options[:postamble] = "<!-- END #{template.short_identifier} -->\n"
end
Engine.new(options).call(source)
end
def supports_streaming?
RailsTemplate.options[:streaming]
end
end
ActionView::Template.register_template_handler(:haml, RailsTemplate.new)
# https://github.com/haml/haml/blob/4.0.7/lib/haml/template.rb
module HamlHelpers
require 'hamlit/parser/haml_helpers/xss_mods'
include Hamlit::HamlHelpers::XssMods
end
module HamlUtil
undef :rails_xss_safe? if defined? rails_xss_safe?
def rails_xss_safe?; true; end
end
end
# Haml extends Haml::Helpers in ActionView each time.
# It costs much, so Hamlit includes a compatible module at first.
ActionView::Base.send :include, Hamlit::RailsHelpers
|