diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 18cde72..a671110 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 @@ -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 diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index 66585bf..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