Document: only superdirectories of the collection are categories

This commit is contained in:
Parker Moore 2015-11-04 15:18:02 +07:00
parent 6e8fd8cb50
commit db6103bdee
4 changed files with 47 additions and 7 deletions

View File

@ -187,6 +187,24 @@ Feature: Post data
Then the _site directory should exist
And I should see "Post category: movies" in "_site/movies/2009/03/27/star-wars.html"
Scenario: Superdirectories of _posts applied to post.categories
Given I have a movies/_posts directory
And I have a "movies/_posts/2009-03-27-star-wars.html" page with layout "simple" that contains "hi"
And I have a _layouts directory
And I have a simple layout that contains "Post category: {{ page.categories }}"
When I run jekyll build
Then the _site directory should exist
And I should see "Post category: movies" in "_site/movies/2009/03/27/star-wars.html"
Scenario: Subdirectories of _posts not applied to post.categories
Given I have a movies/_posts/scifi directory
And I have a "movies/_posts/scifi/2009-03-27-star-wars.html" page with layout "simple" that contains "hi"
And I have a _layouts directory
And I have a simple layout that contains "Post category: {{ page.categories }}"
When I run jekyll build
Then the _site directory should exist
And I should see "Post category: movies" in "_site/movies/2009/03/27/star-wars.html"
Scenario: Use post.categories variable when categories are in YAML with mixed case
Given I have a _posts directory
And I have a _layouts directory

View File

@ -24,10 +24,11 @@ module Jekyll
@collection = relations[:collection]
@has_yaml_header = nil
subdirs = relative_path.split(File::SEPARATOR).reject do |c|
c.empty? || c.eql?(collection.relative_directory) || c.eql?("_drafts") || c.eql?(basename)
if draft?
categories_from_path("_drafts")
else
categories_from_path(collection.relative_directory)
end
merge_data!({'categories' => subdirs })
data.default_proc = proc do |hash, key|
site.frontmatter_defaults.find(relative_path, collection.label, key)
@ -75,6 +76,15 @@ module Jekyll
data['date'] ||= site.time
end
# Returns whether the document is a draft. This is only the case if
# the document is in the 'posts' collection but in a different
# directory than '_posts'.
#
# Returns whether the document is a draft.
def draft?
data['draft'] ||= relative_path.index(collection.relative_directory).nil? && collection.label == "posts"
end
# The path to the document, relative to the site source.
#
# Returns a String path which represents the relative path
@ -311,9 +321,21 @@ module Jekyll
end
end
# Add superdirectories of the special_dir to categories.
# In the case of es/_posts, 'es' is added as a category.
# In the case of _posts/es, 'es' is NOT added as a category.
#
# Returns nothing.
def categories_from_path(special_dir)
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 })
end
def populate_categories
merge_data!({
"categories" => (
'categories' => (
Array(data['categories']) + Utils.pluralized_array_from_hash(data, 'category', 'categories')
).map { |c| c.to_s }.flatten.uniq
})

View File

@ -220,7 +220,7 @@ class TestSite < JekyllUnitTest
posts = Dir[source_dir("**", "_posts", "**", "*")]
posts.delete_if { |post| File.directory?(post) && !(post =~ Document::DATE_FILENAME_MATCHER) }
categories = %w(2013 bar baz category foo z_category MixedCase Mixedcase es publish_test win).sort
categories = %w(2013 bar baz category foo z_category MixedCase Mixedcase publish_test win).sort
assert_equal posts.size - @num_invalid_posts, @site.posts.size
assert_equal categories, @site.categories.keys.sort

View File

@ -455,8 +455,8 @@ CONTENT
end
should "have the url to the \"nested\" post from 2008-11-21" do
assert_match %r{3\s/es/2008/11/21/nested/}, @result
assert_match %r{4\s/es/2008/11/21/nested/}, @result
assert_match %r{3\s/2008/11/21/nested/}, @result
assert_match %r{4\s/2008/11/21/nested/}, @result
end
end