<commit_before>module Banzai
  module Filter
    # Issues, Merge Requests, Snippets, Commits and Commit Ranges share
    # similar functionality in reference filtering.
    class AbstractReferenceFilter < ReferenceFilter
      include CrossProjectReference

      def self.object_class
        # Implement in child class
        # Example: MergeRequest
      end

      def self.object_name
        @object_name ||= object_class.name.underscore
      end

      def self.object_sym
        @object_sym ||= object_name.to_sym
      end

      # Public: Find references in text (like `!123` for merge requests)
      #
      #   AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
      #     object = find_object(project_ref, id)
      #     "<a href=...>#{object.to_reference}</a>"
      #   end
      #
      # text - String text to search.
      #
      # Yields the String match, the Integer referenced object ID, an optional String
      # of the external project reference, and all of the matchdata.
      #
      # Returns a String replaced with the return of the block.
      def self.references_in(text, pattern = object_class.reference_pattern)
        text.gsub(pattern) do |match|
          symbol = $~[object_sym]
          if object_class.reference_valid?(symbol)
            yield match, symbol.to_i, $~[:project], $~[:namespace], $~
          else
            match
          end
        end
      end

      def object_class
        self.class.object_class
      end

      def object_sym
        self.class.object_sym
      end

      def references_in(*args, &block)
        self.class.references_in(*args, &block)
      end

      # Implement in child class
      # Example: project.merge_requests.find
      def find_object(parent_object, id)
      end

      # Override if the link reference pattern produces a different ID (global
      # ID vs internal ID, for instance) to the regular reference pattern.
      def find_object_from_link(parent_object, id)
        find_object(parent_object, id)
      end

      # Implement in child class
      # Example: project_merge_request_url
      def url_for_object(object, parent_object)
      end

      def find_object_cached(parent_object, id)
        cached_call(:banzai_find_object, id, path: [object_class, parent_object.id]) do
          find_object(parent_object, id)
        end
      end

      def find_object_from_link_cached(parent_object, id)
        cached_call(:banzai_find_object_from_link, id, path: [object_class, parent_object.id]) do
          find_object_from_link(parent_object, id)
        end
      end

      def from_ref_cached(ref)
        cached_call("banzai_#{parent_type}_refs".to_sym, ref) do
          parent_from_ref(ref)
        end
      end

      def url_for_object_cached(object, parent_object)
        cached_call(:banzai_url_for_object, object, path: [object_class, parent_object.id]) do
          url_for_object(object, parent_object)
        end
      end

      def call
        return doc unless project || group

        ref_pattern = object_class.reference_pattern
        link_pattern = object_class.link_reference_pattern

        nodes.each do |node|
          if text_node?(node) && ref_pattern
            replace_text_when_pattern_matches(node, ref_pattern) do |content|
              object_link_filter(content, ref_pattern)
            end

          elsif element_node?(node)
            yield_valid_link(node) do |link, inner_html|
              if ref_pattern && link =~ /\A#{ref_pattern}\z/
                replace_link_node_with_href(node, link) do
                  object_link_filter(link, ref_pattern, link_content: inner_html)
                end

                next
              end

              next unless link_pattern

              if link == inner_html && inner_html =~ /\A#{link_pattern}/
                replace_link_node_with_text(node, link) do
                  object_link_filter(inner_html, link_pattern, link_reference: true)
                end

                next
              end

              if link =~ /\A#{link_pattern}\z/<commit_msg>Improve render performance of large wiki pages<commit_after>module Banzai
  module Filter
    # Issues, Merge Requests, Snippets, Commits and Commit Ranges share
    # similar functionality in reference filtering.
    class AbstractReferenceFilter < ReferenceFilter
      include CrossProjectReference

      def self.object_class
        # Implement in child class
        # Example: MergeRequest
      end

      def self.object_name
        @object_name ||= object_class.name.underscore
      end

      def self.object_sym
        @object_sym ||= object_name.to_sym
      end

      # Public: Find references in text (like `!123` for merge requests)
      #
      #   AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
      #     object = find_object(project_ref, id)
      #     "<a href=...>#{object.to_reference}</a>"
      #   end
      #
      # text - String text to search.
      #
      # Yields the String match, the Integer referenced object ID, an optional String
      # of the external project reference, and all of the matchdata.
      #
      # Returns a String replaced with the return of the block.
      def self.references_in(text, pattern = object_class.reference_pattern)
        text.gsub(pattern) do |match|
          symbol = $~[object_sym]
          if object_class.reference_valid?(symbol)
            yield match, symbol.to_i, $~[:project], $~[:namespace], $~
          else
            match
          end
        end
      end

      def object_class
        self.class.object_class
      end

      def object_sym
        self.class.object_sym
      end

      def references_in(*args, &block)
        self.class.references_in(*args, &block)
      end

      # Implement in child class
      # Example: project.merge_requests.find
      def find_object(parent_object, id)
      end

      # Override if the link reference pattern produces a different ID (global
      # ID vs internal ID, for instance) to the regular reference pattern.
      def find_object_from_link(parent_object, id)
        find_object(parent_object, id)
      end

      # Implement in child class
      # Example: project_merge_request_url
      def url_for_object(object, parent_object)
      end

      def find_object_cached(parent_object, id)
        cached_call(:banzai_find_object, id, path: [object_class, parent_object.id]) do
          find_object(parent_object, id)
        end
      end

      def find_object_from_link_cached(parent_object, id)
        cached_call(:banzai_find_object_from_link, id, path: [object_class, parent_object.id]) do
          find_object_from_link(parent_object, id)
        end
      end

      def from_ref_cached(ref)
        cached_call("banzai_#{parent_type}_refs".to_sym, ref) do
          parent_from_ref(ref)
        end
      end

      def url_for_object_cached(object, parent_object)
        cached_call(:banzai_url_for_object, object, path: [object_class, parent_object.id]) do
          url_for_object(object, parent_object)
        end
      end

      def call
        return doc unless project || group

        ref_pattern = object_class.reference_pattern
        link_pattern = object_class.link_reference_pattern

        # Compile often used regexps only once outside of the loop
        ref_pattern_anchor = /\A#{ref_pattern}\z/
        link_pattern_start = /\A#{link_pattern}/
        link_pattern_anchor = /\A#{link_pattern}\z/

        nodes.each do |node|
          if text_node?(node) && ref_pattern
            replace_text_when_pattern_matches(node, ref_pattern) do |content|
              object_link_filter(content, ref_pattern)
            end

          elsif element_node?(node)
            yield_valid_link(node) do |link, inner_html|
              if ref_pattern && link =~ ref_pattern_anchor
                replace_link_node_with_href(node, link) do
                  object_link_filter(link, ref_pattern, link_content: inner_html)
                end

                next
              end

              next unless link_pattern

              if link == inner_html && inner_html =~ link_pattern_start
                replace_link_node_with_text(node, link) do
                  object_link_filter(inner_html, link_pattern, link_reference: true)
                end

                next
              end

              if link =~ link_pattern_anchor
{'fraction_non_alphanumeric': 0.07074637424831977, 'fraction_numerical': 0.0007074637424831977, 'mean_word_length': 2.2031722054380665, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1076627', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2369, 'n_words': 613}