<commit_before>module JekyllRedirectFrom
  class Redirector < Jekyll::Generator
    safe true

    def generate(site)
      original_pages = site.pages.dup
      generate_alt_urls(site, site.posts)
      generate_alt_urls(site, original_pages)
      generate_alt_urls(site, site.docs_to_write)
    end

    def generate_alt_urls(site, list)
      list.each do |item|
        if has_alt_urls?(item)
          alt_urls(item).each do |alt_url|
            redirect_page = RedirectPage.new(site, site.source, "", "")
            redirect_page.data['permalink'] = alt_url
            redirect_page.generate_redirect_content(redirect_url(site, item))
            site.pages << redirect_page
          end
        end
        if has_redirect_to_url?(item)
          redirect_to_url(item).flatten.each do |alt_url|
            redirect_page = RedirectPage.new(site, site.source, File.dirname(item.url), File.basename(item.url))
            redirect_page.data['permalink'] = item.url
            redirect_page.generate_redirect_content(alt_url)
            if item.is_a?(Jekyll::Document)
              item.content = item.output = redirect_page.content
            else
              site.pages << redirect_page
            end
          end
        end
      end
    end

    def is_dynamic_document?(page_or_post)
      page_or_post.is_a?(Jekyll::Post) ||
        page_or_post.is_a?(Jekyll::Page) ||
        page_or_post.is_a?(Jekyll::Document)
    end

    def has_alt_urls?(page_or_post)
      is_dynamic_document?(page_or_post) &&
        page_or_post.data.has_key?('redirect_from') &&
        !alt_urls(page_or_post).empty?
    end

    def alt_urls(page_or_post)
      Array[page_or_post.data['redirect_from']].flatten.compact
    end

    def has_redirect_to_url?(page_or_post)
      page_or_post.data.has_key?('redirect_to') && !redirect_to_url(page_or_post).empty?
    end

    def redirect_to_url(page_or_post)
      [Array[page_or_post.data['redirect_to']].flatten.first].compact<commit_msg>Also guard redirect_to url lookup against non-dynamic docs.<commit_after>module JekyllRedirectFrom
  class Redirector < Jekyll::Generator
    safe true

    def generate(site)
      original_pages = site.pages.dup
      generate_alt_urls(site, site.posts)
      generate_alt_urls(site, original_pages)
      generate_alt_urls(site, site.docs_to_write)
    end

    def generate_alt_urls(site, list)
      list.each do |item|
        if has_alt_urls?(item)
          alt_urls(item).each do |alt_url|
            redirect_page = RedirectPage.new(site, site.source, "", "")
            redirect_page.data['permalink'] = alt_url
            redirect_page.generate_redirect_content(redirect_url(site, item))
            site.pages << redirect_page
          end
        end
        if has_redirect_to_url?(item)
          redirect_to_url(item).flatten.each do |alt_url|
            redirect_page = RedirectPage.new(site, site.source, File.dirname(item.url), File.basename(item.url))
            redirect_page.data['permalink'] = item.url
            redirect_page.generate_redirect_content(alt_url)
            if item.is_a?(Jekyll::Document)
              item.content = item.output = redirect_page.content
            else
              site.pages << redirect_page
            end
          end
        end
      end
    end

    def is_dynamic_document?(page_or_post)
      page_or_post.is_a?(Jekyll::Post) ||
        page_or_post.is_a?(Jekyll::Page) ||
        page_or_post.is_a?(Jekyll::Document)
    end

    def has_alt_urls?(page_or_post)
      is_dynamic_document?(page_or_post) &&
        page_or_post.data.has_key?('redirect_from') &&
        !alt_urls(page_or_post).empty?
    end

    def alt_urls(page_or_post)
      Array[page_or_post.data['redirect_from']].flatten.compact
    end

    def has_redirect_to_url?(page_or_post)
      is_dynamic_document?(page_or_post) &&
        page_or_post.data.has_key?('redirect_to') &&
        !redirect_to_url(page_or_post).empty?
    end

    def redirect_to_url(page_or_post)
      [Array[page_or_post.data['redirect_to']].flatten.first].compact
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.0, 'mean_word_length': 2.7463369963369964, 'pattern_counts': {'":': 0, '<': 13, '<?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': '3816940', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1357, 'n_words': 187}