Added new accessible Liquid attribute for Sites: .topics.

Behaves like the .categories attribute.
This commit is contained in:
Basil Shkara 2008-12-27 11:06:25 +11:00 committed by Tom Preston-Werner
parent 4770882217
commit 4db696152c
1 changed files with 18 additions and 10 deletions

View File

@ -129,20 +129,28 @@ module Jekyll
end
end
# Constructs a hash map of Posts indexed by the specified Post attribute
#
# Returns {post_attr => [<Posts>]}
def post_attr_hash(post_attr)
# Build a hash map based on the specified post attribute ( post attr => array of posts )
# then sort each array in reverse order
hash = Hash.new { |hash, key| hash[key] = Array.new }
self.posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a} }
return hash
end
# The Hash payload containing site-wide data
#
# Returns {"site" => {"time" => <Time>, "posts" => [<Post>]}}
# Returns {"site" => {"time" => <Time>, "posts" => [<Post>], "categories" => [<Categories>], "topics" =>
# [<Topics>] }}
def site_payload
# Build the category hash map of category ( names => arrays of posts )
# then sort each array in reverse order
categories = Hash.new { |hash, key| hash[key] = Array.new }
self.posts.each { |p| p.categories.each { |c| categories[c] << p } }
categories.values.map { |cats| cats.sort! { |a, b| b <=> a} }
{"site" => {
"time" => Time.now,
"posts" => self.posts.sort { |a,b| b <=> a },
"categories" => categories
"categories" => post_attr_hash('categories'),
"topics" => post_attr_hash('topics')
}}
end
end