Document: throw a useful error when an invalid date is given

This commit is contained in:
Parker Moore 2016-01-20 12:23:48 -08:00
parent ec0eff3315
commit a72629908a
3 changed files with 29 additions and 10 deletions

View File

@ -39,6 +39,20 @@ Feature: Post data
And the _site directory should exist
And I should see "Post date: 27 Mar 2009" in "_site/2009/03/27/star-wars.html"
Scenario: Use post.date variable with invalid
Given I have a _posts directory
And I have a "_posts/2016-01-01-test.md" page with date "tuesday" that contains "I have a bad date."
When I run jekyll build
Then the _site directory should not exist
And I should see "Document '_posts/2016-01-01-test.md' does not have a valid date in the YAML front matter." in the build output
Scenario: Invalid date in filename
Given I have a _posts directory
And I have a "_posts/2016-22-01-test.md" page that contains "I have a bad date."
When I run jekyll build
Then the _site directory should not exist
And I should see "Document '_posts/2016-22-01-test.md' does not have a valid date in the filename." in the build output
Scenario: Use post.id variable
Given I have a _posts directory
And I have a _layouts directory

View File

@ -50,7 +50,7 @@ module Jekyll
# Merge some data in with this document's data.
#
# Returns the merged data.
def merge_data!(other)
def merge_data!(other, source: "YAML front matter")
if other.key?('categories') && !other['categories'].nil?
if other['categories'].is_a?(String)
other['categories'] = other['categories'].split(" ").map(&:strip)
@ -61,7 +61,7 @@ module Jekyll
if data.key?('date') && !data['date'].is_a?(Time)
data['date'] = Utils.parse_date(
data['date'].to_s,
"Document '#{relative_path}' does not have a valid date in the YAML front matter."
"Document '#{relative_path}' does not have a valid date in the #{source}."
)
end
data
@ -267,20 +267,23 @@ module Jekyll
else
begin
defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym)
merge_data!(defaults) unless defaults.empty?
merge_data!(defaults, source: "front matter defaults") unless defaults.empty?
self.content = File.read(path, merged_file_read_opts(opts))
if content =~ YAML_FRONT_MATTER_REGEXP
self.content = $POSTMATCH
data_file = SafeYAML.load(Regexp.last_match(1))
merge_data!(data_file) if data_file
merge_data!(data_file, source: "YAML front matter") if data_file
end
post_read
rescue SyntaxError => e
puts "YAML Exception reading #{path}: #{e.message}"
Jekyll.logger.error "Error:", "YAML Exception reading #{path}: #{e.message}"
rescue Exception => e
puts "Error reading file #{path}: #{e.message}"
if e.is_a? Jekyll::Errors::FatalException
raise e
end
Jekyll.logger.error "Error:", "could not read file #{path}: #{e.message}"
end
end
end
@ -291,9 +294,11 @@ module Jekyll
merge_data!({
"slug" => slug,
"ext" => ext
})
merge_data!({ "date" => date }) if data['date'].nil? || data['date'].to_i == site.time.to_i
}, source: "filename")
data['title'] ||= slug.split('-').select(&:capitalize).join(' ')
if data['date'].nil? || data['date'].to_i == site.time.to_i
merge_data!({"date" => date}, source: "filename")
end
end
populate_categories
populate_tags
@ -312,7 +317,7 @@ module Jekyll
superdirs = relative_path.sub(/#{special_dir}(.*)/, '').split(File::SEPARATOR).reject do |c|
c.empty? || c.eql?(special_dir) || c.eql?(basename)
end
merge_data!({ 'categories' => superdirs })
merge_data!({ 'categories' => superdirs }, source: "file path")
end
def populate_categories

View File

@ -225,7 +225,7 @@ module Jekyll
# Build a hash map based on the specified post attribute ( post attr =>
# array of posts ) then sort each array in reverse order.
hash = Hash.new { |h, key| h[key] = [] }
posts.docs.each { |p| p.data[post_attr].each { |t| hash[t] << p } }
posts.docs.each { |p| p.data[post_attr].each { |t| hash[t] << p } if p.data[post_attr] }
hash.values.each { |posts| posts.sort!.reverse! }
hash
end