Class: Jekyll::ArchivesV2::Archives

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

Constant Summary collapse

DATE_ENABLES =
%w(year month day).freeze

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Archives

Returns a new instance of Archives.



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 16

def initialize(config = {})
  defaults = {}
  config.fetch("collections", {}).each do |name, _collection|
    defaults[name] = {
      "layout"     => "archive",
      "enabled"    => [],
      "permalinks" => {
        "year"  => "/:collection/:year/",
        "month" => "/:collection/:year/:month/",
        "day"   => "/:collection/:year/:month/:day/",
        "tags"  => "/:collection/:type/: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

#days(month_documents) ⇒ Object

Custom post_attr_hash method for days



130
131
132
# File 'lib/jekyll-archives-v2.rb', line 130

def days(month_documents)
  date_attr_hash(month_documents, "%d")
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



125
126
127
# File 'lib/jekyll-archives-v2.rb', line 125

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

#read(collection) ⇒ Object

Read archive data from collection



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll-archives-v2.rb', line 60

def read(collection)
  if @config[collection]["enabled"].is_a?(Array)
    use_year = @config[collection]["enabled"].include?("year")
    use_month = @config[collection]["enabled"].include?("month")
    use_day = @config[collection]["enabled"].include?("day")

    if use_year || use_month || use_day
      read_dates(collection, :use_year => use_year, :use_month => use_month, :use_day => use_day)
    end

    # read all attributes that are not year, month, or day
    attributes = @config[collection]["enabled"].reject { |attr| DATE_ENABLES.include?(attr) }

    attributes.each do |attr|
      read_attrs(collection, attr)
    end

  elsif @config[collection]["enabled"] == true || @config[collection]["enabled"] == "all"
    read_dates(collection, :use_year => true, :use_month => true, :use_day => true)

    # create a list of all attributes
    attributes = @collections[collection].docs.flat_map { |doc| doc.data.keys }.uniq
    # discard any attribute that is not an array
    attributes.reject! { |attr| @collections[collection].docs.all? { |doc| !doc.data[attr].is_a?(Array) } }

    attributes.each do |attr|
      read_attrs(collection, attr)
    end
  end
end

#read_attrs(collection, attr) ⇒ Object



91
92
93
94
95
# File 'lib/jekyll-archives-v2.rb', line 91

def read_attrs(collection, attr)
  doc_attr_hash(@collections[collection], attr).each do |title, documents|
    @archives << Archive.new(@site, title, attr, collection, documents)
  end
end

#read_dates(collection, use_year: false, use_month: false, use_day: false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jekyll-archives-v2.rb', line 97

def read_dates(collection, use_year: false, use_month: false, use_day: false)
  years(@collections[collection]).each do |year, y_documents|
    if use_year
      @archives << Archive.new(@site, { :year => year }, "year", collection, y_documents)
    end

    next unless use_month || use_day

    months(y_documents).each do |month, m_documents|
      if use_month
        @archives << Archive.new(@site, { :year => year, :month => month }, "month", collection, m_documents)
      end

      next unless use_day

      days(m_documents).each do |day, d_documents|
        @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", collection, d_documents)
      end
    end
  end
end

#years(documents) ⇒ Object

Custom post_attr_hash method for years



120
121
122
# File 'lib/jekyll-archives-v2.rb', line 120

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