Skip to content

Commit f449475

Browse files
authored
Merge pull request #131 from jekyll/bb-refactor
Merge pull request 131
2 parents ba2efbd + 2c1e818 commit f449475

30 files changed

+687
-444
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
--color
22
--format progress
3+
--require spec_helper
4+
--order random

.rubocop.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/**/*

.travis.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ matrix:
99
- # GitHub Pages
1010
rvm: 2.1.1
1111
env: GH_PAGES=true
12-
- # Ruby 1.9
13-
rvm: 1.9
14-
env: JEKYLL_VERSION=2.0
1512
allow_failures:
1613
- env: GH_PAGES=true # Jekyll 2.4 will fail tests
1714
fast_finish: true
1815

1916
rvm:
17+
- 2.3.0
2018
- 2.2
2119
- 2.1
22-
- 2.0
23-
env:
24-
- ""
25-
- JEKYLL_VERSION=3.0
26-
- JEKYLL_VERSION=2.0

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ gemspec
33

44
if ENV["GH_PAGES"]
55
gem "github-pages"
6-
elsif ENV["JEKYLL_VERSION"]
7-
gem "jekyll", "~> #{ENV["JEKYLL_VERSION"]}"
86
end

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ If you have multiple `redirect_to`s set, only the first one will be respected.
120120

121121
**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.
122122

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+
123132
## Contributing
124133

125134
1. Fork it

jekyll-redirect-from.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_runtime_dependency "jekyll", ">= 2.0"
21+
spec.add_runtime_dependency "jekyll", "~> 3.3"
2222

2323
spec.add_development_dependency "bundler", "~> 1.3"
2424
spec.add_development_dependency "rake"
2525
spec.add_development_dependency "rspec"
2626
spec.add_development_dependency "jekyll-sitemap", "~> 0.8.1"
27+
spec.add_development_dependency "rubocop", "~> 0.43"
2728
end

lib/jekyll-redirect-from.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
require "jekyll"
2+
require "jekyll-redirect-from/version"
3+
require "jekyll-redirect-from/generator"
24

35
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"
713
end
814

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
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/jekyll-redirect-from/layout.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)