<commit_before># Various useful functions for Middleman-based sites
class SiteHelpers < Middleman::Extension
  def initialize(app, options_hash = {}, &block)
    super
  end

  helpers do
    require 'open-uri/cached'

    def normalize_url(dirty_URL)
      r = url_for Middleman::Util.normalize_path(dirty_URL)
      r.sub(/\/$/, '')
    end

    def pretty_date(sometime, length = 'long')
      return unless sometime

      sometime = Time.parse(sometime) if sometime.class == String

      format = length == 'short' ? '%a %e %b' : '%A %e %B'
      format << ' %Y' unless sometime.year == Time.now.year

      sometime.to_time.strftime(format) rescue ''
    end

    def word_unwrap(content)
      content.to_s
        .gsub(/\n\n/, '!ಠ_ಠ!')
        .gsub(/\n/, ' ')
        .squeeze(' ')
        .gsub(/!ಠ_ಠ!/, "\n\n")
    end

    def html_to_plaintext(content)
      Nokogiri::HTML(content).text.strip
    end

    def markdown_to_html(content)
      content.strip!
      Tilt['markdown'].new(config[:markdown]) { content }.render if content
    end

    def markdown_to_plaintext(content)
      word_unwrap html_to_plaintext(markdown_to_html(content))
    end

    # Use the title from frontmatter metadata,
    # or peek into the page to find the H1,
    # or fallback to a filename-based-title
    def discover_title(page = current_page)
      return page.data.title unless page.data.title.nil?

      page.render(layout: false).match(/<h[1-2][^>]*>(.*?)<\/h[1-2]>/m) do |m|
        m ? html_to_plaintext(m[1].strip) : page.url.split(/\//).last.titleize
      end
    end

    def demote_headings(content)
      h_add = case content
              when /<h1>/
                2
              when /<h2>/
                1
              end

      if h_add
        content.to_str.gsub(/<(\/?)h([1-6])>/) do |_m|<commit_msg>be sure to convert to string before strip (to prevent nil strip)<commit_after># Various useful functions for Middleman-based sites
class SiteHelpers < Middleman::Extension
  def initialize(app, options_hash = {}, &block)
    super
  end

  helpers do
    require 'open-uri/cached'

    def normalize_url(dirty_URL)
      r = url_for Middleman::Util.normalize_path(dirty_URL)
      r.sub(/\/$/, '')
    end

    def pretty_date(sometime, length = 'long')
      return unless sometime

      sometime = Time.parse(sometime) if sometime.class == String

      format = length == 'short' ? '%a %e %b' : '%A %e %B'
      format << ' %Y' unless sometime.year == Time.now.year

      sometime.to_time.strftime(format) rescue ''
    end

    def word_unwrap(content)
      content.to_s
        .gsub(/\n\n/, '!ಠ_ಠ!')
        .gsub(/\n/, ' ')
        .squeeze(' ')
        .gsub(/!ಠ_ಠ!/, "\n\n")
    end

    def html_to_plaintext(content)
      Nokogiri::HTML(content).text.strip
    end

    def markdown_to_html(content)
      content.to_s.strip!
      Tilt['markdown'].new(config[:markdown]) { content }.render if content
    end

    def markdown_to_plaintext(content)
      word_unwrap html_to_plaintext(markdown_to_html(content))
    end

    # Use the title from frontmatter metadata,
    # or peek into the page to find the H1,
    # or fallback to a filename-based-title
    def discover_title(page = current_page)
      return page.data.title unless page.data.title.nil?

      page.render(layout: false).match(/<h[1-2][^>]*>(.*?)<\/h[1-2]>/m) do |m|
        m ? html_to_plaintext(m[1].strip) : page.url.split(/\//).last.titleize
      end
    end

    def demote_headings(content)
      h_add = case content
              when /<h1>/
                2
              when /<h2>/
                1
              end

      if h_add
        content.to_str.gsub(/<(\/?)h([1-6])>/) do |_m|
{'fraction_non_alphanumeric': 0.13743948359332975, 'fraction_numerical': 0.006455083378160301, 'mean_word_length': 2.2824360105913506, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534465', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1255, 'n_words': 284}