From e5ca5bfc1d48d4a3d2f9ffc6e09e7c661793d6f7 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Fri, 20 Jan 2017 00:19:52 +0000 Subject: [PATCH 1/2] Proof of concept for collections support --- lib/jekyll-archives.rb | 74 ++++++++++++++++++++++++---------- lib/jekyll-archives/archive.rb | 1 + 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 0176594..373f129 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -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 @@ -100,40 +101,69 @@ def write 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 } + if Jekyll::VERSION >= '3.0.0' + 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 diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index 74769e1..62a8b25 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -168,6 +168,7 @@ def regenerate? def inspect "#" end + end end end From 5d466f07ec3df5ecfeed54c36c12247bc8d2082c Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Fri, 3 Feb 2017 21:46:27 +0000 Subject: [PATCH 2/2] Appease Rubocop --- lib/jekyll-archives.rb | 34 +++++++++++++++++----------------- lib/jekyll-archives/archive.rb | 31 +++++++++++++++++++------------ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index c7a76ed..a671110 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -33,10 +33,10 @@ def generate(site) @site = site # Archive the posts collection by default - @site.posts.metadata['archive'] = true + @site.posts.metadata["archive"] = true @archives = [] - @site.config['jekyll-archives'] = @config + @site.config["jekyll-archives"] = @config read @site.pages.concat(@archives) @@ -82,7 +82,7 @@ def read_dates def enabled?(archive) @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array @config["enabled"].include? archive - end + end end def date_attr_hash(date_format, date_posts) @@ -100,14 +100,14 @@ def doc_attr_hash(doc_attr) hash = Hash.new { |h, key| h[key] = [] } @site.collections.each do |name, collection| - if collection.metadata['archive'] + 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' + 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 @@ -119,24 +119,24 @@ def doc_attr_hash(doc_attr) end def tags - if Jekyll::VERSION >= '3.0.0' - doc_attr_hash('tags') + if Jekyll::VERSION >= "3.0.0" + doc_attr_hash("tags") else - @site.post_attr_hash('tags') + @site.post_attr_hash("tags") end end def categories - if Jekyll::VERSION >= '3.0.0' - doc_attr_hash('categories') + if Jekyll::VERSION >= "3.0.0" + doc_attr_hash("categories") else - @site.post_attr_hash('categories') + @site.post_attr_hash("categories") end end def years - if Jekyll::VERSION >= '3.0.0' - doc_attr_hash('years') + if Jekyll::VERSION >= "3.0.0" + doc_attr_hash("years") else date_attr_hash("%Y", @site.posts) end diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index f5103ca..fce2fb5 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -120,7 +128,6 @@ def relative_path def inspect "#" end - end end end