Set categor[y|ies] on post if they appear in site frontmatter defaults

This commit is contained in:
Terry Schmidt 2014-05-09 13:34:13 -05:00
parent 6c22ae3759
commit 0371b69952
5 changed files with 81 additions and 10 deletions

View File

@ -35,17 +35,26 @@ module Jekyll
#
# Returns an array
def pluralized_array_from_hash(hash, singular_key, plural_key)
if hash.has_key?(singular_key)
array = [hash[singular_key]] if hash[singular_key]
elsif hash.has_key?(plural_key)
case hash[plural_key]
array = []
array << value_from_singular_key(hash, singular_key)
array << value_from_plural_key(hash, plural_key) if array.flatten.compact.empty?
array.flatten.compact
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
array = hash[plural_key].split
val.split
when Array
array = hash[plural_key].compact
val.compact
end
end
array || []
end
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
should "ensure post count is as expected" do
assert_equal 41, @site.posts.size
assert_equal 42, @site.posts.size
end
should "insert site.posts into the index" do

View File

@ -645,5 +645,61 @@ class TestPost < Test::Unit::TestCase
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

View File

@ -15,9 +15,9 @@ class TestUtils < Test::Unit::TestCase
assert_equal [], Utils.pluralized_array_from_hash(data, 'tag', 'tags')
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'] }
assert_equal [], Utils.pluralized_array_from_hash(data, 'tag', 'tags')
assert_equal ['dog', 'cat'], Utils.pluralized_array_from_hash(data, 'tag', 'tags')
end
should "return single value array with matching singular" do