<commit_before>module Diagnosticss
  class Middleware
    MIME_TYPES = ['text/html', 'application/xhtml+xml']
    CSS_PATH = '/diagnosticss.css'

    def initialize(app)
      @app = app
    end

    def call(env)
      @request = Rack::Request.new(env)

      if diagnosticss_stylesheet_request?
        body = File.read(File.dirname(__FILE__) + CSS_PATH)
        @response = Rack::Response.new(body, 200, {})
      else
        status, headers, body = @app.call(env)
        @response = Rack::Response.new(body, status, headers)

        inject_css if response_okay_to_modify?
      end

      return @response.to_a
    end

    private

    def response_okay_to_modify?
      content_type, charset = @response.content_type.split(";")
      diagnosticss = @request.params['diagnosticss']
      @response.ok? && MIME_TYPES.include?(content_type) && diagnosticss
    end

    def diagnosticss_stylesheet_request?
      @request.path_info == CSS_PATH
    end

    def inject_css
      full_body = @response.body.join
      full_body.sub! /<\/body>/, css + '</body>'

      @response["Content-Length"] = full_body.bytesize.to_s

      # Ensure that browser does
      @response["Etag"] = ""
      @response["Cache-Control"] = "no-cache"

      @response.body = [full_body]
    end

    def css
      %{<link href="#{CSS_PATH}" media="screen" rel="stylesheet" type="text/css" />\n}
    end
  end
end<commit_msg>use media='all' since diagnosticss also detects print styles problems<commit_after>module Diagnosticss
  class Middleware
    MIME_TYPES = ['text/html', 'application/xhtml+xml']
    CSS_PATH = '/diagnosticss.css'

    def initialize(app)
      @app = app
    end

    def call(env)
      @request = Rack::Request.new(env)

      if diagnosticss_stylesheet_request?
        body = File.read(File.dirname(__FILE__) + CSS_PATH)
        @response = Rack::Response.new(body, 200, {})
      else
        status, headers, body = @app.call(env)
        @response = Rack::Response.new(body, status, headers)

        inject_css if response_okay_to_modify?
      end

      return @response.to_a
    end

    private

    def response_okay_to_modify?
      content_type, charset = @response.content_type.split(";")
      diagnosticss = @request.params['diagnosticss']
      @response.ok? && MIME_TYPES.include?(content_type) && diagnosticss
    end

    def diagnosticss_stylesheet_request?
      @request.path_info == CSS_PATH
    end

    def inject_css
      full_body = @response.body.join
      full_body.sub! /<\/body>/, css + '</body>'

      @response["Content-Length"] = full_body.bytesize.to_s

      # Ensure that browser does
      @response["Etag"] = ""
      @response["Cache-Control"] = "no-cache"

      @response.body = [full_body]
    end

    def css
      %{<link href="#{CSS_PATH}" media="all" rel="stylesheet" type="text/css" />\n}
    end
  end
end
{'fraction_non_alphanumeric': 0.12652492157546183, 'fraction_numerical': 0.0020913210177762286, 'mean_word_length': 2.8215712383488682, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7224063', 'n_tokens_mistral': 944, 'n_tokens_neox': 916, 'n_words': 187}