From f8a66ca76c1dcc0ce79aed2655f5660527aa7d60 Mon Sep 17 00:00:00 2001 From: ashmaroli Date: Sat, 10 Mar 2018 07:41:24 +0530 Subject: [PATCH] Improve handling non-default collection documents rendering and writing (#6795) Merge pull request 6795 --- docs/_docs/collections.md | 14 ++++++++++ features/collections.feature | 50 +++++++++++++++++++++++++++++++++++- lib/jekyll/collection.rb | 11 ++++---- lib/jekyll/document.rb | 5 ++-- test/test_document.rb | 8 +++--- 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/docs/_docs/collections.md b/docs/_docs/collections.md index bd57c302..c45a3868 100644 --- a/docs/_docs/collections.md +++ b/docs/_docs/collections.md @@ -348,6 +348,20 @@ you specified in your `_config.yml` (if present) and the following information: {% raw %}{{ site.collections | where: "label", "myCollection" | first }}{% endraw %}

+
+
Collections and Time
+

Except for documents in hard-coded default collection posts, all documents in collections + you create, are accessible via Liquid irrespective of their assigned date, if any, and therefore renderable. +

+

However documents are attempted to be written to disk only if the concerned collection + metadata has output: true. Additionally, future-dated documents are only written if + site.future is also true. +

+

More fine-grained control over documents being written to disk can be exercised by setting + published: false (true by default) in the document's front matter. +

+
+ ### Documents diff --git a/features/collections.feature b/features/collections.feature index ba663510..a8a4e402 100644 --- a/features/collections.feature +++ b/features/collections.feature @@ -116,7 +116,31 @@ Feature: Collections And the _site directory should exist And the "_site/puppies/fido.html" file should exist - Scenario: Hidden collection with document with future date + Scenario: Access rendered collection with future dated document via Liquid + Given I have a _puppies directory + And I have the following documents under the puppies collection: + | title | date | content | + | Rover | 2007-12-31 | content for Rover. | + | Fido | 2120-12-31 | content for Fido. | + And I have a "_config.yml" file with content: + """ + collections: + puppies: + output: true + """ + And I have a "index.html" page that contains "Newest puppy: {% assign puppy = site.puppies.last %}{{ puppy.title }}" + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "Newest puppy: Fido" in "_site/index.html" + But the "_site/puppies/fido.html" file should not exist + When I run jekyll build --future + Then I should get a zero exit status + And the _site directory should exist + And I should see "Newest puppy: Fido" in "_site/index.html" + And the "_site/puppies/fido.html" file should exist + + Scenario: Unrendered collection with future dated document Given I have a _puppies directory And I have the following documents under the puppies collection: | title | date | content | @@ -139,6 +163,30 @@ Feature: Collections And the _site directory should exist And the "_site/puppies/fido.html" file should not exist + Scenario: Access unrendered collection with future dated document via Liquid + Given I have a _puppies directory + And I have the following documents under the puppies collection: + | title | date | content | + | Rover | 2007-12-31 | content for Rover. | + | Fido | 2120-12-31 | content for Fido. | + And I have a "_config.yml" file with content: + """ + collections: + puppies: + output: false + """ + And I have a "index.html" page that contains "Newest puppy: {% assign puppy = site.puppies.last %}{{ puppy.title }}" + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "Newest puppy: Fido" in "_site/index.html" + But the "_site/puppies/fido.html" file should not exist + When I run jekyll build --future + Then I should get a zero exit status + And the _site directory should exist + And I should see "Newest puppy: Fido" in "_site/index.html" + And the "_site/puppies/fido.html" file should not exist + Scenario: All the documents Given I have an "index.html" page that contains "All documents: {% for doc in site.documents %}{{ doc.relative_path }} {% endfor %}" And I have fixture collections diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index 2ec4d20d..2b33a1e6 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -210,12 +210,11 @@ module Jekyll private def read_document(full_path) - doc = Jekyll::Document.new(full_path, :site => site, :collection => self) - doc.read - if site.publisher.publish?(doc) || !write? - docs << doc - else - Jekyll.logger.debug "Skipped Publishing:", doc.relative_path + docs << Document.new(full_path, :site => site, :collection => self).tap do |doc| + doc.read + if !site.publisher.publish?(doc) && site.publisher.hidden_in_the_future?(doc) + Jekyll.logger.debug "Skip Publishing:", "#{doc.relative_path} has a future date" + end end end diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 908490c2..1485672f 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -311,9 +311,10 @@ module Jekyll # Based on the Collection to which it belongs. # # True if the document has a collection and if that collection's #write? - # method returns true, otherwise false. + # method returns true, and if the site's Publisher will publish the document. + # False otherwise. def write? - collection && collection.write? + collection && collection.write? && site.publisher.publish?(self) end # The Document excerpt_separator, from the YAML Front-Matter or site diff --git a/test/test_document.rb b/test/test_document.rb index 213271c8..ce8dbeda 100644 --- a/test/test_document.rb +++ b/test/test_document.rb @@ -449,14 +449,16 @@ class TestDocument < JekyllUnitTest refute_nil @files.find do |doc| doc.relative_path == "_slides/example-slide-4.html" end + assert_exist dest_dir("slides/example-slide-4.html") end end context "with output overrides" do should "be output according its front matter" do - assert_nil( - @files.find { |doc| doc.relative_path == "_slides/non-outputted-slide.html" } - ) + assert @files.find do |doc| + doc.relative_path == "_slides/non-outputted-slide.html" + end + refute_exist dest_dir("slides/non-outputted-slide.html") end end end