<commit_before>require 'tilt'
require 'json'

module HandlebarsAssets
  module Unindent
    # http://bit.ly/aze9FV
    # Strip leading whitespace from each line that is the same as the
    # amount of whitespace on the first line of the string.
    # Leaves _additional_ indentation on later lines intact.
    def unindent(heredoc)
      heredoc.gsub(/^#{heredoc[/\A\s*/]}/, '')
    end
  end

  # Sprockets <= 3
  class HandlebarsTemplate < Tilt::Template
    def self.default_mime_type
      'application/javascript'
    end

    def initialize_engine
      HandlebarsRenderer.initialize_engine
    end

    def prepare
      @engine = renderer.choose_engine(data)
    end

    def evaluate(scope, locals, &block)
      source = @engine.render(scope, locals, &block)
      renderer.compile(source)
    end

    private

    def renderer
      @renderer ||= HandlebarsRenderer.new(path: @file)
    end
  end

  # Sprockets 4
  class HandlebarsProcessor

    def self.instance
      @instance ||= new
    end

    def self.call(input)
      instance.call(input)
    end

    def self.cache_key
      instance.cache_key
    end

    attr_reader :cache_key

    def initialize(options = {})
      @cache_key = [self.class.name, ::HandlebarsAssets::VERSION, options].freeze
    end

    def call(input)
      renderer = HandlebarsRenderer.new(path: input[:filename])
      engine = renderer.choose_engine(input[:data])
      renderer.compile(engine.render)
    end
  end

  class NoOpEngine
    def initialize(data)
      @data = data
    end

    def render(*args)
      @data
    end
  end

  class HandlebarsRenderer
    include Unindent

    def self.initialize_engine
      return if @initialized

      begin
        require 'haml'
      rescue LoadError
        # haml not available
      end
      begin
        require 'slim'
      rescue LoadError
        # slim not available
      end

      @initialized = true
    end

    def initialize(options)
      self.class.initialize_engine
      @template_path = TemplatePath.new(options[:path])
    end

    def choose_engine(data)
      if @template_path.is_haml?
        Haml::Engine.new(data, HandlebarsAssets::Config.haml_options)
      elsif @template_path.is_slim?
        Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }
      else
        NoOpEngine.new(data)
      end
    end

    def compile(source)
      # remove trailing \n on file, for some reason the directives pipeline adds this
      source.chomp!($/)

      # handle the case of multiple frameworks combined with ember
      # DEFER: use extension setup for ember
      if (HandlebarsAssets::Config.multiple_frameworks? && @template_path.is_ember?) ||
         (HandlebarsAssets::Config.ember? && !HandlebarsAssets::Config.multiple_frameworks?)
        compile_ember(source)
      else
        compile_default(source)
      end
    end

    def compile_ember(source)
      "window.Ember.TEMPLATES[#{@template_path.name}] = Ember.Handlebars.compile(#{JSON.dump(source)});"
    end

    def compile_default(source)<commit_msg>Trying to compile ember templates correctly<commit_after>require 'tilt'
require 'json'

module HandlebarsAssets
  module Unindent
    # http://bit.ly/aze9FV
    # Strip leading whitespace from each line that is the same as the
    # amount of whitespace on the first line of the string.
    # Leaves _additional_ indentation on later lines intact.
    def unindent(heredoc)
      heredoc.gsub(/^#{heredoc[/\A\s*/]}/, '')
    end
  end

  # Sprockets <= 3
  class HandlebarsTemplate < Tilt::Template
    def self.default_mime_type
      'application/javascript'
    end

    def initialize_engine
      HandlebarsRenderer.initialize_engine
    end

    def prepare
      @engine = renderer.choose_engine(data)
    end

    def evaluate(scope, locals, &block)
      source = @engine.render(scope, locals, &block)
      renderer.compile(source)
    end

    private

    def renderer
      @renderer ||= HandlebarsRenderer.new(path: @file)
    end
  end

  # Sprockets 4
  class HandlebarsProcessor

    def self.instance
      @instance ||= new
    end

    def self.call(input)
      instance.call(input)
    end

    def self.cache_key
      instance.cache_key
    end

    attr_reader :cache_key

    def initialize(options = {})
      @cache_key = [self.class.name, ::HandlebarsAssets::VERSION, options].freeze
    end

    def call(input)
      renderer = HandlebarsRenderer.new(path: input[:filename])
      engine = renderer.choose_engine(input[:data])
      renderer.compile(engine.render)
    end
  end

  class NoOpEngine
    def initialize(data)
      @data = data
    end

    def render(*args)
      @data
    end
  end

  class HandlebarsRenderer
    include Unindent

    def self.initialize_engine
      return if @initialized

      begin
        require 'haml'
      rescue LoadError
        # haml not available
      end
      begin
        require 'slim'
      rescue LoadError
        # slim not available
      end

      @initialized = true
    end

    def initialize(options)
      self.class.initialize_engine
      @template_path = TemplatePath.new(options[:path])
    end

    def choose_engine(data)
      if @template_path.is_haml?
        Haml::Engine.new(data, HandlebarsAssets::Config.haml_options)
      elsif @template_path.is_slim?
        Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }
      else
        NoOpEngine.new(data)
      end
    end

    def compile(source)
      # remove trailing \n on file, for some reason the directives pipeline adds this
      source.chomp!($/)

      compile_ember(source)

      # handle the case of multiple frameworks combined with ember
      # DEFER: use extension setup for ember
      # if (HandlebarsAssets::Config.multiple_frameworks? && @template_path.is_ember?) ||
      #    (HandlebarsAssets::Config.ember? && !HandlebarsAssets::Config.multiple_frameworks?)
      #   compile_ember(source)
      # else
      #   compile_default(source)
      # end
    end

    def compile_ember(source)
      "window.Ember.TEMPLATES[#{@template_path.name}] = Ember.Handlebars.compile(#{JSON.dump(source)});"
    end

    def compile_default(source)
{'fraction_non_alphanumeric': 0.08673222634209253, 'fraction_numerical': 0.0009672738997259391, 'mean_word_length': 2.6280701754385967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '7548288', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1839, 'n_words': 455}