Move instances of `Time.parse` into a Utils method

This commit is contained in:
Alfred Xing 2014-08-02 09:34:05 -07:00
parent 9a6dc7b520
commit d5322a73bf
4 changed files with 18 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 = "Invalid date: could not be parsed.")
Time.parse(input)
rescue ArgumentError
raise Errors::FatalException.new(msg)
end
end end
end end
end end