Improve handling non-default collection documents rendering and writing (#6795)

Merge pull request 6795
This commit is contained in:
ashmaroli 2018-03-10 07:41:24 +05:30 committed by jekyllbot
parent 7508da11c1
commit f8a66ca76c
5 changed files with 76 additions and 12 deletions

View File

@ -348,6 +348,20 @@ you specified in your `_config.yml` (if present) and the following information:
<code>{% raw %}{{ site.collections | where: "label", "myCollection" | first }}{% endraw %}</code></p> <code>{% raw %}{{ site.collections | where: "label", "myCollection" | first }}{% endraw %}</code></p>
</div> </div>
<div class="note info">
<h5>Collections and Time</h5>
<p>Except for documents in hard-coded default collection <code>posts</code>, all documents in collections
you create, are accessible via Liquid irrespective of their assigned date, if any, and therefore renderable.
</p>
<p>However documents are attempted to be written to disk only if the concerned collection
metadata has <code>output: true</code>. Additionally, future-dated documents are only written if
<code>site.future</code> <em>is also true</em>.
</p>
<p>More fine-grained control over documents being written to disk can be exercised by setting
<code>published: false</code> (<em><code>true</code> by default</em>) in the document's front matter.
</p>
</div>
### Documents ### Documents

View File

@ -116,7 +116,31 @@ Feature: Collections
And the _site directory should exist And the _site directory should exist
And the "_site/puppies/fido.html" file 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 Given I have a _puppies directory
And I have the following documents under the puppies collection: And I have the following documents under the puppies collection:
| title | date | content | | title | date | content |
@ -139,6 +163,30 @@ Feature: Collections
And the _site directory should exist And the _site directory should exist
And the "_site/puppies/fido.html" file should not 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 Scenario: All the documents
Given I have an "index.html" page that contains "All documents: {% for doc in site.documents %}{{ doc.relative_path }} {% endfor %}" 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 And I have fixture collections

View File

@ -210,12 +210,11 @@ module Jekyll
private private
def read_document(full_path) def read_document(full_path)
doc = Jekyll::Document.new(full_path, :site => site, :collection => self) docs << Document.new(full_path, :site => site, :collection => self).tap do |doc|
doc.read doc.read
if site.publisher.publish?(doc) || !write? if !site.publisher.publish?(doc) && site.publisher.hidden_in_the_future?(doc)
docs << doc Jekyll.logger.debug "Skip Publishing:", "#{doc.relative_path} has a future date"
else end
Jekyll.logger.debug "Skipped Publishing:", doc.relative_path
end end
end end

View File

@ -311,9 +311,10 @@ module Jekyll
# Based on the Collection to which it belongs. # Based on the Collection to which it belongs.
# #
# True if the document has a collection and if that collection's #write? # 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? def write?
collection && collection.write? collection && collection.write? && site.publisher.publish?(self)
end end
# The Document excerpt_separator, from the YAML Front-Matter or site # The Document excerpt_separator, from the YAML Front-Matter or site

View File

@ -449,14 +449,16 @@ class TestDocument < JekyllUnitTest
refute_nil @files.find do |doc| refute_nil @files.find do |doc|
doc.relative_path == "_slides/example-slide-4.html" doc.relative_path == "_slides/example-slide-4.html"
end end
assert_exist dest_dir("slides/example-slide-4.html")
end end
end end
context "with output overrides" do context "with output overrides" do
should "be output according its front matter" do should "be output according its front matter" do
assert_nil( assert @files.find do |doc|
@files.find { |doc| doc.relative_path == "_slides/non-outputted-slide.html" } doc.relative_path == "_slides/non-outputted-slide.html"
) end
refute_exist dest_dir("slides/non-outputted-slide.html")
end end
end end
end end