<commit_before>module GovspeakHelper

  def govspeak_to_admin_html(text)
    doc = markup_to_nokogiri_doc(text)

    replace_internal_admin_links_in(doc) do |replacement_html, document|
      latest_edition = document && document.document_identity.latest_edition
      if latest_edition.nil?
        replacement_html = "<del>#{replacement_html}</del>"
        explanation = state = "deleted"
      else
        state = latest_edition.state
        explanation = %{<a href="#{admin_document_path(latest_edition)}">#{state}</a>}
      end

      %{<span class="#{state}_link">#{replacement_html} <sup class="explanation">(#{explanation})</sup></span>}
    end

    doc.to_html.html_safe
  end

  def govspeak_to_html(text)
    doc = markup_to_nokogiri_doc(text)
    replace_internal_admin_links_in doc
    doc.to_html.html_safe
  end

  def govspeak_headers(text, level = 2)
    Govspeak::Document.new(text).headers.each do |header|
      header.level == level
    end
  end

  private

  def replace_internal_admin_links_in(nokogiri_doc)
    nokogiri_doc.search('a').each do |anchor|
      next unless is_internal_admin_link?(uri = anchor['href'])

      document, supporting_page = find_documents_from_uri(uri)
      replacement_html = replacement_html_for(anchor, document, supporting_page)
      replacement_html = yield(replacement_html, document) if block_given?

      anchor.replace Nokogiri::HTML.fragment(replacement_html)
    end
  end

  def replacement_html_for(anchor, document, supporting_page)
    if document.present? && document.linkable?
      anchor.dup.tap do |anchor|
        anchor['href'] = rewritten_href_for_documents(document, supporting_page)
      end.to_html
    else
      anchor.inner_text
    end
  end

  def markup_to_nokogiri_doc(text)
    govspeak = Govspeak::Document.to_html(text)
    html = '<div class="govspeak">' + govspeak + '</div>'
    doc = Nokogiri::HTML::Document.new
    doc.encoding = "UTF-8"
    doc.fragment(html)<commit_msg>Extract markup_to_html_with_replaced_admin_links from govspeak_to_admin_html and govspeak_to_html helpers<commit_after>module GovspeakHelper

  def govspeak_to_admin_html(text)
    markup_to_html_with_replaced_admin_links(text) do |replacement_html, document|
      latest_edition = document && document.document_identity.latest_edition
      if latest_edition.nil?
        replacement_html = "<del>#{replacement_html}</del>"
        explanation = state = "deleted"
      else
        state = latest_edition.state
        explanation = %{<a href="#{admin_document_path(latest_edition)}">#{state}</a>}
      end

      %{<span class="#{state}_link">#{replacement_html} <sup class="explanation">(#{explanation})</sup></span>}
    end
  end

  def govspeak_to_html(text)
    markup_to_html_with_replaced_admin_links(text)
  end

  def govspeak_headers(text, level = 2)
    Govspeak::Document.new(text).headers.each do |header|
      header.level == level
    end
  end

  private

  def markup_to_html_with_replaced_admin_links(text, &block)
    markup_to_nokogiri_doc(text).tap do |nokogiri_doc|
      replace_internal_admin_links_in nokogiri_doc, &block
    end.to_html.html_safe
  end

  def replace_internal_admin_links_in(nokogiri_doc)
    nokogiri_doc.search('a').each do |anchor|
      next unless is_internal_admin_link?(uri = anchor['href'])

      document, supporting_page = find_documents_from_uri(uri)
      replacement_html = replacement_html_for(anchor, document, supporting_page)
      replacement_html = yield(replacement_html, document) if block_given?

      anchor.replace Nokogiri::HTML.fragment(replacement_html)
    end
  end

  def replacement_html_for(anchor, document, supporting_page)
    if document.present? && document.linkable?
      anchor.dup.tap do |anchor|
        anchor['href'] = rewritten_href_for_documents(document, supporting_page)
      end.to_html
    else
      anchor.inner_text
    end
  end

  def markup_to_nokogiri_doc(text)
    govspeak = Govspeak::Document.to_html(text)
    html = '<div class="govspeak">' + govspeak + '</div>'
    doc = Nokogiri::HTML::Document.new
    doc.encoding = "UTF-8"
    doc.fragment(html)
{'fraction_non_alphanumeric': 0.10311518956773726, 'fraction_numerical': 0.0009659502535619416, 'mean_word_length': 4.10727496917386, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '7461730', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1385, 'n_words': 231}