<commit_before>module Xray

  # This is the main point of integration with Rails. This engine hooks into
  # Sprockets and monkey patches ActionView in order to augment the app's JS
  # and HTML templates with filepath information that can be used by xray.js
  # in the browser. It also hooks in a middleware responsible for injecting
  # xray.js and the xray bar into the app's response bodies.
  class Engine < ::Rails::Engine
    initializer "xray.initialize" do |app|
      app.middleware.use Xray::Middleware

      # Required by Rails 4.1
      app.config.assets.precompile += %w(xray.js xray.css)
    end

    config.after_initialize do |app|
      ensure_asset_pipeline_enabled! app

      # Monkey patch ActionView::Template to augment server-side templates
      # with filepath information. See `Xray.augment_template` for details.
      ActionView::Template.class_eval do
        extend Xray::Aliasing

        def render_with_xray(*args, &block)
          path = identifier
          view = args.first
          source = render_without_xray(*args, &block)

          suitable_template = !(view.respond_to?(:mailer) && view.mailer) &&
                              !path.include?('_xray_bar') &&
                              path =~ /\.(html|slim|haml|hamlc)(\.|$)/ &&
                              path !~ /\.(js|json|css)(\.|$)/

          options = args.last.kind_of?(Hash) ? args.last : {}

          if source && suitable_template && !(options.has_key?(:xray) && (options[:xray] == false))
            Xray.augment_template(source, path)
          else
            source
          end
        end
        xray_method_alias :render
      end

      # Sprockets preprocessor interface which supports all versions of Sprockets.
      # See: https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
      class JavascriptPreprocessor
        def initialize(filename, &block)
          @filename = filename
          @source   = block.call
        end

        def render(context, empty_hash_wtf)
          self.class.run(@filename, @source, context)
        end

        def self.run(filename, source, context)
          path = context.pathname.to_s
          if path =~ /^#{Rails.root}.+\.(jst)(\.|$)/
            Xray.augment_template(source, path)
          else
            source
          end
        end

        def self.call(input)
          filename = input[:filename]
          source   = input[:data]
          context  = input[:environment].context_class.new(input)

          result = run(filename, source, context)
          context.metadata.merge(data: result)
        end
      end

      # Augment JS templates
      app.assets.register_preprocessor 'application/javascript', JavascriptPreprocessor<commit_msg>Adapt to Sprockets 4 (#103)<commit_after>module Xray

  # This is the main point of integration with Rails. This engine hooks into
  # Sprockets and monkey patches ActionView in order to augment the app's JS
  # and HTML templates with filepath information that can be used by xray.js
  # in the browser. It also hooks in a middleware responsible for injecting
  # xray.js and the xray bar into the app's response bodies.
  class Engine < ::Rails::Engine
    initializer "xray.initialize" do |app|
      app.middleware.use Xray::Middleware

      # Required by Rails 4.1
      app.config.assets.precompile += %w(xray.js xray.css)
    end

    config.after_initialize do |app|
      ensure_asset_pipeline_enabled! app

      # Monkey patch ActionView::Template to augment server-side templates
      # with filepath information. See `Xray.augment_template` for details.
      ActionView::Template.class_eval do
        extend Xray::Aliasing

        def render_with_xray(*args, &block)
          path = identifier
          view = args.first
          source = render_without_xray(*args, &block)

          suitable_template = !(view.respond_to?(:mailer) && view.mailer) &&
                              !path.include?('_xray_bar') &&
                              path =~ /\.(html|slim|haml|hamlc)(\.|$)/ &&
                              path !~ /\.(js|json|css)(\.|$)/

          options = args.last.kind_of?(Hash) ? args.last : {}

          if source && suitable_template && !(options.has_key?(:xray) && (options[:xray] == false))
            Xray.augment_template(source, path)
          else
            source
          end
        end
        xray_method_alias :render
      end

      # Sprockets preprocessor interface which supports all versions of Sprockets.
      # See: https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
      class JavascriptPreprocessor
        def initialize(filename, &block)
          @filename = filename
          @source   = block.call
        end

        def render(context, empty_hash_wtf)
          self.class.run(@filename, @source, context)
        end

        def self.run(filename, source, context)
          path = Pathname.new(context.filename).to_s
          if path =~ /^#{Rails.root}.+\.(jst)(\.|$)/
            Xray.augment_template(source, path)
          else
            source
          end
        end

        def self.call(input)
          filename = input[:filename]
          source   = input[:data]
          context  = input[:environment].context_class.new(input)

          result = run(filename, source, context)
          context.metadata.merge(data: result)
        end
      end

      # Augment JS templates
      app.assets.register_preprocessor 'application/javascript', JavascriptPreprocessor
{'fraction_non_alphanumeric': 0.10007072135785007, 'fraction_numerical': 0.0014144271570014145, 'mean_word_length': 2.196045197740113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1955654', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1575, 'n_words': 407}