jekyll-redirect-from 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d791a13d885f60395b4ae51369248e46e4b049b
4
- data.tar.gz: d4bb94fb82a1dca865b49d404ec035ce037b3e65
3
+ metadata.gz: 7e24a249a812fdc138185c2969c99551fe3381e4
4
+ data.tar.gz: cae3b55835fb5559a4ba03fd2c9a1daf1e7c3042
5
5
  SHA512:
6
- metadata.gz: d918ab960428cd3d81738dddf6c8b66a68560ea686db49d29c775d0c722ea5ea3cd5d4722e1ffa67f7ad6eaf6ab802a91332a9a30a5bd3c16a316d2afd82cc39
7
- data.tar.gz: a9c2dd79f38008c59b9fca3314bea0a4d3c1b1a51bb218e60e4837bcf75f33ba7fcc3be67dd55a6aa4e2011c9f6c9eaf6074503e4318764485a75cded95e19c0
6
+ metadata.gz: 495dde833d74a819dd8b46f5e0b003813bfac7458cf31697ba048cce13c715236f0792fc8e7d53c20d1dd47b48c026f64b407e24649bcf0c2d0dc02e184c1d54
7
+ data.tar.gz: 4dc27cedf73031652945823ee9aa75bce46a2a9df78f9aa49ad37cc05d4e7ce18c7273bf333a9121bd1f9c19bb8ec42dcd186df26aa76deb2ad08ee1b58e5549
data/.rspec CHANGED
@@ -1,2 +1,4 @@
1
1
  --color
2
2
  --format progress
3
+ --require spec_helper
4
+ --order random
@@ -0,0 +1,14 @@
1
+ inherit_gem:
2
+ jekyll: .rubocop.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - vendor/**/*
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - spec/**/*
11
+
12
+ Metrics/LineLength:
13
+ Exclude:
14
+ - spec/**/*
@@ -9,18 +9,11 @@ matrix:
9
9
  - # GitHub Pages
10
10
  rvm: 2.1.1
11
11
  env: GH_PAGES=true
12
- - # Ruby 1.9
13
- rvm: 1.9
14
- env: JEKYLL_VERSION=2.0
15
12
  allow_failures:
16
13
  - env: GH_PAGES=true # Jekyll 2.4 will fail tests
17
14
  fast_finish: true
18
15
 
19
16
  rvm:
17
+ - 2.3.0
20
18
  - 2.2
21
19
  - 2.1
22
- - 2.0
23
- env:
24
- - ""
25
- - JEKYLL_VERSION=3.0
26
- - JEKYLL_VERSION=2.0
data/Gemfile CHANGED
@@ -3,6 +3,4 @@ gemspec
3
3
 
4
4
  if ENV["GH_PAGES"]
5
5
  gem "github-pages"
6
- elsif ENV["JEKYLL_VERSION"]
7
- gem "jekyll", "~> #{ENV["JEKYLL_VERSION"]}"
8
6
  end
