diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index dc19a3e4..6dab6127 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -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) diff --git a/test/source/_posts/2009-01-27-no-category.textile b/test/source/_posts/2009-01-27-no-category.textile new file mode 100644 index 00000000..af8382df --- /dev/null +++ b/test/source/_posts/2009-01-27-no-category.textile @@ -0,0 +1,6 @@ +--- +layout: default +title: Category in YAML +--- + +Best *post* ever diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 78a49a44..ca3124c5 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -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 diff --git a/test/test_post.rb b/test/test_post.rb index 9f963d0f..a13a7c5d 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -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 diff --git a/test/test_utils.rb b/test/test_utils.rb index 829ba2d8..de1ebba5 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -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