<commit_before>require 'erubis'
module ActionView ; ENCODING_FLAG = '#.*coding[:=]\s*(\S+)[ \t]*' ;end
require 'action_view/buffers'
require 'action_view/template'

class Flatrack
  module Template
    class Erubis < Tilt::ErubisTemplate

      def self.engine_initialized?
        defined? ActionView::Template::Handlers::Erubis
      end

      def prepare
        source = data
        identifier = File.expand_path @file
        handler = ActionView::Template::Handlers::ERB.new
        @template = ActionView::Template.new(source, identifier, handler, {})
      end

      def precompiled_template(locals)
        binding.pry
        # @template.render(@reader)
      end

    end
  end
end

# Tilt.prefer Flatrack::Template::Erubis, 'erb'<commit_msg>attempt to borrow erb rendering from rails<commit_after>require 'erubis'

module Flatrack
  module Template
    class Erubis < Tilt::ErubisTemplate

      class ErubisHandler < ::Erubis::Eruby
        def add_preamble(src)
          @newline_pending = 0
          src << "@output_buffer = output_buffer || View::OutputBuffer.new;"
        end

        def add_text(src, text)
          return if text.empty?
          if text == "\n"
            @newline_pending += 1
          else
            src << "@output_buffer.safe_append='"
            src << "\n" * @newline_pending if @newline_pending > 0
            src << escape_text(text)
            src << "'.freeze;"
            @newline_pending = 0
          end
        end

        # Erubis toggles <%= and <%== behavior when escaping is enabled.
        # We override to always treat <%== as escaped.
        def add_expr(src, code, indicator)
          case indicator
          when '=='
            add_expr_escaped(src, code)
          else
            super
          end
        end

        BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

        def add_expr_literal(src, code)
          flush_newline_if_pending(src)
          if code =~ BLOCK_EXPR
            src << '@output_buffer.append= ' << code
          else
            src << '@output_buffer.append=(' << code << ');'
          end
        end

        def add_expr_escaped(src, code)
          flush_newline_if_pending(src)
          if code =~ BLOCK_EXPR
            src << "@output_buffer.safe_append= " << code
          else
            src << "@output_buffer.safe_append=(" << code << ");"
          end
        end

        def add_stmt(src, code)
          flush_newline_if_pending(src)
          super
        end

        def add_postamble(src)
          flush_newline_if_pending(src)
          src << '@output_buffer.to_s'
        end

        def flush_newline_if_pending(src)
          if @newline_pending > 0
            src << "@output_buffer.safe_append='#{"\n" * @newline_pending}'.freeze;"
            @newline_pending = 0
          end
        end
      end

      def self.engine_initialized?
        defined? ErubisHandler
      end

      def prepare
        @outvar = options.delete(:outvar) || self.class.default_output_variable
        @options.merge!(trim: true)
        @engine = ErubisHandler.new(data, options)
      end

    end
  end
end

Tilt.prefer Flatrack::Template::ErubisTemplate, 'erb'
{'fraction_non_alphanumeric': 0.10902842861605748, 'fraction_numerical': 0.0018744142455482662, 'mean_word_length': 1.9730733519034356, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '795046', 'n_tokens_mistral': 996, 'n_tokens_neox': 954, 'n_words': 212}