Class: Jekyll::ArchivesV2::Archives

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Archives

Returns a new instance of Archives.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-archives-v2.rb', line 15

def initialize(config = {})
  defaults = {}
  config.fetch("collections", {}).each do |name, collection|
    defaults[name] = {
      "layout"     => "archive",
      "enabled"    => [],
      "permalinks" => {
        "year"     => "/#{name}/:year/",
        "month"    => "/#{name}/:year/:month/",
        "day"      => "/#{name}/:year/:month/:day/",
        "tag"      => "/#{name}/tag/:name/",
        "category" => "/#{name}/category/:name/",
      },
    }
  end
  defaults.freeze
  archives_config = config.fetch("jekyll-archives", {})
  if archives_config.is_a?(Hash)
    @config = Utils.deep_merge_hashes(defaults, archives_config)
  else
    @config = nil
    Jekyll.logger.warn "Archives:", "Expected a hash but got #{archives_config.inspect}"
    Jekyll.logger.warn "", "Archives will not be generated for this site."
  end
end

Instance Method Details

#categories(documents) ⇒ Object



107
108
109
# File 'lib/jekyll-archives-v2.rb', line 107

def categories(documents)
  doc_attr_hash(documents, "categories")
end

#days(month_documents) ⇒ Object

Custom ‘post_attr_hash` method for days



122
123
124
# File 'lib/jekyll-archives-v2.rb', line 122

def days(month_documents)
  date_attr_hash(month_documents, "%d")
end

#enabled?(collection_name, archive) ⇒ Boolean

Checks if archive type is enabled in config

Returns:

  • (Boolean)


99
100
101
# File 'lib/jekyll-archives-v2.rb', line 99

def enabled?(collection_name, archive)
  @config[collection_name]["enabled"] == true || @config[collection_name]["enabled"] == "all" || (@config[collection_name]["enabled"].is_a?(Array) && @config[collection_name]["enabled"].include?(archive))
end

#generate(site) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll-archives-v2.rb', line 41

def generate(site)
  return if @config.nil?

  @site = site
  @collections = site.collections
  @archives = []

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

  # loop through collections keys and read them
  @config.each do |collection_name, collection_config|
    read(collection_name)
  end

  @site.pages.concat(@archives)
  @site.config["archives"] = @archives
end

#months(year_documents) ⇒ Object

Custom ‘post_attr_hash` method for months



117
118
119
# File 'lib/jekyll-archives-v2.rb', line 117

def months(year_documents)
  date_attr_hash(year_documents, "%m")
end

#read(collection_name) ⇒ Object

Read archive data from collection



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll-archives-v2.rb', line 60

def read(collection_name)
  if enabled?(collection_name, "tags")
    read_tags(collection_name)
  end

  if enabled?(collection_name, "categories")
    read_categories(collection_name)
  end

  if enabled?(collection_name, "year") || enabled?(collection_name, "month") || enabled?(collection_name, "day")
    read_dates(collection_name)
  end
end

#read_categories(collection_name) ⇒ Object



80
81
82
83
84
# File 'lib/jekyll-archives-v2.rb', line 80

def read_categories(collection_name)
  categories(@collections[collection_name]).each do |title, documents|
    @archives << Archive.new(@site, title, "category", collection_name, documents)
  end
end

#read_dates(collection_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jekyll-archives-v2.rb', line 86

def read_dates(collection_name)
  years(@collections[collection_name]).each do |year, y_documents|
    append_enabled_date_type({ :year => year }, "year", collection_name, y_documents)
    months(y_documents).each do |month, m_documents|
      append_enabled_date_type({ :year => year, :month => month }, "month", collection_name, m_documents)
      days(m_documents).each do |day, d_documents|
        append_enabled_date_type({ :year => year, :month => month, :day => day }, "day", collection_name, d_documents)
      end
    end
  end
end

#read_tags(collection_name) ⇒ Object



74
75
76
77
78
# File 'lib/jekyll-archives-v2.rb', line 74

def read_tags(collection_name)
  tags(@collections[collection_name]).each do |title, documents|
    @archives << Archive.new(@site, title, "tag", collection_name, documents)
  end
end

#tags(documents) ⇒ Object



103
104
105
# File 'lib/jekyll-archives-v2.rb', line 103

def tags(documents)
  doc_attr_hash(documents, "tags")
end

#years(documents) ⇒ Object

Custom ‘post_attr_hash` method for years



112
113
114
# File 'lib/jekyll-archives-v2.rb', line 112

def years(documents)
  date_attr_hash(documents, "%Y")
end