Class: Jekyll::Archives::Archives

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-archives.rb

Constant Summary collapse

DEFAULTS =
{
  "layout"     => "archive",
  "enabled"    => [],
  "permalinks" => {
    "year"     => "/:year/",
    "month"    => "/:year/:month/",
    "day"      => "/:year/:month/:day/",
    "tag"      => "/tag/:name/",
    "category" => "/category/:name/",
  },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Archives

Returns a new instance of Archives.



26
27
28
# File 'lib/jekyll-archives.rb', line 26

def initialize(config = nil)
  @config = Utils.deep_merge_hashes(DEFAULTS, config.fetch("jekyll-archives", {}))
end

Instance Method Details

#categoriesObject



89
90
91
# File 'lib/jekyll-archives.rb', line 89

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

#days(month_posts) ⇒ Object



114
115
116
117
118
119
# 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) ⇒ Boolean

Checks if archive type is enabled in config

Returns:

  • (Boolean)


79
80
81
82
83
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



107
108
109
110
111
112
# 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

#readObject

Read archive data from posts



44
45
46
47
48
# File 'lib/jekyll-archives.rb', line 44

def read
  read_tags
  read_categories
  read_dates
end

#read_categoriesObject



58
59
60
61
62
63
64
# 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_datesObject



66
67
68
69
70
71
72
73
74
75
76
# 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_tagsObject



50
51
52
53
54
55
56
# 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

#tagsObject



85
86
87
# File 'lib/jekyll-archives.rb', line 85

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

#yearsObject

Custom ‘post_attr_hash` method for years



94
95
96
97
98
99
100
101
102
103
104
105
# 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