Skip to content

Commit 8e0da42

Browse files
committed
Merge pull request #96 from jekyll/jekyll-3-drops
Merge pull request 96
2 parents a1eb49b + 54e6955 commit 8e0da42

File tree

8 files changed

+52
-40
lines changed

8 files changed

+52
-40
lines changed

lib/jekyll-redirect-from.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
require "jekyll"
2+
3+
module JekyllRedirectFrom
4+
def self.jekyll_3?
5+
@jekyll_3 ||= (Jekyll::VERSION >= '3.0.0')
6+
end
7+
end
8+
29
require "jekyll-redirect-from/version"
310
require "jekyll-redirect-from/redirect_page"
411
require "jekyll-redirect-from/redirector"

lib/jekyll-redirect-from/redirect_page.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def initialize(site, base, dir, name)
1616

1717
self.process(name)
1818
self.data = {}
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?
1925
end
2026

2127
def generate_redirect_content(item_url)

lib/jekyll-redirect-from/redirector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def generate_alt_urls(site, list)
1313
list.each do |item|
1414
if has_alt_urls?(item)
1515
alt_urls(item).each do |alt_url|
16-
redirect_page = RedirectPage.new(site, site.source, "", "")
16+
redirect_page = RedirectPage.new(site, site.source, "", "redirect.html")
1717
redirect_page.data['permalink'] = alt_url
1818
redirect_page.data['sitemap'] = false
1919
redirect_page.generate_redirect_content(redirect_url(site, item))

script/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
bundle exec rspec $@

spec/integrations_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
describe("Integration Tests") do
44
it "writes the redirect pages for collection items which are outputted" do
5-
expect(@dest.join("articles", "redirect-me-plz.html")).to exist
6-
expect(@dest.join("articles", "23128432159832", "mary-had-a-little-lamb")).to exist
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
77
end
88

99
it "doesn't write redirect pages for collection items which are not outputted" do
10-
expect(@dest.join("authors")).not_to exist
11-
expect(@dest.join("kansaichris")).not_to exist
10+
expect(dest_dir("authors")).not_to exist
11+
expect(dest_dir("kansaichris")).not_to exist
1212
end
1313
end

spec/jekyll_redirect_from/redirect_page_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@
3636
let(:redirect_page) { new_redirect_page(permalink_dir) }
3737

3838
it "knows to add the index.html if it's a folder" do
39-
dest = dest_dir("/posts/1914798137981389/larry-had-a-little-lamb/index.html")
40-
expect(redirect_page.destination("/")).to eql(dest)
39+
expected = dest_dir("posts", "1914798137981389", "larry-had-a-little-lamb", "index.html").to_s
40+
dest = redirect_page.destination("/")
41+
dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
42+
expect(dest).to eql(expected)
4143
end
4244
end
4345

4446
context "of a redirect page meant to be a file" do
4547
it "knows not to add the index.html if it's not a folder" do
46-
dest = dest_dir("/posts/12435151125/larry-had-a-little-lamb")
47-
expect(redirect_page.destination("/")).to eql(dest)
48+
expected = dest_dir("posts", "12435151125", "larry-had-a-little-lamb#{forced_output_ext}").to_s
49+
dest = redirect_page.destination("/")
50+
dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
51+
expect(dest).to eql(expected)
4852
end
4953
end
5054
end
@@ -58,7 +62,7 @@
5862
end
5963

6064
it "fetches the path properly" do
61-
expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb$/
65+
expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb#{forced_output_ext}$/
6266
end
6367

6468
it "is written to the proper location" do

spec/jekyll_redirect_from/redirector_spec.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
let(:page_with_many_redirect_to) { setup_page("multiple_redirect_tos.md") }
1212

1313
it "knows if a page or post is requesting a redirect page" do
14-
if Jekyll::VERSION < '3.0.0'
15-
expect(redirector.has_alt_urls?(post_to_redirect)).to be_truthy
16-
else
14+
if JekyllRedirectFrom.jekyll_3?
1715
skip "Don't need to test posts in Jekyll 3"
16+
else
17+
expect(redirector.has_alt_urls?(post_to_redirect)).to be_truthy
1818
end
1919
end
2020

@@ -52,33 +52,33 @@
5252
end
5353