@@ -1,3 +1,23 @@
1
+ ## HEAD
2
+
3
+ ### Major Enhancements
4
+
5
+ * Support for custom redirect templates
6
+ * Use Jekyll's `absolute_url` filter to generate canonical URLs (now respecting `baseurl`)
7
+ * Rely more heavily on Jekyll's native Page, permalink, and extension handling logic
8
+
9
+ ### Minor Enhancementse
10
+
11
+ * redirect_to Pages should not have a layout. (#115)
12
+ * Require Jekyll >= 3.3
13
+
14
+ ### Development Enhancements
15
+
16
+ * Push redirect logic to the redirect page model (#131)
17
+ * Add Rubocop and enforce Jekyll coding standards
18
+ * Tests no longer build and write the entire site between each example
19
+ * Removed all the `is_*`? and `has_*`? helpers from the generator
20
+
1
21
  ## 0.11.0 / 2016-07-06
2
22
 
3
23
  * Redirect page should not have any layout (#106)
data/README.md CHANGED
@@ -100,10 +100,10 @@ redirect_from: /post/123456798/
100
100
  ```
101
101
 
102
102
  ### Prefix
103
- If `site.baseurl` is set, its value is used as a prefix for the redirect url automatically.
104
- This is useful for scenarios where a site isn't available from the domain root, so the redirects point to the correct path.
105
103
 
106
- **_Note_**: If you are hosting your Jekyll site on [GitHub Pages](https://pages.github.com/), the prefix is set to the pages domain name i.e. `http://example.github.io/project` or a custom `CNAME`.
104
+ If `site.url` is set, its value, together with `site.baseurl`, is used as a prefix for the redirect url automatically. This is useful for scenarios where a site isn't available from the domain root, so the redirects point to the correct path. If `site.url` is not set, only `site.baseurl` is used, if set.
105
+
106
+ **_Note_**: If you are hosting your Jekyll site on [GitHub Pages](https://pages.github.com/), and `site.url` is not set, the prefix is set to the pages domain name i.e. http://example.github.io/project or a custom CNAME.
107
107
 
108
108
  ### Redirect To
109
109
 
@@ -120,6 +120,15 @@ If you have multiple `redirect_to`s set, only the first one will be respected.
120
120
 
121
121
  **Note**: Using `redirect_to` or `redirect_from` with collections will only work with files which are output to HTML, such as `.md`, `.textile`, `.html` etc.
122
122
 
123
+ ## Customizing the redirect template
124
+
125
+ If you want to customize the redirect template, you can. Simply create a layout in your site's `_layouts` directory called `redirect.html`.
126
+
127
+ Your layout will get the following variables:
128
+
129
+ * `page.redirect.from` - the relative path to the redirect page
130
+ * `page.redirect.to` - the absolute URL (where available) to the target page
131
+
123
132
  ## Contributing
124
133
 
125
134
  1. Fork it
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "jekyll", ">= 2.0"
21
+ spec.add_runtime_dependency "jekyll", "~> 3.3"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
26
  spec.add_development_dependency "jekyll-sitemap", "~> 0.8.1"
27
+ spec.add_development_dependency "rubocop", "~> 0.43"
27
28
  end
@@ -1,11 +1,17 @@
1
1
  require "jekyll"
2
+ require "jekyll-redirect-from/version"
3
+ require "jekyll-redirect-from/generator"
2
4
 
3
5
  module JekyllRedirectFrom
4
- def self.jekyll_3?
5
- @jekyll_3 ||= (Jekyll::VERSION >= '3.0.0')
6
- end
6
+ # Jekyll classes which should be redirectable
7
+ CLASSES = [Jekyll::Page, Jekyll::Document].freeze
8
+
9
+ autoload :Context, "jekyll-redirect-from/context"
10
+ autoload :RedirectPage, "jekyll-redirect-from/redirect_page"
11
+ autoload :Redirectable, "jekyll-redirect-from/redirectable"
12
+ autoload :Layout, "jekyll-redirect-from/layout"
7
13
  end
8
14
 
9
- require "jekyll-redirect-from/version"
10
- require "jekyll-redirect-from/redirect_page"
11
- require "jekyll-redirect-from/redirector"
15
+ JekyllRedirectFrom::CLASSES.each do |klass|
16
+ klass.include JekyllRedirectFrom::Redirectable
17
+ end
@@ -0,0 +1,14 @@
1
+ module JekyllRedirectFrom
2
+ # Stubbed LiquidContext to support relative_url and absolute_url helpers
3
+ class Context
4
+ attr_reader :site
5
+
6
+ def initialize(site)
7
+ @site = site
8
+ end
9
+
10
+ def registers
11
+ { :site => site }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module JekyllRedirectFrom
2
+ class Generator < Jekyll::Generator
3
+ safe true
4
+ attr_reader :site
5
+
6
+ def generate(site)
7
+ @site = site
8
+
9
+ # Inject our layout, unless the user has already specified a redirect layout'
10
+ unless site.layouts.keys.any? { |name| name == "redirect" }
11
+ site.layouts["redirect"] = JekyllRedirectFrom::Layout.new(site)
12
+ end
13
+
14
+ # Must duplicate pages to modify while in loop
15
+ (site.docs_to_write + site.pages.dup).each do |doc|
16
+ next unless JekyllRedirectFrom::CLASSES.include?(doc.class)
17
+ generate_redirect_from(doc)
18
+ generate_redirect_to(doc)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ # For every `redirect_from` entry, generate a redirect page
25
+ def generate_redirect_from(doc)
26
+ doc.redirect_from.each do |path|
27
+ doc.site.pages << RedirectPage.redirect_from(doc, path)
28
+ end
29
+ end
30
+
31
+ def generate_redirect_to(doc)
32
+ return unless doc.redirect_to
33
+ redirect_page = RedirectPage.redirect_to(doc, doc.redirect_to)
34
+ doc.data.merge!(redirect_page.data)
35
+ doc.content = doc.output = redirect_page.output
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module JekyllRedirectFrom
2
+ # A stubbed layout for our default redirect template
3
+ # We cannot use the standard Layout class because of site.in_source_dir
4
+ class Layout < Jekyll::Layout
5
+ def initialize(site)
6
+ @site = site
7
+ @base = File.dirname(__FILE__)
8
+ @name = "redirect.html"
9
+ @path = File.expand_path(@name, @base)
10
+ @relative_path = "_layouts/redirect.html"
11
+
12
+ self.data = {}
13
+ self.ext = "html"
14
+ self.content = File.read(@path)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en-US">
3
+ <meta charset="utf-8">
4
+ <title>Redirecting…</title>
5
+ <link rel="canonical" href="{{ page.redirect.to }}">
6
+ <meta http-equiv="refresh" content="0; url={{ page.redirect.to }}">
7
+ <h1>Redirecting…</h1>
8
+ <a href="{{ page.redirect.to }}">Click here if you are not redirected.</a>
9
+ <script>location="{{ page.redirect.to }}"</script>
10
+ </html>
@@ -1,42 +1,61 @@
1
1
  # Encoding: utf-8
2
-
3
2
  module JekyllRedirectFrom
3
+ # Specialty page which implements the redirect path logic
4
4
  class RedirectPage < Jekyll::Page
5
- # Initialize a new RedirectPage.
5
+ # Use Jekyll's native absolute_url filter
6
+ include Jekyll::Filters::URLFilters
7
+
8
+ DEFAULT_DATA = {
9
+ "sitemap" => false,
10
+ "layout" => "redirect"
11
+ }.freeze
12
+
13
+ # Creates a new RedirectPage instance from a source path and redirect path
6
14
  #
7
- # site - The Site object.
8
- # base - The String path to the source.
9
- # dir - The String path between the source and the file.
10
- # name - The String filename of the file.
11
- def initialize(site, base, dir, name)
12
- @site = site
13
- @base = base
14
- @dir = dir
15
- @name = name
16
-
17
- self.process(name)
18
- self.data = { "layout" => nil }
19
-
20
- data.default_proc = proc do |_, key|
21
- site.frontmatter_defaults.find(File.join(dir, name), type, key)
22
- end
23
-
24
- Jekyll::Hooks.trigger :pages, :post_init, self if JekyllRedirectFrom.jekyll_3?
15
+ # site - The Site object
16
+ # from - the (URL) path, relative to the site root to redirect from
17
+ # to - the relative path or URL which the page should redirect to
18
+ def self.from_paths(site, from, to)
19
+ page = RedirectPage.new(site, site.source, "", "redirect.html")
20
+ page.set_paths(from, to)
21
+ page
22
+ end
23
+
24
+ # Creates a new RedirectPage instance from the path to the given doc
25
+ def self.redirect_from(doc, path)
26
+ RedirectPage.from_paths(doc.site, path, doc.url)
27
+ end
28
+
29
+ # Creates a new RedirectPage instance from the doc to the given path
30
+ def self.redirect_to(doc, path)
31
+ RedirectPage.from_paths(doc.site, doc.url, path)
25
32
  end
26
33
 
27
- def generate_redirect_content(item_url)
28
- self.output = self.content = <<-EOF
29
- <!DOCTYPE html>
30
- <html lang="en-US">
31
- <meta charset="utf-8">
32
- <title>Redirecting…</title>
33
- <link rel="canonical" href="#{item_url}">
34
- <meta http-equiv="refresh" content="0; url=#{item_url}">
35
- <h1>Redirecting…</h1>
36
- <a href="#{item_url}">Click here if you are not redirected.</a>
37
- <script>location="#{item_url}"</script>
38
- </html>
39
- EOF
34
+ # Overwrite the default read_yaml method since the file doesn't exist
35
+ def read_yaml(_base, _name, _opts = {})
36
+ self.content = ""
37
+ self.data ||= DEFAULT_DATA.dup
38
+ end
39
+
40
+ # Helper function to set the appropriate path metadata
41
+ #
42
+ # from - the relative path to the redirect page
43
+ # to - the relative path or absolute URL to the redirect target
44
+ def set_paths(from, to)
45
+ @context ||= context
46
+ data.merge!({
47
+ "permalink" => from,
48
+ "redirect" => {
49
+ "from" => from,
50
+ "to" => to =~ %r!^https?://! ? to : absolute_url(to)
51
+ }
52
+ })
53
+ end
54
+
55
+ private
56
+
57
+ def context
58
+ JekyllRedirectFrom::Context.new(site)
40
59
  end
41
60
  end
42
61
  end
@@ -0,0 +1,25 @@
1
+ module JekyllRedirectFrom
2
+ # Module which can be mixed in to documents (and pages) to provide
3
+ # redirect_to and redirect_from helpers
4
+ module Redirectable
5
+ # Returns a string representing the relative path or URL
6
+ # to which the document should be redirected
7
+ def redirect_to
8
+ if to_liquid["redirect_to"].is_a?(Array)
9
+ to_liquid["redirect_to"].compact.first
10
+ else
11
+ to_liquid["redirect_to"]
12
+ end
13
+ end
14
+
15
+ # Returns an array representing the relative paths to other
16
+ # documents which should be redirected to this document
17
+ def redirect_from
18
+ if to_liquid["redirect_from"].is_a?(Array)
19
+ to_liquid["redirect_from"].compact
20
+ else
21
+ [to_liquid["redirect_from"]].compact
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module JekyllRedirectFrom
2
- VERSION = "0.11.0"
2
+ VERSION = "0.12.0".freeze
3
3
  end
@@ -2,3 +2,5 @@
2
2
  set -e
3
3
 
4
4
  bundle exec rake spec
5
+ bundle exec rubocop -S -D
6
+ gem build jekyll-redirect-from.gemspec
@@ -0,0 +1,6 @@
1
+ ---
2
+ title: I only have one redirect path
3
+ redirect_from: some/other/path
4
+ ---
5
+
6
+ One redirect url
@@ -0,0 +1,6 @@
1
+ ---
2
+ title: I am going somewhere external
3
+ redirect_to: /foo
4
+ ---
5
+
6
+ Redirecting elsewhere.
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: I am going somewhere else.
2
+ title: I am going somewhere external
3
3
  redirect_to: https://www.github.com
4
4
  ---
5
5
 
@@ -1,13 +1,86 @@
1
- require 'spec_helper'
1
+ RSpec.describe "JekyllRedirectFrom integration tests" do
2
+ before { site.process }
3
+ let(:relative_path) { "" }
4
+ let(:path) { dest_dir(relative_path) }
5
+ let(:contents) { File.read(path) }
2
6
 
3
- describe("Integration Tests") do
4
- it "writes the redirect pages for collection items which are outputted" do
5
- expect(dest_dir("articles", "redirect-me-plz.html")).to exist
6
- expect(dest_dir("articles", "23128432159832", "mary-had-a-little-lamb#{forced_output_ext}")).to exist
7
+ context "pages" do
8
+ context "single redirect from" do
9
+ let(:relative_path) { "some/other/path.html" }
10
+
11
+ it "exists in the built site" do
12
+ expect(path).to exist
13
+ expect(contents).to match("http://jekyllrb.com/one_redirect_from.html")
14
+ end
15
+ end
16
+
17
+ context "multiple redirect froms" do
18
+ %w(help contact let-there/be/light-he-said geepers/mccreepin).each do |redirect|
19
+ context "the #{redirect} redirect" do
20
+ let(:relative_path) { "#{redirect}.html" }
21
+
22
+ it "exists in the built site" do
23
+ expect(path).to exist
24
+ expect(contents).to match("http://jekyllrb.com/multiple_redirect_froms.html")
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ context "a redirect to URL" do
31
+ let(:relative_path) { "one_redirect_to_url.html" }
32
+
33
+ it "exists in the built site" do
34
+ expect(path).to exist
35
+ expect(contents).to match("https://www.github.com")
36
+ end
37
+ end
38
+
39
+ context "a redirect to path" do
40
+ let(:relative_path) { "one_redirect_to_path.html" }
41
+
42
+ it "exists in the built site" do
43
+ expect(path).to exist
44
+ expect(contents).to match("http://jekyllrb.com/foo")
45
+ end
46
+ end
7
47
  end
8
48
 
9
- it "doesn't write redirect pages for collection items which are not outputted" do
10
- expect(dest_dir("authors")).not_to exist
11
- expect(dest_dir("kansaichris")).not_to exist
49
+ context "documents" do
50
+ context "a single redirect from" do
51
+ let(:relative_path) { "articles/23128432159832/mary-had-a-little-lamb.html" }
52
+
53
+ it "exists in the built site" do
54
+ expect(path).to exist
55
+ expect(contents).to match("http://jekyllrb.com/articles/redirect-me-plz.html")
56
+ end
57
+ end
58
+
59
+ context "redirect to" do
60
+ let(:relative_path) { "articles/redirect-somewhere-else-plz.html" }
61
+
62
+ it "exists in the built site" do
63
+ expect(path).to exist
64
+ expect(contents).to match("http://www.zombo.com")
65
+ end
66
+ end
67
+
68
+ context "with a permalink" do
69
+ let(:relative_path) { "tags/our projects/index.html" }
70
+
71
+ it "exists in the built site" do
72
+ expect(path).to exist
73
+ expect(contents).to match("http://jekyllrb.com/tags/our-projects/")
74
+ end
75
+ end
76
+ end
77
+
78
+ context "sitemap" do
79
+ let(:relative_path) { "sitemap.xml" }
80
+
81
+ it "doesn't contain redirects" do
82
+ expect(contents).to_not be_nil
83
+ expect(contents).to_not match("redirect_to")
84
+ end
12
85
  end
13
86
  end