Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support revised documents/collections in Jekyll 3 (v2) #53

Merged
merged 4 commits into from
Oct 30, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,42 +112,24 @@ def write
end
end

# Construct a Hash of Posts indexed by the specified Post attribute.
#
# post_attr - The String name of the Post attribute.
#
# Examples
#
# post_attr_hash('categories')
# # => { 'tech' => [<Post A>, <Post B>],
# # 'ruby' => [<Post B>] }
#
# Returns the Hash: { attr => posts } where
# attr - One of the values for the requested attribute.
# posts - The Array of Posts with the given attr value.
#
# Taken from jekyll/jekyll (Copyright (c) 2014 Tom Preston-Werner under the MIT).
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 { |h, key| h[key] = [] }
@posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def tags
post_attr_hash('tags')
@site.post_attr_hash('tags')
end

def categories
post_attr_hash('categories')
@site.post_attr_hash('categories')
end

# Custom `post_attr_hash` method for years
def years
hash = Hash.new { |h, key| h[key] = [] }
@posts.each { |p| hash[p.date.strftime("%Y")] << p }

# In Jekyll 3, Collection#each should be called on the #docs array directly.
if Jekyll::VERSION >= '3.0.0'
@posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
else
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
end
hash.values.each { |posts| posts.sort!.reverse! }
hash
end
Expand Down