Merge pull request #2682 from alfredxing/date-parser-util

This commit is contained in:
Parker Moore 2014-08-06 15:30:02 -04:00
commit cb671acd0c
5 changed files with 51 additions and 17 deletions

View File

@ -61,14 +61,7 @@ module Jekyll
end end
if data.key?('date') if data.key?('date')
begin self.date = Utils.parse_date(data["date"].to_s, "Post '#{relative_path}' does not have a valid date in the YAML front matter.")
self.date = Time.parse(data["date"].to_s)
rescue ArgumentError
path = File.join(@dir || "", name)
msg = "Post '#{relative_path}' does not have a valid date in the YAML front matter.\n"
msg << "Fix the date, or exclude the file or directory from being processed"
raise Errors::FatalException.new(msg)
end
end end
populate_categories populate_categories
@ -168,14 +161,9 @@ module Jekyll
m, cats, date, slug, ext = *name.match(MATCHER) m, cats, date, slug, ext = *name.match(MATCHER)
self.categories ||= [] self.categories ||= []
self.categories += (cats || '').downcase.split('/') self.categories += (cats || '').downcase.split('/')
self.date = Time.parse(date) self.date = Utils.parse_date(date, "Post '#{relative_path}' does not have a valid date in the filename.")
self.slug = slug self.slug = slug
self.ext = ext self.ext = ext
rescue ArgumentError
path = File.join(@dir || "", name)
msg = "Post '#{relative_path}' does not have a valid date.\n"
msg << "Fix the date, or exclude the file or directory from being processed"
raise Errors::FatalException.new(msg)
end end
# The generated directory into which the post will be placed # The generated directory into which the post will be placed

View File

@ -49,7 +49,7 @@ module Jekyll
# #
# Returns nothing # Returns nothing
def reset def reset
self.time = (config['time'] ? Time.parse(config['time'].to_s) : Time.now) self.time = (config['time'] ? Utils.parse_date(config['time'].to_s, "Invalid time in _config.yml.") : Time.now)
self.layouts = {} self.layouts = {}
self.posts = [] self.posts = []
self.pages = [] self.pages = []

View File

@ -7,9 +7,9 @@ module Jekyll
def initialize(name) def initialize(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 raise ArgumentError.new("'#{name}' does not contain valid date and/or title.") unless all
@slug = path ? path + slug : slug @slug = path ? path + slug : slug
@date = Time.parse(date) @date = Utils.parse_date(date, "'#{name}' does not contain valid date.")
end end
def ==(other) def ==(other)

View File

@ -82,6 +82,19 @@ module Jekyll
transform_keys(hash) { |key| key.to_s rescue key } transform_keys(hash) { |key| key.to_s rescue key }
end end
# Parse a date/time and throw an error if invalid
#
# input - the date/time to parse
# msg - (optional) the error message to show the user
#
# Returns the parsed date if successful, throws a FatalException
# if not
def parse_date(input, msg = "Input could not be parsed.")
Time.parse(input)
rescue ArgumentError
raise Errors::FatalException.new("Invalid date '#{input}': " + msg)
end
end end
end end
end end

View File

@ -64,4 +64,37 @@ class TestUtils < Test::Unit::TestCase
end end
context "The \`Utils.parse_date\` method" do
should "parse a properly formatted date" do
assert Utils.parse_date("2014-08-02 14:43:06 PDT").is_a? Time
end
should "throw an error if the input contains no date data" do
assert_raise Jekyll::Errors::FatalException do
Utils.parse_date("Blah")
end
end
should "throw an error if the input is out of range" do
assert_raise Jekyll::Errors::FatalException do
Utils.parse_date("9999-99-99")
end
end
should "throw an error with the default message if no message is passed in" do
date = "Blah this is invalid"
assert_raise Jekyll::Errors::FatalException, "Invalid date '#{date}': Input could not be parsed." do
Utils.parse_date(date)
end
end
should "throw an error with the provided message if a message is passed in" do
date = "Blah this is invalid"
message = "Aaaah, the world has exploded!"
assert_raise Jekyll::Errors::FatalException, "Invalid date '#{date}': #{message}" do
Utils.parse_date(date, message)
end
end
end
end end