diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5f16476 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format progress diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ad3f18d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: ruby +rvm: + - 2.1.0 + - 2.0.0 + - 1.9.3 +script: "script/cibuild" diff --git a/Rakefile b/Rakefile index 2995527..8754e40 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,6 @@ require "bundler/gem_tasks" +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/jekyll-redirect-from.gemspec b/jekyll-redirect-from.gemspec index d5a40b0..748b257 100644 --- a/jekyll-redirect-from.gemspec +++ b/jekyll-redirect-from.gemspec @@ -1,11 +1,11 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'jekyll/redirect_from/version' +require 'jekyll_redirect_from/version' Gem::Specification.new do |spec| spec.name = "jekyll-redirect-from" - spec.version = Jekyll::RedirectFrom::VERSION + spec.version = JekyllRedirectFrom::VERSION spec.authors = ["Parker Moore"] spec.email = ["parkrmoore@gmail.com"] spec.description = %q{Seamlessly specify multiple redirection URLs for your pages and posts} @@ -22,4 +22,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" + spec.add_development_dependency "rspec" end diff --git a/lib/jekyll-redirect-from.rb b/lib/jekyll-redirect-from.rb new file mode 100644 index 0000000..ca66a56 --- /dev/null +++ b/lib/jekyll-redirect-from.rb @@ -0,0 +1,4 @@ +require "jekyll" +require "jekyll_redirect_from/version" +require "jekyll_redirect_from/redirect_page" +require "jekyll_redirect_from/redirector" diff --git a/lib/jekyll/redirect_from/redirect_page.rb b/lib/jekyll/redirect_from/redirect_page.rb deleted file mode 100644 index f9ab1d2..0000000 --- a/lib/jekyll/redirect_from/redirect_page.rb +++ /dev/null @@ -1,41 +0,0 @@ -module Jekyll - class RedirectFrom < Generator - class RedirectPage < Page - # Initialize a new RedirectPage. - # - # site - The Site object. - # base - The String path to the source. - # dir - The String path between the source and the file. - # name - The String filename of the file. - def initialize(site, base, dir, name) - @site = site - @base = base - @dir = dir - @name = name - - self.process(name) - end - - def generate_redirect_content(item_url) - self.content = <<-EOF - - -
-Redirecting...
-Click here if you are not redirected.
- - - - EOF - end - end - end -end diff --git a/lib/jekyll/redirect_from/version.rb b/lib/jekyll/redirect_from/version.rb deleted file mode 100644 index 9477dee..0000000 --- a/lib/jekyll/redirect_from/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Jekyll - class RedirectFrom - VERSION = "0.1.0" - end -end diff --git a/lib/jekyll_redirect_from/redirect_page.rb b/lib/jekyll_redirect_from/redirect_page.rb new file mode 100644 index 0000000..52fb021 --- /dev/null +++ b/lib/jekyll_redirect_from/redirect_page.rb @@ -0,0 +1,40 @@ +module JekyllRedirectFrom + class RedirectPage < Jekyll::Page + # Initialize a new RedirectPage. + # + # site - The Site object. + # base - The String path to the source. + # dir - The String path between the source and the file. + # name - The String filename of the file. + def initialize(site, base, dir, name) + @site = site + @base = base + @dir = dir + @name = name + + self.process(name) + self.data = {} + end + + def generate_redirect_content(item_url) + self.output = self.content = <<-EOF + + + +Redirecting...
+Click here if you are not redirected.
+ + + + EOF + end + end +end diff --git a/lib/jekyll/redirect_from.rb b/lib/jekyll_redirect_from/redirector.rb similarity index 86% rename from lib/jekyll/redirect_from.rb rename to lib/jekyll_redirect_from/redirector.rb index bc42e41..037aaea 100644 --- a/lib/jekyll/redirect_from.rb +++ b/lib/jekyll_redirect_from/redirector.rb @@ -1,8 +1,5 @@ -require "jekyll/redirect_from/version" -require "jekyll/redirect_from/redirect_page" - -module Jekyll - class RedirectFrom < Generator +module JekyllRedirectFrom + class Redirector < Jekyll::Generator def generate(site) original_pages = site.pages.dup generate_alt_urls(site, site.posts) diff --git a/lib/jekyll_redirect_from/version.rb b/lib/jekyll_redirect_from/version.rb new file mode 100644 index 0000000..a8491ec --- /dev/null +++ b/lib/jekyll_redirect_from/version.rb @@ -0,0 +1,3 @@ +module JekyllRedirectFrom + VERSION = "0.1.0" +end diff --git a/script/bootstrap b/script/bootstrap new file mode 100755 index 0000000..8a09d8a --- /dev/null +++ b/script/bootstrap @@ -0,0 +1,3 @@ +#! /bin/bash + +bundle install diff --git a/script/cibuild b/script/cibuild new file mode 100755 index 0000000..2834a20 --- /dev/null +++ b/script/cibuild @@ -0,0 +1,4 @@ +#! /bin/bash + +script/bootstrap > /dev/null 2>&1 +bundle exec rake spec diff --git a/spec/fixtures/_config.yml b/spec/fixtures/_config.yml new file mode 100644 index 0000000..ec0ea86 --- /dev/null +++ b/spec/fixtures/_config.yml @@ -0,0 +1,3 @@ +url: http://jekyllrb.com +gems: + - jekyll-redirect-from diff --git a/spec/fixtures/_posts/2014-01-03-redirect-me-plz.md b/spec/fixtures/_posts/2014-01-03-redirect-me-plz.md new file mode 100644 index 0000000..e25d2c3 --- /dev/null +++ b/spec/fixtures/_posts/2014-01-03-redirect-me-plz.md @@ -0,0 +1,6 @@ +--- +title: Please redirect me, sir. +redirect_from: /posts/23128432159832/mary-had-a-little-lamb +--- + +Yay. diff --git a/spec/fixtures/multiple_redirect_urls.md b/spec/fixtures/multiple_redirect_urls.md new file mode 100644 index 0000000..d7a196d --- /dev/null +++ b/spec/fixtures/multiple_redirect_urls.md @@ -0,0 +1,10 @@ +--- +title: I have lots of redirect urls +redirect_from: +- help +- contact +- let-there/be/light-he-said +- /geepers/mccreepin +--- + +Lots of redirect urls diff --git a/spec/fixtures/one_redirect_url.md b/spec/fixtures/one_redirect_url.md new file mode 100644 index 0000000..20bd637 --- /dev/null +++ b/spec/fixtures/one_redirect_url.md @@ -0,0 +1,6 @@ +--- +title: I only have one redirect url. +redirect_from: mencius/was/my/father +--- + +One redirect url diff --git a/spec/jekyll_redirect_from/redirect_page_spec.rb b/spec/jekyll_redirect_from/redirect_page_spec.rb new file mode 100644 index 0000000..9f702f4 --- /dev/null +++ b/spec/jekyll_redirect_from/redirect_page_spec.rb @@ -0,0 +1,50 @@ +require "spec_helper" + +describe JekyllRedirectFrom::RedirectPage do + let(:redirect_page) { described_class.new(@site, @site.source, "posts/12435151125", "larry-had-a-little-lamb") } + let(:item_url) { File.join(@site.config["url"], "2014", "01", "03", "moving-to-jekyll.md") } + let(:page_content) { redirect_page.generate_redirect_content(item_url) } + + context "#generate_redirect_content" do + it "sets the #content to the generated refresh page" do + expect(page_content).to eq(" \n \n \nRedirecting...
\n \n \n \n \n") + end + + it "contains the meta refresh tag" do + expect(page_content).to include("") + end + + it "contains JavaScript redirect" do + expect(page_content).to include("document.location.href = \"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\";") + end + + it "contains canonical link in header" do + expect(page_content).to include("") + end + + it "contains a clickable link to redirect" do + expect(page_content).to include("Click here if you are not redirected.") + end + end + + context "when writing to disk" do + let(:redirect_page_full_path) { redirect_page.destination(@site.dest) } + + before(:each) do + redirect_page.generate_redirect_content(item_url) + redirect_page.write(@site.dest) + end + + it "fetches the path properly" do + expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb/ + end + + it "is written to the proper location" do + expect(File.exist?(redirect_page_full_path)).to be_true + end + + it "writes the context we expect" do + expect(File.read(redirect_page_full_path)).to eql(page_content) + end + end +end \ No newline at end of file diff --git a/spec/jekyll_redirect_from/redirector_spec.rb b/spec/jekyll_redirect_from/redirector_spec.rb new file mode 100644 index 0000000..96192d3 --- /dev/null +++ b/spec/jekyll_redirect_from/redirector_spec.rb @@ -0,0 +1,41 @@ +require "spec_helper" + +describe JekyllRedirectFrom::Redirector do + let(:redirector) { described_class.new } + let(:post_to_redirect) { setup_post("2014-01-03-redirect-me-plz.md") } + let(:page_with_one) { setup_page("one_redirect_url.md") } + let(:page_with_many) { setup_page("multiple_redirect_urls.md") } + + it "knows if a page or post is requesting a redirect page" do + expect(redirector.has_alt_urls?(post_to_redirect)).to be_true + end + + it "handles one redirect path" do + expect(redirector.alt_urls(page_with_one)).to eql(["mencius/was/my/father"]) + end + + it "handles many redirect paths" do + expect(redirector.alt_urls(page_with_many)).to eql(["help", "contact", "let-there/be/light-he-said", "/geepers/mccreepin"]) + end + + context "refresh page generation" do + before(:all) do + described_class.new.generate(@site) + end + + it "generates the refresh page for the post properly" do + expect(destination_file_exists?("posts/23128432159832/mary-had-a-little-lamb")).to be_true + end + + it "generates the refresh pages for the page with multiple redirect_from urls" do + expect(destination_file_exists?("help")).to be_true + expect(destination_file_exists?("contact")).to be_true + expect(destination_file_exists?("let-there/be/light-he-said")).to be_true + expect(destination_file_exists?("/geepers/mccreepin")).to be_true + end + + it "generates the refresh page for the page with one redirect_from url" do + expect(destination_file_exists?("mencius/was/my/father")).to be_true + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..4587b1a --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,48 @@ +require "jekyll" +require File.expand_path("lib/jekyll-redirect-from.rb") + +RSpec.configure do |config| + config.treat_symbols_as_metadata_keys_with_true_values = true + config.run_all_when_everything_filtered = true + config.filter_run :focus + config.order = 'random' + + config.expect_with :rspec do |c| + c.syntax = :expect + end + + config.before(:all) do + Jekyll.logger.log_level = Jekyll::Stevenson::ERROR + + @fixtures_path = Pathname.new(__FILE__).parent.join("fixtures") + @dest = @fixtures_path.join("_site") + @posts_src = File.join(@fixtures_path, "_posts") + @layouts_src = File.join(@fixtures_path, "_layouts") + @plugins_src = File.join(@fixtures_path, "_plugins") + + @site = Jekyll::Site.new(Jekyll.configuration({ + "source" => @fixtures_path.to_s, + "destination" => @dest.to_s, + "plugins" => @plugins_src + })) + + @dest.rmtree if @dest.exist? + @site.process + end + + config.after(:all) do + @dest.rmtree if @dest.exist? + end + + def setup_post(file) + Jekyll::Post.new(@site, @fixtures_path, '', file) + end + + def setup_page(file) + Jekyll::Page.new(@site, @fixtures_path, File.dirname(file), File.basename(file)) + end + + def destination_file_exists?(file) + File.exists?(File.join(@dest.to_s, file)) + end +end