Sha256: 7954f4c124fce4fbc169deac0183f7c50838b1f2faa1b87e19e288470f57f855

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

# 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

      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_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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
hamlit-2.12.0-java lib/hamlit/rails_template.rb
hamlit-2.12.0 lib/hamlit/rails_template.rb
hamlit-2.11.1 lib/hamlit/rails_template.rb
hamlit-2.11.0-java lib/hamlit/rails_template.rb
hamlit-2.11.0 lib/hamlit/rails_template.rb
hamlit-2.10.1-java lib/hamlit/rails_template.rb
hamlit-2.10.1 lib/hamlit/rails_template.rb
hamlit-2.10.0-java lib/hamlit/rails_template.rb
hamlit-2.10.0 lib/hamlit/rails_template.rb
hamlit-2.9.5-java lib/hamlit/rails_template.rb
hamlit-2.9.4-java lib/hamlit/rails_template.rb
hamlit-2.9.5 lib/hamlit/rails_template.rb
hamlit-2.9.4 lib/hamlit/rails_template.rb
hamlit-2.9.3-java lib/hamlit/rails_template.rb
hamlit-2.9.3 lib/hamlit/rails_template.rb