Sha256: 1195cca8a7532a943b3a06cf4916a703324dee9dfe990d3562a77ecb04f12220

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

require "jekyll/redirect_from/version"
require "jekyll/redirect_from/redirect_page"

module Jekyll
  class RedirectFrom < Generator
    def generate(site)
      original_pages = site.pages.dup
      generate_alt_urls(site, site.posts)
      generate_alt_urls(site, original_pages)
    end

    def generate_alt_urls(site, list)
      list.each do |item|
        if has_alt_urls?(item)
          alt_urls(item).flatten.each do |alt_url|
            redirect_page = RedirectPage.new(site, site.source, File.dirname(alt_url), File.basename(alt_url))
            redirect_page.generate_redirect_content(item.url)
            site.pages << redirect_page
          end
        end
      end
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-redirect-from-0.1.0 lib/jekyll/redirect_from.rb