Allow placeholders in permalinks

This commit is contained in:
Alfred Xing 2014-10-26 18:23:58 -07:00
parent 4e8ebd999a
commit 0fe1d0686a
2 changed files with 32 additions and 3 deletions

View File

@ -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 \

View File

@ -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