<commit_before>if defined?(Rails) && Rails.env.development?

  require "xray/version"
  require "open3"

  module Xray
    CONSTRUCTOR_REGEX = /^( *)(?!_)([\w\.]+) *= *(\(function\(_super\)|(?!jQuery|_)[\w\.]+.extend\()/

    def self.request_info
      @request_info ||= {}
    end

    def self.augment_js(source, path)
      source.gsub(CONSTRUCTOR_REGEX) do
        space, class_name, func = $1, $2, $3
        info = {name: class_name, path: path.to_s}
        xray = "(window.XrayPaths||(window.XrayPaths={}))['#{info.to_json}']"
        "#{space}#{class_name} = #{xray} = #{func}"
      end
    end

    def self.augment_template(source, path)
      id = next_id
      augmented = "<!-- XRAY START #{id} #{path} -->\n#{source}\n<!-- XRAY END #{id} -->"
      ActiveSupport::SafeBuffer === source ? ActiveSupport::SafeBuffer.new(augmented) : augmented
    end

    def self.next_id
      cur_id = (@id ||= 1)
      @id += 1
      cur_id
    end

    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        if env['PATH_INFO'] == '/xray/open'
          req, res = Rack::Request.new(env), Rack::Response.new
          out, err, status = Open3.capture3('/usr/local/bin/subl', req.GET['path'])
          if status.success?
            res.status = 200
          else
            res.write out
            res.status = 500
          end
          res.finish
        else
          status, headers, response = @app.call(env)
          return [status, headers, response] if file?(headers) || empty?(response)

          if status == 200 && !response.body.frozen? && html_request?(headers, response)
            body = response.body.sub(/<body.*>/) { "#{$~}\n#{xray_content}" }
            append_js!(body, 'jquery', :xray)
            append_js!(body, 'backbone', :'xray-backbone')
            headers['Content-Length'] = body.bytesize.to_s
          end
          [status, headers, body ? [body] : response]<commit_msg>Fix regex greediness on xray bar insertion<commit_after>if defined?(Rails) && Rails.env.development?

  require "xray/version"
  require "open3"

  module Xray
    CONSTRUCTOR_REGEX = /^( *)(?!_)([\w\.]+) *= *(\(function\(_super\)|(?!jQuery|_)[\w\.]+.extend\()/

    def self.request_info
      @request_info ||= {}
    end

    def self.augment_js(source, path)
      source.gsub(CONSTRUCTOR_REGEX) do
        space, class_name, func = $1, $2, $3
        info = {name: class_name, path: path.to_s}
        xray = "(window.XrayPaths||(window.XrayPaths={}))['#{info.to_json}']"
        "#{space}#{class_name} = #{xray} = #{func}"
      end
    end

    def self.augment_template(source, path)
      id = next_id
      augmented = "<!-- XRAY START #{id} #{path} -->\n#{source}\n<!-- XRAY END #{id} -->"
      ActiveSupport::SafeBuffer === source ? ActiveSupport::SafeBuffer.new(augmented) : augmented
    end

    def self.next_id
      cur_id = (@id ||= 1)
      @id += 1
      cur_id
    end

    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        if env['PATH_INFO'] == '/xray/open'
          req, res = Rack::Request.new(env), Rack::Response.new
          out, err, status = Open3.capture3('/usr/local/bin/subl', req.GET['path'])
          if status.success?
            res.status = 200
          else
            res.write out
            res.status = 500
          end
          res.finish
        else
          status, headers, response = @app.call(env)
          return [status, headers, response] if file?(headers) || empty?(response)

          if status == 200 && !response.body.frozen? && html_request?(headers, response)
            body = response.body.sub(/<body[^>]*>/) { "#{$~}\n#{xray_content}" }
            append_js!(body, 'jquery', :xray)
            append_js!(body, 'backbone', :'xray-backbone')
            headers['Content-Length'] = body.bytesize.to_s
          end
          [status, headers, body ? [body] : response]
{'fraction_non_alphanumeric': 0.16843434343434344, 'fraction_numerical': 0.008585858585858586, 'mean_word_length': 2.2203252032520324, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '5170750', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1295, 'n_words': 273}