diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 2db21fee..49839f6b 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -61,14 +61,7 @@ module Jekyll end if data.key?('date') - begin - 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 + self.date = Utils.parse_date(data["date"].to_s, "Post '#{relative_path}' does not have a valid date in the YAML front matter.") end populate_categories @@ -168,14 +161,9 @@ module Jekyll m, cats, date, slug, ext = *name.match(MATCHER) self.categories ||= [] 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.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 # The generated directory into which the post will be placed diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 15bacb14..87b84f0e 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -49,7 +49,7 @@ module Jekyll # # Returns nothing 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.posts = [] self.pages = [] diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb index af366702..0f92df49 100644 --- a/lib/jekyll/tags/post_url.rb +++ b/lib/jekyll/tags/post_url.rb @@ -7,9 +7,9 @@ module Jekyll def initialize(name) 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 - @date = Time.parse(date) + @date = Utils.parse_date(date, "'#{name}' does not contain valid date.") end def ==(other) diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index a3910d81..dc190297 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -82,6 +82,19 @@ module Jekyll transform_keys(hash) { |key| key.to_s rescue key } 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 diff --git a/test/test_utils.rb b/test/test_utils.rb index de1ebba5..d4daa097 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -64,4 +64,37 @@ class TestUtils < Test::Unit::TestCase 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