<commit_before># frozen_string_literal: true

HTML::Pipeline.require_dependency('rouge', 'SyntaxHighlightFilter')

module HTML
  class Pipeline
    # HTML Filter that syntax highlights code blocks wrapped
    # in <pre lang="...">.
    class SyntaxHighlightFilter < Filter
      def initialize(*args)
        super(*args)
        @formatter = Rouge::Formatters::HTML.new
      end

      def call
        doc.search('pre').each do |node|
          default = context[:highlight] && context[:highlight].to_s
          next unless lang = node['lang'] || default
          next unless lexer = lexer_for(lang)
          text = node.inner_text

          html = highlight_with_timeout_handling(text, lang)
          next if html.nil?

          node.inner_html = html
          scope = context.fetch(:scope) { 'highlight' }
          node['class'] = "#{scope} #{scope}-#{lang}"
        end
        doc
      end

      def highlight_with_timeout_handling(text, lang)
        Rouge.highlight(text, lang, @formatter)
      rescue Timeout::Error => _
        nil
      end

      def lexer_for(lang)
        Rouge::Lexer.find(lang)<commit_msg>Improve documents of SyntaxHighlightFilter<commit_after># frozen_string_literal: true

HTML::Pipeline.require_dependency('rouge', 'SyntaxHighlightFilter')

module HTML
  class Pipeline
    # HTML Filter that syntax highlights text inside code blocks.
    #
    # Context options:
    #
    #   :highlight => String represents the language to pick lexer. Defaults to empty string.
    #   :scope => String represents the class attribute adds to pre element after.
    #             Defaults to "highlight highlight-css" if highlights a css code block.
    #
    # This filter does not write any additional information to the context hash.
    class SyntaxHighlightFilter < Filter
      def initialize(*args)
        super(*args)
        @formatter = Rouge::Formatters::HTML.new
      end

      def call
        doc.search('pre').each do |node|
          default = context[:highlight] && context[:highlight].to_s
          next unless lang = node['lang'] || default
          next unless lexer = lexer_for(lang)
          text = node.inner_text

          html = highlight_with_timeout_handling(text, lang)
          next if html.nil?

          node.inner_html = html
          scope = context.fetch(:scope) { 'highlight' }
          node['class'] = "#{scope} #{scope}-#{lang}"
        end
        doc
      end

      def highlight_with_timeout_handling(text, lang)
        Rouge.highlight(text, lang, @formatter)
      rescue Timeout::Error => _
        nil
      end

      def lexer_for(lang)
        Rouge::Lexer.find(lang)
{'fraction_non_alphanumeric': 0.09842223891810668, 'fraction_numerical': 0.0, 'mean_word_length': 2.445019404915912, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '4032609', 'n_tokens_mistral': 758, 'n_tokens_neox': 716, 'n_words': 203}