From 0fe1d0686ae24e4b69b16025278190c0340827e4 Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Sun, 26 Oct 2014 18:23:58 -0700 Subject: [PATCH 1/3] Allow placeholders in permalinks --- lib/jekyll/url.rb | 17 +++++++++++++++-- test/test_url.rb | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 777ae3d1..f0c1e602 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -37,11 +37,11 @@ module Jekyll # # Returns the String URL def to_s - sanitize_url(@permalink || generate_url) + sanitize_url(generate_permalink_url || generate_url) end # Internal: Generate the URL by replacing all placeholders with their - # respective values + # respective values in the template # # Returns the _unsanitizied_ String URL def generate_url @@ -51,6 +51,19 @@ module Jekyll end end + # Internal: Generate the URL by replacing all placeholders with their + # respective values in the permalink + # + # Returns the _unsanitizied_ String URL + def generate_permalink_url + if not @permalink.nil? + @placeholders.inject(@permalink) do |result, token| + break result if result.index(':').nil? + result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) + end + end + end + # Returns a sanitized String URL def sanitize_url(in_url) url = in_url \ diff --git a/test/test_url.rb b/test/test_url.rb index dcbe8bed..15f989d8 100644 --- a/test/test_url.rb +++ b/test/test_url.rb @@ -23,7 +23,7 @@ class TestURL < Test::Unit::TestCase ).to_s end - should "return permalink if given" do + should "use permalink if given" do assert_equal "/le/perma/link", URL.new( :template => "/:x/:y", :placeholders => {:x => "foo", :y => "bar"}, @@ -31,5 +31,21 @@ class TestURL < Test::Unit::TestCase ).to_s end + should "replace placeholders in permalinks" do + assert_equal "/foo/bar", URL.new( + :template => "/baz", + :permalink => "/:x/:y", + :placeholders => {:x => "foo", :y => "bar"} + ).to_s + end + + should "handle multiple of the same key in the permalink" do + assert_equal '/foo/bar/foo/', URL.new( + :template => "/baz", + :permalink => "/:x/:y/:x/", + :placeholders => {:x => "foo", :y => "bar"} + ).to_s + end + end end From cd9d38c5ea5f62bc44c0945179e471f95929ff71 Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Sun, 26 Oct 2014 21:38:19 -0700 Subject: [PATCH 2/3] Remove duplicate code --- lib/jekyll/url.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index f0c1e602..7dd8cfe7 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -37,33 +37,20 @@ module Jekyll # # Returns the String URL def to_s - sanitize_url(generate_permalink_url || generate_url) + sanitize_url(generate_url) end # Internal: Generate the URL by replacing all placeholders with their - # respective values in the template + # respective values in the template or permalink # # Returns the _unsanitizied_ String URL def generate_url - @placeholders.inject(@template) do |result, token| + @placeholders.inject(@permalink || @template) do |result, token| break result if result.index(':').nil? result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) end end - # Internal: Generate the URL by replacing all placeholders with their - # respective values in the permalink - # - # Returns the _unsanitizied_ String URL - def generate_permalink_url - if not @permalink.nil? - @placeholders.inject(@permalink) do |result, token| - break result if result.index(':').nil? - result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) - end - end - end - # Returns a sanitized String URL def sanitize_url(in_url) url = in_url \ From 02e53fb6ff5d668057542b55e71775d1102cbfbf Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Mon, 27 Oct 2014 22:59:15 -0700 Subject: [PATCH 3/3] Implement @parkr's suggestions --- lib/jekyll/url.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 7dd8cfe7..5adf2468 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -37,15 +37,29 @@ module Jekyll # # Returns the String URL def to_s - sanitize_url(generate_url) + sanitize_url(generated_permalink || generated_url) + end + + # Generates a URL from the permalink + # + # Returns the _unsanitized String URL + def generated_permalink + (@generated_permlink ||= generate_url(@permalink)) if @permalink + end + + # Generates a URL from the template + # + # Returns the _unsanitized String URL + def generated_url + @generated_url ||= generate_url(@template) end # Internal: Generate the URL by replacing all placeholders with their - # respective values in the template or permalink + # respective values in the given template # # Returns the _unsanitizied_ String URL - def generate_url - @placeholders.inject(@permalink || @template) do |result, token| + def generate_url(template) + @placeholders.inject(template) do |result, token| break result if result.index(':').nil? result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) end