Move instances of `Time.parse` into a Utils method
This commit is contained in:
parent
9a6dc7b520
commit
d5322a73bf
|
@ -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
|
||||
|
|
|
@ -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 = []
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue