Merge pull request #3031 from alfredxing/frontmatter-permalinks
This commit is contained in:
commit
8dcf7a6680
|
@ -37,15 +37,29 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the String URL
|
# Returns the String URL
|
||||||
def to_s
|
def to_s
|
||||||
sanitize_url(@permalink || 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
|
end
|
||||||
|
|
||||||
# Internal: Generate the URL by replacing all placeholders with their
|
# Internal: Generate the URL by replacing all placeholders with their
|
||||||
# respective values
|
# respective values in the given template
|
||||||
#
|
#
|
||||||
# Returns the _unsanitizied_ String URL
|
# Returns the _unsanitizied_ String URL
|
||||||
def generate_url
|
def generate_url(template)
|
||||||
@placeholders.inject(@template) do |result, token|
|
@placeholders.inject(template) do |result, token|
|
||||||
break result if result.index(':').nil?
|
break result if result.index(':').nil?
|
||||||
result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
|
result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ class TestURL < Test::Unit::TestCase
|
||||||
).to_s
|
).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
should "return permalink if given" do
|
should "use permalink if given" do
|
||||||
assert_equal "/le/perma/link", URL.new(
|
assert_equal "/le/perma/link", URL.new(
|
||||||
:template => "/:x/:y",
|
:template => "/:x/:y",
|
||||||
:placeholders => {:x => "foo", :y => "bar"},
|
:placeholders => {:x => "foo", :y => "bar"},
|
||||||
|
@ -31,5 +31,21 @@ class TestURL < Test::Unit::TestCase
|
||||||
).to_s
|
).to_s
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue