Filter entries in the collection per EntryFilter#filter

This commit is contained in:
Parker Moore 2014-04-06 12:59:31 -04:00
parent 323ea0ef73
commit aa502348e5
5 changed files with 57 additions and 17 deletions

View File

@ -25,16 +25,34 @@ module Jekyll
#
# Returns the sorted array of docs.
def read
Dir.glob(File.join(directory, "**", "*.*")).each do |file_path|
if allowed_document?(file_path)
doc = Jekyll::Document.new(file_path, { site: site, collection: self })
doc.read
docs << doc
end
filtered_entries.each do |file_path|
doc = Jekyll::Document.new(Jekyll.sanitized_path(directory, file_path), { site: site, collection: self })
doc.read
docs << doc
end
docs.sort!
end
# All the entries in this collection.
#
# Returns an Array of file paths to the documents in this collection
# relative to the collection's directory
def entries
Dir.glob(File.join(directory, "**", "*.*")).map do |entry|
entry[File.join(directory, "")] = ''; entry
end
end
# Filtered version of the entries in this collection.
# See `Jekyll::EntryFilter#filter` for more information.
#
# Returns a list of filtered entry paths.
def filtered_entries
Dir.chdir(directory) do
entry_filter.filter(entries)
end
end
# The directory for this Collection, relative to the site source.
#
# Returns a String containing the directory name where the collection
@ -51,14 +69,12 @@ module Jekyll
Jekyll.sanitized_path(site.source, relative_directory)
end
# Determine whether the document at a given path is an allowed document.
# The entry filter for this collection.
# Creates an instance of Jekyll::EntryFilter.
#
# path - the path to the document within this collection
#
# Returns false if the site is in safe mode and the document is a symlink,
# true otherwise.
def allowed_document?(path)
!(site.safe && File.symlink?(path))
# Returns the instance of Jekyll::EntryFilter for this collection.
def entry_filter
@entry_filter ||= Jekyll::EntryFilter.new(site, relative_directory)
end
# An inspect string.

View File

@ -193,12 +193,12 @@ module Jekyll
#
# Returns a Hash representing this Document's data.
def to_liquid
data.merge({
Utils.deep_merge_hashes data, {
"content" => content,
"path" => path,
"relative_path" => relative_path,
"url" => url
})
}
end
# The inspect string for this document.

View File

@ -0,0 +1,5 @@
---
title: The unreadable wonder
---
Don't read me, you fool! FILTER ME

View File

@ -0,0 +1,5 @@
---
title: Don't Include Me Either
---
Don't include me either. FILTER ME PLZ

View File

@ -68,6 +68,7 @@ class TestCollections < Test::Unit::TestCase
"collections" => ["methods"]
})
@site.process
@collection = @site.collections["methods"]
end
should "create a Hash on Site with the label mapped to the instance of the Collection" do
@ -89,6 +90,19 @@ class TestCollections < Test::Unit::TestCase
], doc.relative_path
end
end
should "not include files which start with an underscore in the base collection directory" do
assert_not_include @collection.filtered_entries, "_do_not_read_me.md"
end
should "not include files which start with an underscore in a subdirectory" do
assert_not_include @collection.filtered_entries, "site/_dont_include_me_either.md"
end
should "not include the underscored files in the list of docs" do
assert_not_include @collection.docs.map(&:relative_path), "_methods/_do_not_read_me.md"
assert_not_include @collection.docs.map(&:relative_path), "_methods/site/_dont_include_me_either.md"
end
end
context "in safe mode" do
@ -102,11 +116,11 @@ class TestCollections < Test::Unit::TestCase
end
should "not allow symlinks" do
assert !@collection.allowed_document?(File.join(@collection.directory, "um_hi.md"))
assert_not_include @collection.filtered_entries, "um_hi.md"
end
should "not include the symlinked file in the list of docs" do
assert_not_include %w[_methods/um_hi.md], @collection.docs.map(&:relative_path)
assert_not_include @collection.docs.map(&:relative_path), "_methods/um_hi.md"
end
end