Merge pull request #2373 from tschmidt/handle-categories-in-site-config-defaults

This commit is contained in:
Parker Moore 2014-05-21 01:10:37 -04:00
commit b9c3d8ba03
5 changed files with 80 additions and 10 deletions

View File

@ -35,17 +35,25 @@ module Jekyll
# #
# Returns an array # Returns an array
def pluralized_array_from_hash(hash, singular_key, plural_key) def pluralized_array_from_hash(hash, singular_key, plural_key)
if hash.has_key?(singular_key) [].tap do |array|
array = [hash[singular_key]] if hash[singular_key] array << (value_from_singular_key(hash, singular_key) || value_from_plural_key(hash, plural_key))
elsif hash.has_key?(plural_key) end.flatten.compact
case hash[plural_key] end
def value_from_singular_key(hash, key)
hash[key] if (hash.has_key?(key) || (hash.default_proc && hash[key]))
end
def value_from_plural_key(hash, key)
if hash.has_key?(key) || (hash.default_proc && hash[key])
val = hash[key]
case val
when String when String
array = hash[plural_key].split val.split
when Array when Array
array = hash[plural_key].compact val.compact
end end
end end
array || []
end end
def transform_keys(hash) def transform_keys(hash)

View File

@ -0,0 +1,6 @@
---
layout: default
title: Category in YAML
---
Best *post* ever

View File

@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
end end
should "ensure post count is as expected" do should "ensure post count is as expected" do
assert_equal 41, @site.posts.size assert_equal 42, @site.posts.size
end end
should "insert site.posts into the index" do should "insert site.posts into the index" do

View File

@ -666,5 +666,61 @@ class TestPost < Test::Unit::TestCase
end end
end end
context "site config with category" do
setup do
config = Jekyll::Configuration::DEFAULTS.merge({
'defaults' => [
'scope' => {
'path' => ''
},
'values' => {
'category' => 'article'
}
]
})
@site = Site.new(config)
end
should "return category if post does not specify category" do
post = setup_post("2009-01-27-no-category.textile")
assert post.categories.include?('article'), "Expected post.categories to include 'article' but did not."
end
should "override site category if set on post" do
post = setup_post("2009-01-27-category.textile")
assert post.categories.include?('foo'), "Expected post.categories to include 'foo' but did not."
assert !post.categories.include?('article'), "Did not expect post.categories to include 'article' but it did."
end
end
context "site config with categories" do
setup do
config = Jekyll::Configuration::DEFAULTS.merge({
'defaults' => [
'scope' => {
'path' => ''
},
'values' => {
'categories' => ['article']
}
]
})
@site = Site.new(config)
end
should "return categories if post does not specify categories" do
post = setup_post("2009-01-27-no-category.textile")
assert post.categories.include?('article'), "Expected post.categories to include 'article' but did not."
end
should "override site categories if set on post" do
post = setup_post("2009-01-27-categories.textile")
['foo', 'bar', 'baz'].each do |category|
assert post.categories.include?(category), "Expected post.categories to include '#{category}' but did not."
end
assert !post.categories.include?('article'), "Did not expect post.categories to include 'article' but it did."
end
end
end end

View File

@ -15,9 +15,9 @@ class TestUtils < Test::Unit::TestCase
assert_equal [], Utils.pluralized_array_from_hash(data, 'tag', 'tags') assert_equal [], Utils.pluralized_array_from_hash(data, 'tag', 'tags')
end end
should "return empty array with matching nil singular" do should "return plural array with nil singular" do
data = { 'foo' => 'bar', 'tag' => nil, 'tags' => ['dog', 'cat'] } data = { 'foo' => 'bar', 'tag' => nil, 'tags' => ['dog', 'cat'] }
assert_equal [], Utils.pluralized_array_from_hash(data, 'tag', 'tags') assert_equal ['dog', 'cat'], Utils.pluralized_array_from_hash(data, 'tag', 'tags')
end end
should "return single value array with matching singular" do should "return single value array with matching singular" do