Sha256: d5ed2128c2d0cc96e8b0420e6b30933bf9b609a5c21664846896b7223e430929

Contents?: true

Size: 1.94 KB

Versions: 16

Compression:

Stored size: 1.94 KB

Contents

module Padrino
  module Rendering
    ##
    # SafeBufferEnhancer is an Erubis Enhancer that compiles templates that
    # are fit for using ActiveSupport::SafeBuffer as a Buffer.
    #
    # @api private
    module SafeBufferEnhancer
      def add_expr_literal(src, code)
        src << " #{@bufvar}.concat((" << code << ').to_s);'
      end

      def add_stmt(src, code)
        code = code.sub('end', 'nil;end') if code =~ /\A\s*end\s*\Z/
        src << code
        src << ';' unless code[-1] == ?\n
      end

      def add_expr_escaped(src, code)
        src << " #{@bufvar}.safe_concat " << code << ';'
      end

      def add_text(src, text)
        src << " #{@bufvar}.safe_concat '" << escape_text(text) << "';" unless text.empty?
      end
    end

    ##
    # SafeBufferTemplate is the Eruby engine, augmented with SafeBufferEnhancer.
    #
    # @api private
    class SafeEruby < ::Erubis::Eruby
      include SafeBufferEnhancer
    end

    ##
    # Modded ErubisTemplate that doesn't insist in an String as output
    # buffer.
    #
    # @api private
    class ErubisTemplate < Tilt::ErubisTemplate
      def render(*args)
        app       = args.first
        app_class = app.class
        @is_padrino_app = (defined?(Padrino::Application) && app.kind_of?(Padrino::Application)) || 
                          (app_class.respond_to?(:erb) && app_class.erb[:engine_class] == Padrino::Rendering::SafeEruby)
        super
      end

      ##
      # In preamble we need a flag `__in_erb_template` and SafeBuffer for padrino apps.
      #
      def precompiled_preamble(locals)
        original = super
        return original unless @is_padrino_app
        "__in_erb_template = true\n" << original.rpartition("\n").first << "#{@outvar} = _buf = ActiveSupport::SafeBuffer.new\n"
      end
    end
  end
end

Tilt.prefer(Padrino::Rendering::ErubisTemplate, :erb)

Padrino::Rendering.engine_configurations[:erb] = {
  :engine_class => Padrino::Rendering::SafeEruby,
}

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
padrino-helpers-0.12.8.1 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.8 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.7 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.6 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.2 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.1 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.1.beta1 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.0 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.0.beta3 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.0.beta2 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.5 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.13.0.beta1 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.4 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.3 lib/padrino/rendering/erubis_template.rb
padrino-helpers-cj-0.12.2 lib/padrino/rendering/erubis_template.rb
padrino-helpers-0.12.2 lib/padrino/rendering/erubis_template.rb