<commit_before>require 'rouge/plugins/redcarpet'

module Banzai
  module Filter
    # HTML Filter to highlight fenced code blocks
    #
    class SyntaxHighlightFilter < HTML::Pipeline::Filter
      include Rouge::Plugins::Redcarpet

      def call
        doc.search('pre > code').each do |node|
          highlight_node(node)
        end

        doc
      end

      def highlight_node(node)
        language = node.attr('class')
        code     = node.text

        begin
          highlighted = %<<pre class="#{css_classes}"><code>#{block_code(code, language)}</code></pre>>
        rescue
          # Gracefully handle syntax highlighter bugs/errors to ensure
          # users can still access an issue/comment/etc.
          highlighted = "<pre>#{code}</pre>"
        end

        # Extracted to a method to measure it
        replace_parent_pre_element(node, highlighted)
      end

      def css_classes
        "code highlight js-syntax-highlight #{lexer.tag}"
      end

      private

      def replace_parent_pre_element(node, highlighted)
        # Replace the parent `pre` element with the entire highlighted block
        node.parent.replace(highlighted)
      end

      # Override Rouge::Plugins::Redcarpet#rouge_formatter
      def rouge_formatter(lexer)
        Rouge::Formatters::HTML.new
      end
    end
  end
end<commit_msg>bugfix: don't error in css_classes<commit_after>require 'rouge/plugins/redcarpet'

module Banzai
  module Filter
    # HTML Filter to highlight fenced code blocks
    #
    class SyntaxHighlightFilter < HTML::Pipeline::Filter
      include Rouge::Plugins::Redcarpet

      def call
        doc.search('pre > code').each do |node|
          highlight_node(node)
        end

        doc
      end

      def highlight_node(node)
        language = node.attr('class')
        code     = node.text

        lexer = Rouge::Lexer.find_fancy(language)
        formatter = Rouge::Formatters::HTML.new
        css_classes = "code highlight js-syntax-highlight #{lexer.tag}"

        begin
          highlighted = ''
          highlighted << %<<pre class="#{css_classes}"><code>>
          highlighted << formatter.format(lexer.lex(code))
          highlighted << %<</code></pre>>
        rescue
          # Gracefully handle syntax highlighter bugs/errors to ensure
          # users can still access an issue/comment/etc.
          highlighted = "<pre>#{code}</pre>"
        end

        # Extracted to a method to measure it
        replace_parent_pre_element(node, highlighted)
      end

      private

      def replace_parent_pre_element(node, highlighted)
        # Replace the parent `pre` element with the entire highlighted block
        node.parent.replace(highlighted)
      end

      # Override Rouge::Plugins::Redcarpet#rouge_formatter
      def rouge_formatter(lexer)
        Rouge::Formatters::HTML.new
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08996539792387544, 'fraction_numerical': 0.0, 'mean_word_length': 2.3538283062645013, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 20, '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': '6950501', 'n_tokens_mistral': 826, 'n_tokens_neox': 789, 'n_words': 205}