From a72629908ae5c0886c4c1962df8ed0d1324f4868 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 20 Jan 2016 12:23:48 -0800 Subject: [PATCH] Document: throw a useful error when an invalid date is given --- features/post_data.feature | 14 ++++++++++++++ lib/jekyll/document.rb | 23 ++++++++++++++--------- lib/jekyll/site.rb | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/features/post_data.feature b/features/post_data.feature index 3b4f3cf7..3692c382 100644 --- a/features/post_data.feature +++ b/features/post_data.feature @@ -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 diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 2911c4aa..8e0d3e86 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -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 diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 15698b99..72d42ad9 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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