5454
it "generates the refresh page for the post properly" do
55-
expect(destination_file_exists?("posts/23128432159832/mary-had-a-little-lamb")).to be_truthy
55+
expect(dest_dir("posts/23128432159832/mary-had-a-little-lamb#{forced_output_ext}")).to exist
5656
end
5757

5858
it "generates the refresh pages for the page with multiple redirect_from urls" do
59-
expect(destination_file_exists?("help")).to be_truthy
60-
expect(destination_file_exists?("contact")).to be_truthy
61-
expect(destination_file_exists?("let-there/be/light-he-said")).to be_truthy
62-
expect(destination_file_exists?("/geepers/mccreepin")).to be_truthy
59+
expect(dest_dir("help")).to be_truthy
60+
expect(dest_dir("contact")).to be_truthy
61+
expect(dest_dir("let-there/be/light-he-said")).to be_truthy
62+
expect(dest_dir("/geepers/mccreepin")).to be_truthy
6363
end
6464

6565
it "generates the refresh page for the page with one redirect_from url" do
66-
expect(destination_file_exists?("mencius/was/my/father")).to be_truthy
66+
expect(dest_dir("mencius/was/my/father#{forced_output_ext}")).to exist
6767
end
6868

6969
it "generates the refresh page for the collection with one redirect_to url" do
70-
expect(@dest.join("articles", "redirect-somewhere-else-plz.html")).to exist
71-
expect(destination_doc_contents("articles", "redirect-somewhere-else-plz.html")).to include(%|<meta http-equiv="refresh" content="0; url=http://www.zombo.com">|)
70+
expect(dest_dir("articles", "redirect-somewhere-else-plz.html")).to exist
71+
expect(dest_dir("articles", "redirect-somewhere-else-plz.html").read).to include(%|<meta http-equiv="refresh" content="0; url=http://www.zombo.com">|)
7272
end
7373

7474
it "generates the refresh page for the page with one redirect_to url" do
75-
expect(destination_file_exists?("one_redirect_to.html")).to be_truthy
76-
expect(destination_file_contents("one_redirect_to.html")).to include(%|<meta http-equiv="refresh" content="0; url=https://www.github.com">|)
75+
expect(dest_dir("one_redirect_to.html")).to exist
76+
expect(dest_dir("one_redirect_to.html").read).to include(%|<meta http-equiv="refresh" content="0; url=https://www.github.com">|)
7777
end
7878

7979
it "generates the refresh page for the page with multiple redirect_to urls" do
80-
expect(destination_file_exists?("multiple_redirect_tos.html")).to be_truthy
81-
expect(destination_file_contents("multiple_redirect_tos.html")).to include(%|<meta http-equiv="refresh" content="0; url=https://www.jekyllrb.com">|)
80+
expect(dest_dir("multiple_redirect_tos.html")).to exist
81+
expect(dest_dir("multiple_redirect_tos.html").read).to include(%|<meta http-equiv="refresh" content="0; url=https://www.jekyllrb.com">|)
8282
end
8383
end
8484

spec/spec_helper.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
end
3939

4040
def dest_dir(*paths)
41-
File.join(@dest.to_s, *paths)
41+
@dest.join(*paths)
4242
end
4343

4444
def unpublished_doc
@@ -57,26 +57,19 @@ def setup_page(file)
5757
Jekyll::Page.new(@site, @fixtures_path.to_s, File.dirname(file), File.basename(file))
5858
end
5959

60-
def destination_file_exists?(file)
61-
File.exists?(File.join(@dest.to_s, file))
62-
end
63-
64-
def destination_file_contents(file)
65-
File.read(File.join(@dest.to_s, file))
66-
end
67-
68-
def destination_doc_contents(collection, file)
69-
File.read(File.join(@dest.to_s, collection, file))
70-
end
71-
7260
def new_redirect_page(permalink)
73-
page = JekyllRedirectFrom::RedirectPage.new(@site, @site.source, "", "")
61+
page = JekyllRedirectFrom::RedirectPage.new(@site, @site.source, "", "index.html")
7462
page.data['permalink'] = permalink
63+
page.data['sitemap'] = false
7564
page
7665
end
7766

7867
def destination_sitemap
79-
File.read(File.join(@dest.to_s, 'sitemap.xml'))
68+
@dest.join("sitemap.xml").read
69+
end
70+
71+
def forced_output_ext
72+
JekyllRedirectFrom.jekyll_3? ? ".html" : ""
8073
end
8174
end
8275

0 commit comments

Comments
 (0)