Skip to content

Proof of concept: Collections support #88

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
72 changes: 51 additions & 21 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def initialize(config = nil)

def generate(site)
@site = site
@posts = site.posts
@archives = []

# Archive the posts collection by default
@site.posts.metadata["archive"] = true

@archives = []
@site.config["jekyll-archives"] = @config

read
@site.pages.concat(@archives)

@site.config["archives"] = @archives
end

Expand Down Expand Up @@ -81,43 +82,72 @@ def read_dates
def enabled?(archive)
@config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
@config["enabled"].include? archive
end
end

def date_attr_hash(date_format, date_posts)
hash = Hash.new { |h, key| h[key] = [] }

date_posts.each do |p|
hash[p.date.strftime(date_format)] << p
end

hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def doc_attr_hash(doc_attr)
hash = Hash.new { |h, key| h[key] = [] }

@site.collections.each do |name, collection|
if collection.metadata["archive"]
collection.docs.each do |d|
case doc_attr
when "tags"
d.data["tags"].each { |t| hash[t] << d } if d.data["tags"]
when "categories"
d.data["categories"].each { |t| hash[t] << d } if d.data["categories"]
when "years"
hash[d.date.strftime("%Y")] << d
end
end
end
end

hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def tags
@site.post_attr_hash("tags")
if Jekyll::VERSION >= "3.0.0"
doc_attr_hash("tags")
else
@site.post_attr_hash("tags")
end
end

def categories
@site.post_attr_hash("categories")
if Jekyll::VERSION >= "3.0.0"
doc_attr_hash("categories")
else
@site.post_attr_hash("categories")
end
end

# Custom `post_attr_hash` method for years
def years
hash = Hash.new { |h, key| h[key] = [] }

# 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 }
doc_attr_hash("years")
else
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
date_attr_hash("%Y", @site.posts)
end
hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def months(year_posts)
hash = Hash.new { |h, key| h[key] = [] }
year_posts.each { |p| hash[p.date.strftime("%m")] << p }
hash.values.each { |posts| posts.sort!.reverse! }
hash
date_attr_hash("%m", year_posts)
end

def days(month_posts)
hash = Hash.new { |h, key| h[key] = [] }
month_posts.each { |p| hash[p.date.strftime("%d")] << p }
hash.values.each { |posts| posts.sort!.reverse! }
hash
date_attr_hash("%d", month_posts)
end
end
end
Expand Down
30 changes: 19 additions & 11 deletions lib/jekyll-archives/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class Archive < Jekyll::Page
# Initialize a new Archive page
#
# site - The Site object.
# title - The name of the tag/category or a Hash of the year/month/day in case of date.
# e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
# type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
# posts - The array of posts that belong in this archive.
# title - The name of the tag/category or a Hash of
# the year/month/day in case of date.
# e.g. { :year => 2014, :month => 08 } or
# "my-category" or "my-tag".
# type - The type of archive. Can be one of "year",
# "month", "day", "category", or "tag"
# posts - The array of posts that belong in this
# archive.
def initialize(site, title, type, posts)
@site = site
@posts = posts
Expand Down Expand Up @@ -62,8 +66,9 @@ def layout
end
end

# Returns a hash of URL placeholder names (as symbols) mapping to the
# desired placeholder replacements. For details see "url.rb".
# Returns a hash of URL placeholder names (as symbols)
# mapping to the desired placeholder replacements.
# For details see "url.rb".
def url_placeholders
if @title.is_a? Hash
@title.merge(:type => @type)
Expand All @@ -72,7 +77,8 @@ def url_placeholders
end
end

# The generated relative url of this page. e.g. /about.html.
# The generated relative url of this page. e.g.
# /about.html.
#
# Returns the String url.
def url
Expand All @@ -89,10 +95,11 @@ def permalink
data && data.is_a?(Hash) && data["permalink"]
end

# Produce a title object suitable for Liquid based on type of archive.
# Produce a title object suitable for Liquid based on
# type of archive.
#
# Returns a String (for tag and category archives) and nil for
# date-based archives.
# Returns a String (for tag and category archives) and
# nil for date-based archives.
def title
@title if @title.is_a? String
end
Expand All @@ -107,7 +114,8 @@ def date
end
end

# Obtain the write path relative to the destination directory
# Obtain the write path relative to the destination
# directory.
#
# Returns the destination relative path String.
def relative_path
Expand Down