bunto-redirect-from 5.0.0 → 6.0.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.
@@ -1,27 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'bunto-redirect-from/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "bunto-redirect-from"
8
- spec.version = BuntoRedirectFrom::VERSION
9
- spec.authors = ["Parker Moore", "Suriyaa Kudo"]
10
- spec.email = ["parkrmoore@gmail.com", "SuriyaaKudoIsc@users.noreply.github.com"]
11
- spec.description = %q{Seamlessly specify multiple redirection URLs for your pages and posts}
12
- spec.summary = %q{Seamlessly specify multiple redirection URLs for your pages and posts}
13
- spec.homepage = "https://github.com/bunto/bunto-redirect-from"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_runtime_dependency "bunto"
22
-
23
- spec.add_development_dependency "bundler", "~> 1.3"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec"
26
- spec.add_development_dependency "bunto-sitemap", "~> 2.0.0"
27
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bunto-redirect-from/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bunto-redirect-from"
8
+ spec.version = BuntoRedirectFrom::VERSION
9
+ spec.authors = ["Parker Moore", "Suriyaa Kudo"]
10
+ spec.email = ["parkrmoore@gmail.com", "SuriyaaKudoIsc@users.noreply.github.com"]
11
+ spec.description = %q{Seamlessly specify multiple redirection URLs for your pages and posts}
12
+ spec.summary = %q{Seamlessly specify multiple redirection URLs for your pages and posts}
13
+ spec.homepage = "https://github.com/bunto/bunto-redirect-from"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "bunto"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "bunto-sitemap"
27
+ end
@@ -1,4 +1,11 @@
1
- require "bunto"
2
- require "bunto-redirect-from/version"
3
- require "bunto-redirect-from/redirect_page"
4
- require "bunto-redirect-from/redirector"
1
+ require "bunto"
2
+
3
+ module BuntoRedirectFrom
4
+ def self.bunto_3?
5
+ @bunto_3 ||= (Bunto::VERSION >= '3.0.0')
6
+ end
7
+ end
8
+
9
+ require "bunto-redirect-from/version"
10
+ require "bunto-redirect-from/redirect_page"
11
+ require "bunto-redirect-from/redirector"
@@ -1,34 +1,42 @@
1
- # Encoding: utf-8
2
-
3
- module BuntoRedirectFrom
4
- class RedirectPage < Bunto::Page
5
- # Initialize a new RedirectPage.
6
- #
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 = {}
19
- end
20
-
21
- def generate_redirect_content(item_url)
22
- self.output = self.content = <<-EOF
23
- <!DOCTYPE html>
24
- <meta charset="utf-8">
25
- <title>Redirecting…</title>
26
- <link rel="canonical" href="#{item_url}">
27
- <meta http-equiv="refresh" content="0; url=#{item_url}">
28
- <h1>Redirecting…</h1>
29
- <a href="#{item_url}">Click here if you are not redirected.</a>
30
- <script>location="#{item_url}"</script>
31
- EOF
32
- end
33
- end
34
- end
1
+ # Encoding: utf-8
2
+
3
+ module BuntoRedirectFrom
4
+ class RedirectPage < Bunto::Page
5
+ # Initialize a new RedirectPage.
6
+ #
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, "sitemap" => false }
19
+
20
+ data.default_proc = proc do |_, key|
21
+ site.frontmatter_defaults.find(File.join(dir, name), type, key)
22
+ end
23
+
24
+ Bunto::Hooks.trigger :pages, :post_init, self if BuntoRedirectFrom.bunto_3?
25
+ end
26
+
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
40
+ end
41
+ end
42
+ end
@@ -1,86 +1,90 @@
1
- module BuntoRedirectFrom
2
- class Redirector < Bunto::Generator
3
- safe true
4
-
5
- def generate(site)
6
- original_pages = site.pages.dup
7
- generate_alt_urls(site, site.posts) if Bunto::VERSION < '3.0.0'
8
- generate_alt_urls(site, original_pages)
9
- generate_alt_urls(site, site.docs_to_write)
10
- end
11
-
12
- def generate_alt_urls(site, list)
13
- list.each do |item|
14
- if has_alt_urls?(item)
15
- alt_urls(item).each do |alt_url|
16
- redirect_page = RedirectPage.new(site, site.source, "", "")
17
- redirect_page.data['permalink'] = alt_url
18
- redirect_page.data['sitemap'] = false
19
- redirect_page.generate_redirect_content(redirect_url(site, item))
20
- site.pages << redirect_page
21
- end
22
- end
23
- if has_redirect_to_url?(item)
24
- redirect_to_url(item).flatten.each do |alt_url|
25
- item.data['sitemap'] = false
26
- redirect_page = RedirectPage.new(site, site.source, File.dirname(item.url), File.basename(item.url))
27
- redirect_page.data['permalink'] = item.url
28
- redirect_page.data['sitemap'] = false
29
- redirect_page.generate_redirect_content(alt_url)
30
- if item.is_a?(Bunto::Document)
31
- item.content = item.output = redirect_page.content
32
- else
33
- site.pages << redirect_page
34
- end
35
- end
36
- end
37
- end
38
- end
39
-
40
- def is_dynamic_document?(page_or_post)
41
- page_or_post.is_a?(Bunto::Page) ||
42
- page_or_post.is_a?(Bunto::Document) ||
43
- (Bunto::VERSION < '3.0.0' &&
44
- page_or_post.is_a?(Bunto::Post))
45
- end
46
-
47
- def has_alt_urls?(page_or_post)
48
- is_dynamic_document?(page_or_post) &&
49
- page_or_post.data.has_key?('redirect_from') &&
50
- !alt_urls(page_or_post).empty?
51
- end
52
-
53
- def alt_urls(page_or_post)
54
- Array[page_or_post.data['redirect_from']].flatten.compact
55
- end
56
-
57
- def has_redirect_to_url?(page_or_post)
58
- is_dynamic_document?(page_or_post) &&
59
- page_or_post.data.has_key?('redirect_to') &&
60
- !redirect_to_url(page_or_post).empty?
61
- end
62
-
63
- def redirect_to_url(page_or_post)
64
- [Array[page_or_post.data['redirect_to']].flatten.first].compact
65
- end
66
-
67
- def redirect_url(site, item)
68
- File.join redirect_prefix(site), item.url
69
- end
70
-
71
- def redirect_prefix(site)
72
- config_github_url(site) || config_baseurl(site) || ""
73
- end
74
-
75
- def config_github_url(site)
76
- github_config = site.config['github']
77
- if github_config.is_a?(Hash) && github_config['url']
78
- github_config['url'].to_s
79
- end
80
- end
81
-
82
- def config_baseurl(site)
83
- site.config.fetch('baseurl', nil)
84
- end
85
- end
86
- end
1
+ module BuntoRedirectFrom
2
+ class Redirector < Bunto::Generator
3
+ safe true
4
+
5
+ def generate(site)
6
+ original_pages = site.pages.dup
7
+ generate_alt_urls(site, site.posts) if Bunto::VERSION < '3.0.0'
8
+ generate_alt_urls(site, original_pages)
9
+ generate_alt_urls(site, site.docs_to_write)
10
+ end
11
+
12
+ def generate_alt_urls(site, list)
13
+ list.each do |item|
14
+ if has_alt_urls?(item)
15
+ alt_urls(item).each do |alt_url|
16
+ redirect_page = RedirectPage.new(site, site.source, "", "redirect.html")
17
+ redirect_page.data['permalink'] = alt_url
18
+ redirect_page.generate_redirect_content(redirect_url(site, item))
19
+ site.pages << redirect_page
20
+ end
21
+ end
22
+ if has_redirect_to_url?(item)
23
+ redirect_to_url(item).flatten.each do |alt_url|
24
+ item.data['sitemap'] = false
25
+ item.data['layout'] = nil
26
+
27
+ item.url << "index.html" if item.url.end_with?("/")
28
+ redirect_page = RedirectPage.new(site, site.source, File.dirname(item.url), File.basename(item.url))
29
+
30
+ redirect_page.data['permalink'] = item.url
31
+ redirect_page.generate_redirect_content(alt_url)
32
+ if item.is_a?(Bunto::Document)
33
+ item.content = item.output = redirect_page.content
34
+ else
35
+ site.pages << redirect_page
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def is_dynamic_document?(page_or_post)
43
+ page_or_post.is_a?(Bunto::Page) ||
44
+ page_or_post.is_a?(Bunto::Document) ||
45
+ (Bunto::VERSION < '3.0.0' &&
46
+ page_or_post.is_a?(Bunto::Post))
47
+ end
48
+
49
+ def has_alt_urls?(page_or_post)
50
+ is_dynamic_document?(page_or_post) &&
51
+ page_or_post.data.has_key?('redirect_from') &&
52
+ !alt_urls(page_or_post).empty?
53
+ end
54
+
55
+ def alt_urls(page_or_post)
56
+ Array[page_or_post.data['redirect_from']].flatten.compact
57
+ end
58
+
59
+ def has_redirect_to_url?(page_or_post)
60
+ is_dynamic_document?(page_or_post) &&
61
+ page_or_post.data.has_key?('redirect_to') &&
62
+ !redirect_to_url(page_or_post).empty?
63
+ end
64
+
65
+ def redirect_to_url(page_or_post)
66
+ [Array[page_or_post.data['redirect_to']].flatten.first].compact
67
+ end
68
+
69
+ def redirect_url(site, item)
70
+ File.join redirect_prefix(site), item.url
71
+ end
72
+
73
+ def redirect_prefix(site)
74
+ config_github_url(site) || config_url(site)
75
+ end
76
+
77
+ def config_github_url(site)
78
+ github_config = site.config['github']
79
+ if github_config.is_a?(Hash) && github_config['url']
80
+ github_config['url'].to_s
81
+ end
82
+ end
83
+
84
+ def config_url(site)
85
+ url = site.config.fetch('url', nil) || ""
86
+ baseurl = site.config.fetch('baseurl', nil) || ""
87
+ File.join url, baseurl
88
+ end
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
- module BuntoRedirectFrom
2
- VERSION = "5.0.0"
3
- end
1
+ module BuntoRedirectFrom
2
+ VERSION = "6.0.0"
3
+ end
data/script/bootstrap CHANGED
@@ -1,3 +1,3 @@
1
- #! /bin/bash
2
-
3
- bundle install -j8
1
+ #! /bin/bash
2
+
3
+ bundle install -j8
data/script/cibuild CHANGED
@@ -1,4 +1,4 @@
1
- #! /bin/bash
2
- set -e
3
-
4
- bundle exec rake spec
1
+ #! /bin/bash
2
+ set -e
3
+
4
+ bundle exec rake spec
data/script/release CHANGED
@@ -1,3 +1,3 @@
1
- #! /bin/bash
2
-
3
- bundle exec rake release
1
+ #! /bin/bash
2
+
3
+ bundle exec rake release
data/script/test ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ bundle exec rspec $@
@@ -1,72 +1,76 @@
1
- # Encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe BuntoRedirectFrom::RedirectPage do
6
- let(:permalink) { "/posts/12435151125/larry-had-a-little-lamb" }
7
- let(:redirect_page) { new_redirect_page(permalink) }
8
- let(:item_url) { File.join(@site.config["url"], "2014", "01", "03", "moving-to-bunto.md") }
9
- let(:page_content) { redirect_page.generate_redirect_content(item_url) }
10
-
11
- context "#generate_redirect_content" do
12
- it "sets the #content to the generated refresh page" do
13
- expect(page_content).to eq("<!DOCTYPE html>\n<meta charset=\"utf-8\">\n<title>Redirecting…</title>\n<link rel=\"canonical\" href=\"#{item_url}\">\n<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">\n<h1>Redirecting…</h1>\n<a href=\"#{item_url}\">Click here if you are not redirected.</a>\n<script>location=\"#{item_url}\"</script>\n")
14
- end
15
-
16
- it "contains the meta refresh tag" do
17
- expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">")
18
- end
19
-
20
- it "contains JavaScript redirect" do
21
- expect(page_content).to include("location=\"http://bunto.isc/2014/01/03/moving-to-bunto.md\"")
22
- end
23
-
24
- it "contains canonical link in header" do
25
- expect(page_content).to include("<link rel=\"canonical\" href=\"http://bunto.isc/2014/01/03/moving-to-bunto.md\">")
26
- end
27
-
28
- it "contains a clickable link to redirect" do
29
- expect(page_content).to include("<a href=\"http://bunto.isc/2014/01/03/moving-to-bunto.md\">Click here if you are not redirected.</a>")
30
- end
31
- end
32
-
33
- context "when determining the write destination" do
34
- context "of a redirect page meant to be a dir" do
35
- let(:permalink_dir) { "/posts/1914798137981389/larry-had-a-little-lamb/" }
36
- let(:redirect_page) { new_redirect_page(permalink_dir) }
37
-
38
- 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)
41
- end
42
- end
43
-
44
- context "of a redirect page meant to be a file" do
45
- 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
- end
49
- end
50
- end
51
-
52
- context "when writing to disk" do
53
- let(:redirect_page_full_path) { redirect_page.destination(@site.dest) }
54
-
55
- before(:each) do
56
- redirect_page.generate_redirect_content(item_url)
57
- redirect_page.write(@site.dest)
58
- end
59
-
60
- it "fetches the path properly" do
61
- expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb$/
62
- end
63
-
64
- it "is written to the proper location" do
65
- expect(File.exist?(redirect_page_full_path)).to be_truthy
66
- end
67
-
68
- it "writes the context we expect" do
69
- expect(File.read(redirect_page_full_path)).to eql(page_content)
70
- end
71
- end
72
- end
1
+ # Encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe BuntoRedirectFrom::RedirectPage do
6
+ let(:permalink) { "/posts/12435151125/larry-had-a-little-lamb" }
7
+ let(:redirect_page) { new_redirect_page(permalink) }
8
+ let(:item_url) { File.join(@site.config["url"], "2014", "01", "03", "moving-to-bunto.md") }
9
+ let(:page_content) { redirect_page.generate_redirect_content(item_url) }
10
+
11
+ context "#generate_redirect_content" do
12
+ it "sets the #content to the generated refresh page" do
13
+ expect(page_content).to eq("<!DOCTYPE html>\n<html lang=\"en-US\">\n<meta charset=\"utf-8\">\n<title>Redirecting…</title>\n<link rel=\"canonical\" href=\"#{item_url}\">\n<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">\n<h1>Redirecting…</h1>\n<a href=\"#{item_url}\">Click here if you are not redirected.</a>\n<script>location=\"#{item_url}\"</script>\n</html>\n")
14
+ end
15
+
16
+ it "contains the meta refresh tag" do
17
+ expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">")
18
+ end
19
+
20
+ it "contains JavaScript redirect" do
21
+ expect(page_content).to include("location=\"http://bunto.github.io/2014/01/03/moving-to-bunto.md\"")
22
+ end
23
+
24
+ it "contains canonical link in header" do
25
+ expect(page_content).to include("<link rel=\"canonical\" href=\"http://bunto.github.io/2014/01/03/moving-to-bunto.md\">")
26
+ end
27
+
28
+ it "contains a clickable link to redirect" do
29
+ expect(page_content).to include("<a href=\"http://bunto.github.io/2014/01/03/moving-to-bunto.md\">Click here if you are not redirected.</a>")
30
+ end
31
+ end
32
+
33
+ context "when determining the write destination" do
34
+ context "of a redirect page meant to be a dir" do
35
+ let(:permalink_dir) { "/posts/1914798137981389/larry-had-a-little-lamb/" }
36
+ let(:redirect_page) { new_redirect_page(permalink_dir) }
37
+
38
+ it "knows to add the index.html if it's a folder" do
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)
43
+ end
44
+ end
45
+
46
+ context "of a redirect page meant to be a file" do
47
+ it "knows not to add the index.html if it's not a folder" do
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)
52
+ end
53
+ end
54
+ end
55
+
56
+ context "when writing to disk" do
57
+ let(:redirect_page_full_path) { redirect_page.destination(@site.dest) }
58
+
59
+ before(:each) do
60
+ redirect_page.generate_redirect_content(item_url)
61
+ redirect_page.write(@site.dest)
62
+ end
63
+
64
+ it "fetches the path properly" do
65
+ expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb#{forced_output_ext}$/
66
+ end
67
+
68
+ it "is written to the proper location" do
69
+ expect(File.exist?(redirect_page_full_path)).to be_truthy
70
+ end
71
+
72
+ it "writes the context we expect" do
73
+ expect(File.read(redirect_page_full_path)).to eql(page_content)
74
+ end
75
+ end
76
+ end