Class: JekyllRedirectFrom::RedirectPage

Inherits:
Jekyll::Page
  • Object
show all
Includes:
Jekyll::Filters::URLFilters
Defined in:
lib/jekyll-redirect-from/redirect_page.rb

Overview

Specialty page which implements the redirect path logic

Constant Summary collapse

DEFAULT_DATA =
{
  "sitemap" => false,
  "layout"  => "redirect",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_paths(site, from, to) ⇒ Object

Creates a new RedirectPage instance from a source path and redirect path

site - The Site object from - the (URL) path, relative to the site root to redirect from to - the relative path or URL which the page should redirect to



19
20
21
22
23
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 19

def self.from_paths(site, from, to)
  page = RedirectPage.new(site, site.source, "", "redirect.html")
  page.set_paths(from, to)
  page
end

.redirect_from(doc, path) ⇒ Object

Creates a new RedirectPage instance from the path to the given doc



26
27
28
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 26

def self.redirect_from(doc, path)
  RedirectPage.from_paths(doc.site, path, doc.url)
end

.redirect_to(doc, path) ⇒ Object

Creates a new RedirectPage instance from the doc to the given path



31
32
33
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 31

def self.redirect_to(doc, path)
  RedirectPage.from_paths(doc.site, doc.url, path)
end

Instance Method Details

#read_yaml(_base, _name, _opts = {}) ⇒ Object

Overwrite the default read_yaml method since the file doesn’t exist



36
37
38
39
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 36

def read_yaml(_base, _name, _opts = {})
  self.content = self.output = ""
  self.data ||= DEFAULT_DATA.dup
end

#redirect_fromObject



57
58
59
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 57

def redirect_from
  data["redirect"]["from"] if data["redirect"]
end

#redirect_toObject



61
62
63
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 61

def redirect_to
  data["redirect"]["to"] if data["redirect"]
end

#set_paths(from, to) ⇒ Object

Helper function to set the appropriate path metadata

from - the relative path to the redirect page to - the relative path or absolute URL to the redirect target



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jekyll-redirect-from/redirect_page.rb', line 45

def set_paths(from, to)
  @context ||= context
  from = ensure_leading_slash(from)
  data.merge!(
    "permalink" => from,
    "redirect"  => {
      "from" => from,
      "to"   => to =~ %r!^https?://! ? to : absolute_url(to),
    }
  )
end