<commit_before>module Govspeak
  class AdminLinkReplacer
    def initialize(nokogiri_fragment)
      @nokogiri_fragment = nokogiri_fragment
    end

    def replace!(&block)
      @nokogiri_fragment.search('a').each do |anchor|
        next unless DataHygiene::GovspeakLinkValidator.is_internal_admin_link?(anchor['href'])

        replacement_html = replacement_html_for_admin_link(anchor, &block)
        anchor.replace Nokogiri::HTML.fragment(replacement_html)
      end
    end

    def replacement_html_for_admin_link(anchor, &block)
      path = anchor['href']
      edition_path_pattern = Whitehall.edition_route_path_segments.join('|')
      if path[%r{/admin/editions/(\d+)/supporting-pages/([\w-]+)$}]
        policy = Policy.unscoped.find_by_id($1)
        supporting_page = EditionedSupportingPageMapping.find_by_old_supporting_page_id($2).try(:new_supporting_page)
        replacement_html_for_edition_link(anchor, supporting_page, policy_id: policy.document, &block)
      elsif path[%r{/admin/organisations/([\w-]+)/corporate_information_pages/(\d+)$}]
        organisation = Organisation.find_by_slug($1)
        corporate_info_page = organisation.corporate_information_pages.find($2)
        replacement_html_for_edition_link(anchor, corporate_info_page, &block)
      elsif path[%r{/admin/worldwide_organisations/([\w-]+)/corporate_information_pages/(\d+)$}]
        organisation = WorldwideOrganisation.find_by_slug($1)
        corporate_info_page = organisation.corporate_information_pages.find($2)
        replacement_html_for_edition_link(anchor, corporate_info_page, &block)
      elsif path[%r{/admin/(?:#{edition_path_pattern})/(\d+)$}]
        edition = Edition.unscoped.find_by_id($1)
        replacement_html_for_edition_link(anchor, edition, &block)
      else
        replacement_html_for_bad_link(anchor, &block)
      end
    end

    private

    def replacement_html_for_edition_link(anchor, edition, options = {})
      new_html = if edition.present? && edition.linkable?
        anchor.dup.tap do |anchor|
          anchor['href'] = Whitehall.url_maker.public_document_url(edition, options)
        end.to_html.html_safe
      else
        anchor.inner_text
      end

      block_given? ? yield(new_html, edition) : new_html
    end

    def replacement_html_for_bad_link(anchor)
      block_given? ? yield(anchor.inner_text, nil) : anchor.inner_text
    end
  end
end
<commit_msg>Refactor Govspeak::AdminLinkReplacer for clarity<commit_after>module Govspeak
  class AdminLinkReplacer
    def initialize(nokogiri_fragment)
      @nokogiri_fragment = nokogiri_fragment
    end

    def replace!(&block)
      @nokogiri_fragment.search('a').each do |anchor|
        next unless DataHygiene::GovspeakLinkValidator.is_internal_admin_link?(anchor['href'])

        replacement_html = replacement_html_for_admin_link(anchor, &block)
        anchor.replace Nokogiri::HTML.fragment(replacement_html)
      end
    end

    def replacement_html_for_admin_link(anchor, &block)
      case anchor['href']
      when %r{/admin/editions/(\d+)/supporting-pages/([\w-]+)$}
        convert_link_for_old_supporting_page(anchor, $1, $2, &block)
      when %r{/admin/organisations/([\w-]+)/corporate_information_pages/(\d+)$}
        convert_link_for_corporate_information_page(anchor, $1, $2, &block)
      when %r{/admin/worldwide_organisations/([\w-]+)/corporate_information_pages/(\d+)$}
        convert_link_for_worldwide_corporate_information_page(anchor, $1, $2, &block)
      when %r{/admin/(?:#{edition_path_pattern})/(\d+)$}
        edition = Edition.unscoped.find_by_id($1)

        convert_link_for_edition(anchor, edition, &block)
      else
        replace_bad_link(anchor, &block)
      end
    end

    private

    def edition_path_pattern
      Whitehall.edition_route_path_segments.join('|')
    end

    def convert_link_for_old_supporting_page(anchor, policy_slug, slug, &block)
      policy = Policy.unscoped.find_by_id(policy_slug)
      supporting_page = EditionedSupportingPageMapping.find_by_old_supporting_page_id(slug).try(:new_supporting_page)

      convert_link_for_edition(anchor, supporting_page, policy_id: policy.document, &block)
    end

    def convert_link_for_corporate_information_page(anchor, organisation_slug, slug, &block)
      organisation = Organisation.find_by_slug(organisation_slug)
      corporate_info_page = organisation.corporate_information_pages.find(slug)

      convert_link_for_edition(anchor, corporate_info_page, &block)
    end

    def convert_link_for_worldwide_corporate_information_page(anchor, world_org_slug, slug, &block)
      organisation = WorldwideOrganisation.find_by_slug(world_org_slug)
      corporate_info_page = organisation.corporate_information_pages.find(slug)

      convert_link_for_edition(anchor, corporate_info_page, &block)
    end

    def convert_link_for_edition(anchor, edition, options = {})
      new_html = if edition.present? && edition.linkable?
        anchor.dup.tap do |anchor|
          anchor['href'] = Whitehall.url_maker.public_document_url(edition, options)
        end.to_html.html_safe
      else
        anchor.inner_text
      end

      block_given? ? yield(new_html, edition) : new_html
    end

    def replace_bad_link(anchor)
      block_given? ? yield(anchor.inner_text, nil) : anchor.inner_text
    end
  end
end

{'fraction_non_alphanumeric': 0.10323545913596409, 'fraction_numerical': 0.002618290630259959, 'mean_word_length': 4.0452830188679245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7468574', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1739, 'n_words': 245}