<commit_before>module RSpec
  module Core
    module Formatters
      # This class extracts code snippets by looking at the backtrace of the passed error
      class SnippetExtractor
        class NullConverter; def convert(code, pre); code; end; end

        begin
          require 'syntax/convertors/html'
          @@converter = Syntax::Convertors::HTML.for_syntax "ruby"
        rescue LoadError
          @@converter = NullConverter.new
        end

        def snippet(backtrace)
          raw_code, line = snippet_for(backtrace[0])
          highlighted = @@converter.convert(raw_code, false)
          highlighted << "\n<span class=\"comment\"># gem install syntax to get syntax highlighting</span>" if @@converter.is_a?(NullConverter)
          post_process(highlighted, line)
        end

        def snippet_for(error_line)
          if error_line =~ /(.*):(\d+)/
            file = $1
            line = $2.to_i
            [lines_around(file, line), line]
          else
            ["# Couldn't get snippet for #{error_line}", 1]
          end
        end

        def lines_around(file, line)
          if File.file?(file)
            lines = File.read(file).split("\n")
            min = [0, line-3].max
            max = [line+1, lines.length-1].min
            selected_lines = []
            selected_lines.join("\n")
            lines[min..max].join("\n")
          else
            "# Couldn't get snippet for #{file}"
          end
        rescue SecurityError
          "# Couldn't get snippet for #{file}"
        end

        def post_process(highlighted, offending_line)
          new_lines = []<commit_msg>documentation for Formatters::SnippetExtractor<commit_after>module RSpec
  module Core
    module Formatters
      # Extracts code snippets by looking at the backtrace of the passed error and applies synax highlighting and line numbers using html.
      class SnippetExtractor
        class NullConverter; def convert(code, pre); code; end; end

        begin
          require 'syntax/convertors/html'
          @@converter = Syntax::Convertors::HTML.for_syntax "ruby"
        rescue LoadError
          @@converter = NullConverter.new
        end

        # @api public
        #
        # Extract lines of code corresponding to  a backtrace.
        #
        # @param [String] backtrace the backtrace from a test failure
        # @return [String] highlighted code snippet indicating where the test failure occured
        #
        # @see #post_process
        def snippet(backtrace)
          raw_code, line = snippet_for(backtrace[0])
          highlighted = @@converter.convert(raw_code, false)
          highlighted << "\n<span class=\"comment\"># gem install syntax to get syntax highlighting</span>" if @@converter.is_a?(NullConverter)
          post_process(highlighted, line)
        end

        # @api public
        #
        # Create a snippet from a line of code.
        #
        # @param [String] error_line file name with line number (i.e. 'foo_spec.rb:12')
        # @return [String] lines around the target line within the file
        #
        # @see #lines_around
        def snippet_for(error_line)
          if error_line =~ /(.*):(\d+)/
            file = $1
            line = $2.to_i
            [lines_around(file, line), line]
          else
            ["# Couldn't get snippet for #{error_line}", 1]
          end
        end

        # @api public
        #
        # Extract lines of code centered around a particular line within a source file.
        #
        # @param [String] file filename
        # @param [Fixnum] line line number
        # @return [String] lines around the target line within the file (2 above and 1 below).
        def lines_around(file, line)
          if File.file?(file)
            lines = File.read(file).split("\n")
            min = [0, line-3].max
            max = [line+1, lines.length-1].min
            selected_lines = []
            selected_lines.join("\n")
            lines[min..max].join("\n")
          else
            "# Couldn't get snippet for #{file}"
          end
        rescue SecurityError
          "# Couldn't get snippet for #{file}"
        end

        # @api public
        #
        # Adds line numbers to all lines and highlights the line where the failure occurred using html `span` tags.
        #
        # @param [String] highlighted syntax-highlighted snippet surrounding the offending line of code
        # @param [Fixnum] offending_line line where failure occured
        # @return [String] completed snippet
        def post_process(highlighted, offending_line)
          new_lines = []
{'fraction_non_alphanumeric': 0.09481641468682506, 'fraction_numerical': 0.004319654427645789, 'mean_word_length': 1.9421855146124523, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '2227123', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1225, 'n_words': 394}