Merge pull request #4670 from jekyll/clean-up-post-url

Merge pull request 4670
This commit is contained in:
jekyllbot 2016-03-16 13:15:26 -07:00
commit 5930bbfac1
5 changed files with 54 additions and 15 deletions

View File

@ -6,5 +6,9 @@ module Jekyll
InvalidPermalinkError = Class.new(FatalException) InvalidPermalinkError = Class.new(FatalException)
InvalidYAMLFrontMatterError = Class.new(FatalException) InvalidYAMLFrontMatterError = Class.new(FatalException)
MissingDependencyException = Class.new(FatalException) MissingDependencyException = Class.new(FatalException)
InvalidDateError = Class.new(FatalException)
InvalidPostNameError = Class.new(FatalException)
PostURLError = Class.new(FatalException)
end end
end end

View File

@ -7,22 +7,30 @@ module Jekyll
def initialize(name) def initialize(name)
@name = name @name = name
all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER) all, @path, @date, @slug = *name.sub(/^\//, "").match(MATCHER)
raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all unless all
raise Jekyll::Errors::InvalidPostNameError,
"'#{name}' does not contain valid date and/or title."
end
@name_regex = /^#{path}#{date}-#{slug}\.[^.]+/ @name_regex = /^#{path}#{date}-#{slug}\.[^.]+/
end end
def post_date
@post_date ||= Utils.parse_date(date,
"\"#{date}\" does not contain valid date and/or title.")
end
def ==(other) def ==(other)
other.basename.match(@name_regex) other.basename.match(@name_regex)
end end
def deprecated_equality(other) def deprecated_equality(other)
date = Utils.parse_date(name, "'#{name}' does not contain valid date and/or title.")
slug == post_slug(other) && slug == post_slug(other) &&
date.year == other.date.year && post_date.year == other.date.year &&
date.month == other.date.month && post_date.month == other.date.month &&
date.day == other.date.day post_date.day == other.date.day
end end
private private
@ -47,11 +55,13 @@ module Jekyll
@orig_post = post.strip @orig_post = post.strip
begin begin
@post = PostComparer.new(@orig_post) @post = PostComparer.new(@orig_post)
rescue rescue => e
raise ArgumentError.new <<-eos raise Jekyll::Errors::PostURLError, <<-eos
Could not parse name of post "#{@orig_post}" in tag 'post_url'. Could not parse name of post "#{@orig_post}" in tag 'post_url'.
Make sure the post exists and the name is correct. Make sure the post exists and the name is correct.
#{e.class}: #{e.message}
eos eos
end end
end end
@ -75,7 +85,7 @@ eos
return p.url return p.url
end end
raise ArgumentError.new <<-eos raise Jekyll::Errors::PostURLError, <<-eos
Could not find post "#{@orig_post}" in tag 'post_url'. Could not find post "#{@orig_post}" in tag 'post_url'.
Make sure the post exists and the name is correct. Make sure the post exists and the name is correct.

View File

@ -1,3 +1,4 @@
module Jekyll module Jekyll
module Utils module Utils
extend self extend self
@ -126,7 +127,7 @@ module Jekyll
def parse_date(input, msg = "Input could not be parsed.") def parse_date(input, msg = "Input could not be parsed.")
Time.parse(input).localtime Time.parse(input).localtime
rescue ArgumentError rescue ArgumentError
raise Errors::FatalException.new("Invalid date '#{input}': " + msg) raise Errors::InvalidDateError, "Invalid date '#{input}': #{msg}"
end end
# Determines whether a given file has # Determines whether a given file has

View File

@ -470,8 +470,32 @@ title: Invalid post name linking
{% post_url abc2008-11-21-complex %} {% post_url abc2008-11-21-complex %}
CONTENT CONTENT
assert_raises ArgumentError do assert_raises Jekyll::Errors::PostURLError do
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true}) create_post(content, {
'permalink' => 'pretty',
'source' => source_dir,
'destination' => dest_dir,
'read_posts' => true
})
end
end
should "cause an error with a bad date" do
content = <<CONTENT
---
title: Invalid post name linking
---
{% post_url 2008-42-21-complex %}
CONTENT
assert_raises Jekyll::Errors::InvalidDateError do
create_post(content, {
'permalink' => 'pretty',
'source' => source_dir,
'destination' => dest_dir,
'read_posts' => true
})
end end
end end
end end

View File

@ -95,20 +95,20 @@ class TestUtils < JekyllUnitTest
end end
should "throw an error if the input contains no date data" do should "throw an error if the input contains no date data" do
assert_raises Jekyll::Errors::FatalException do assert_raises Jekyll::Errors::InvalidDateError do
Utils.parse_date("Blah") Utils.parse_date("Blah")
end end
end end
should "throw an error if the input is out of range" do should "throw an error if the input is out of range" do
assert_raises Jekyll::Errors::FatalException do assert_raises Jekyll::Errors::InvalidDateError do
Utils.parse_date("9999-99-99") Utils.parse_date("9999-99-99")
end end
end end
should "throw an error with the default message if no message is passed in" do should "throw an error with the default message if no message is passed in" do
date = "Blah this is invalid" date = "Blah this is invalid"
assert_raises Jekyll::Errors::FatalException, "Invalid date '#{date}': Input could not be parsed." do assert_raises Jekyll::Errors::InvalidDateError, "Invalid date '#{date}': Input could not be parsed." do
Utils.parse_date(date) Utils.parse_date(date)
end end
end end
@ -116,7 +116,7 @@ class TestUtils < JekyllUnitTest
should "throw an error with the provided message if a message is passed in" do should "throw an error with the provided message if a message is passed in" do
date = "Blah this is invalid" date = "Blah this is invalid"
message = "Aaaah, the world has exploded!" message = "Aaaah, the world has exploded!"
assert_raises Jekyll::Errors::FatalException, "Invalid date '#{date}': #{message}" do assert_raises Jekyll::Errors::InvalidDateError, "Invalid date '#{date}': #{message}" do
Utils.parse_date(date, message) Utils.parse_date(date, message)
end end
end end