Merge branch 'fix_tags_and_categories_issues_73_and_84' into updates_20100110
This commit is contained in:
commit
db03bcac8f
|
@ -3,6 +3,10 @@
|
|||
#
|
||||
# Requires
|
||||
# self.site -> Jekyll::Site
|
||||
# self.content=
|
||||
# self.data=
|
||||
# self.ext=
|
||||
# self.output=
|
||||
module Jekyll
|
||||
module Convertible
|
||||
# Return the contents as a string
|
||||
|
|
|
@ -19,6 +19,28 @@ class Hash
|
|||
|
||||
target
|
||||
end
|
||||
|
||||
# Read array from the supplied hash favouring the singular key
|
||||
# and then the plural key, and handling any nil entries.
|
||||
# +hash+ the hash to read from
|
||||
# +singular_key+ the singular key
|
||||
# +plural_key+ the singular key
|
||||
#
|
||||
# Returns an array
|
||||
def pluralized_array(singular_key, plural_key)
|
||||
hash = self
|
||||
if hash.has_key?(singular_key)
|
||||
array = [hash[singular_key]] if hash[singular_key]
|
||||
elsif hash.has_key?(plural_key)
|
||||
case hash[plural_key]
|
||||
when String
|
||||
array = hash[plural_key].split
|
||||
when Array
|
||||
array = hash[plural_key].compact
|
||||
end
|
||||
end
|
||||
array || []
|
||||
end
|
||||
end
|
||||
|
||||
# Thanks, ActiveSupport!
|
||||
|
|
|
@ -18,12 +18,9 @@ module Jekyll
|
|||
name =~ MATCHER
|
||||
end
|
||||
|
||||
attr_accessor :site, :date, :slug, :ext, :published, :data, :content, :output, :tags
|
||||
attr_writer :categories
|
||||
|
||||
def categories
|
||||
@categories ||= []
|
||||
end
|
||||
attr_accessor :site
|
||||
attr_accessor :data, :content, :output, :ext
|
||||
attr_accessor :date, :slug, :published, :tags, :categories
|
||||
|
||||
# Initialize this Post instance.
|
||||
# +site+ is the Site
|
||||
|
@ -47,26 +44,10 @@ module Jekyll
|
|||
self.published = true
|
||||
end
|
||||
|
||||
if self.data.has_key?("tag")
|
||||
self.tags = [self.data["tag"]]
|
||||
elsif self.data.has_key?("tags")
|
||||
self.tags = self.data['tags']
|
||||
else
|
||||
self.tags = []
|
||||
end
|
||||
self.tags = self.data.pluralized_array("tag", "tags")
|
||||
|
||||
if self.categories.empty?
|
||||
if self.data.has_key?('category')
|
||||
self.categories << self.data['category']
|
||||
elsif self.data.has_key?('categories')
|
||||
# Look for categories in the YAML-header, either specified as
|
||||
# an array or a string.
|
||||
if self.data['categories'].kind_of? String
|
||||
self.categories = self.data['categories'].split
|
||||
else
|
||||
self.categories = self.data['categories']
|
||||
end
|
||||
end
|
||||
self.categories = self.data.pluralized_array('category', 'categories')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
layout: default
|
||||
title: Category in YAML
|
||||
categories:
|
||||
---
|
||||
|
||||
Best *post* ever
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
layout: default
|
||||
title: Category in YAML
|
||||
category:
|
||||
---
|
||||
|
||||
Best *post* ever
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: A Tag
|
||||
tag:
|
||||
---
|
||||
|
||||
Whoa.
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: Some Tags
|
||||
tags:
|
||||
---
|
||||
|
||||
Awesome!
|
|
@ -0,0 +1,66 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
class TestCoreExt < Test::Unit::TestCase
|
||||
context "hash" do
|
||||
|
||||
context "pluralized_array" do
|
||||
|
||||
should "return empty array with no values" do
|
||||
data = {}
|
||||
assert_equal [], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return empty array with no matching values" do
|
||||
data = { 'foo' => 'bar' }
|
||||
assert_equal [], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return empty array with matching nil singular" do
|
||||
data = { 'foo' => 'bar', 'tag' => nil, 'tags' => ['dog', 'cat'] }
|
||||
assert_equal [], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return single value array with matching singular" do
|
||||
data = { 'foo' => 'bar', 'tag' => 'dog', 'tags' => ['dog', 'cat'] }
|
||||
assert_equal ['dog'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return single value array with matching singular with spaces" do
|
||||
data = { 'foo' => 'bar', 'tag' => 'dog cat', 'tags' => ['dog', 'cat'] }
|
||||
assert_equal ['dog cat'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return empty array with matching nil plural" do
|
||||
data = { 'foo' => 'bar', 'tags' => nil }
|
||||
assert_equal [], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return empty array with matching empty array" do
|
||||
data = { 'foo' => 'bar', 'tags' => [] }
|
||||
assert_equal [], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return single value array with matching plural with single string value" do
|
||||
data = { 'foo' => 'bar', 'tags' => 'dog' }
|
||||
assert_equal ['dog'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return multiple value array with matching plural with single string value with spaces" do
|
||||
data = { 'foo' => 'bar', 'tags' => 'dog cat' }
|
||||
assert_equal ['dog', 'cat'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return single value array with matching plural with single value array" do
|
||||
data = { 'foo' => 'bar', 'tags' => ['dog'] }
|
||||
assert_equal ['dog'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
should "return multiple value array with matching plural with multiple value array" do
|
||||
data = { 'foo' => 'bar', 'tags' => ['dog', 'cat'] }
|
||||
assert_equal ['dog', 'cat'], data.pluralized_array('tag', 'tags')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "ensure post count is as expected" do
|
||||
assert_equal 18, @site.posts.size
|
||||
assert_equal 22, @site.posts.size
|
||||
end
|
||||
|
||||
should "insert site.posts into the index" do
|
||||
|
|
|
@ -36,6 +36,7 @@ class TestPost < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "keep date, title, and markup type" do
|
||||
@post.categories = []
|
||||
@post.process(@fake_file)
|
||||
|
||||
assert_equal Time.parse("2008-10-19"), @post.date
|
||||
|
@ -236,6 +237,16 @@ class TestPost < Test::Unit::TestCase
|
|||
assert post.categories.include?('baz')
|
||||
end
|
||||
|
||||
should "recognize empty category in yaml" do
|
||||
post = setup_post("2009-01-27-empty-category.textile")
|
||||
assert_equal [], post.categories
|
||||
end
|
||||
|
||||
should "recognize empty categories in yaml" do
|
||||
post = setup_post("2009-01-27-empty-categories.textile")
|
||||
assert_equal [], post.categories
|
||||
end
|
||||
|
||||
should "recognize tag in yaml" do
|
||||
post = setup_post("2009-05-18-tag.textile")
|
||||
assert post.tags.include?('code')
|
||||
|
@ -248,6 +259,16 @@ class TestPost < Test::Unit::TestCase
|
|||
assert post.tags.include?('pizza')
|
||||
end
|
||||
|
||||
should "recognize empty tag in yaml" do
|
||||
post = setup_post("2009-05-18-empty-tag.textile")
|
||||
assert_equal [], post.tags
|
||||
end
|
||||
|
||||
should "recognize empty tags in yaml" do
|
||||
post = setup_post("2009-05-18-empty-tags.textile")
|
||||
assert_equal [], post.tags
|
||||
end
|
||||
|
||||
should "allow no yaml" do
|
||||
post = setup_post("2009-06-22-no-yaml.textile")
|
||||
assert_equal "No YAML.", post.content
|
||||
|
|
Loading…
Reference in New Issue