class Jekyll::Archives::Archives

Constants

DEFAULTS

Public Class Methods

new(config = nil) click to toggle source
# File lib/jekyll-archives.rb, line 26
def initialize(config = nil)
  @config = Utils.deep_merge_hashes(DEFAULTS, config.fetch("jekyll-archives", {}))
end

Public Instance Methods

categories() click to toggle source
# File lib/jekyll-archives.rb, line 89
def categories
  @site.post_attr_hash("categories")
end
days(month_posts) click to toggle source
# File lib/jekyll-archives.rb, line 114
def days(month_posts)
  hash = Hash.new { |h, key| h[key] = [] }
  month_posts.each { |p| hash[p.date.strftime("%d")] << p }
  hash.each_value { |posts| posts.sort!.reverse! }
  hash
end
enabled?(archive) click to toggle source

Checks if archive type is enabled in config

# File lib/jekyll-archives.rb, line 79
def enabled?(archive)
  @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
                                                                 @config["enabled"].include? archive
                                                               end
end
generate(site) click to toggle source
# File lib/jekyll-archives.rb, line 30
def generate(site)
  @site = site
  @posts = site.posts
  @archives = []

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

  read
  @site.pages.concat(@archives)

  @site.config["archives"] = @archives
end
months(year_posts) click to toggle source
# File lib/jekyll-archives.rb, line 107
def months(year_posts)
  hash = Hash.new { |h, key| h[key] = [] }
  year_posts.each { |p| hash[p.date.strftime("%m")] << p }
  hash.each_value { |posts| posts.sort!.reverse! }
  hash
end
read() click to toggle source

Read archive data from posts

# File lib/jekyll-archives.rb, line 44
def read
  read_tags
  read_categories
  read_dates
end
read_categories() click to toggle source
# File lib/jekyll-archives.rb, line 58
def read_categories
  if enabled? "categories"
    categories.each do |title, posts|
      @archives << Archive.new(@site, title, "category", posts)
    end
  end
end
read_dates() click to toggle source
# File lib/jekyll-archives.rb, line 66
def read_dates
  years.each do |year, posts|
    @archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
    months(posts).each do |month, posts|
      @archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
      days(posts).each do |day, posts|
        @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
      end
    end
  end
end
read_tags() click to toggle source
# File lib/jekyll-archives.rb, line 50
def read_tags
  if enabled? "tags"
    tags.each do |title, posts|
      @archives << Archive.new(@site, title, "tag", posts)
    end
  end
end
tags() click to toggle source
# File lib/jekyll-archives.rb, line 85
def tags
  @site.post_attr_hash("tags")
end
years() click to toggle source

Custom `post_attr_hash` method for years

# File lib/jekyll-archives.rb, line 94
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 }
  else
    @posts.each { |p| hash[p.date.strftime("%Y")] << p }
  end
  hash.each_value { |posts| posts.sort!.reverse! }
  hash
end