From d5322a73bfb03e622ac70283df5289507fe3c558 Mon Sep 17 00:00:00 2001 From: Alfred Xing Date: Sat, 2 Aug 2014 09:34:05 -0700 Subject: [PATCH] Move instances of `Time.parse` into a Utils method --- lib/jekyll/post.rb | 16 ++-------------- lib/jekyll/site.rb | 2 +- lib/jekyll/tags/post_url.rb | 4 ++-- lib/jekyll/utils.rb | 13 +++++++++++++ 4 files changed, 18 insertions(+), 17 deletions(-) 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..fe8eb79c 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 = "Invalid date: could not be parsed.") + Time.parse(input) + rescue ArgumentError + raise Errors::FatalException.new(msg) + end + end end end