Class: Jekyll::ArchivesV2::Archives
- Inherits:
-
Generator
- Object
- Generator
- Jekyll::ArchivesV2::Archives
- Defined in:
- lib/jekyll-archives-v2.rb
Instance Method Summary collapse
-
#days(month_documents) ⇒ Object
Custom ‘post_attr_hash` method for days.
-
#enabled?(collection, archive) ⇒ Boolean
Checks if archive type is enabled in config.
- #generate(site) ⇒ Object
-
#initialize(config = {}) ⇒ Archives
constructor
A new instance of Archives.
-
#months(year_documents) ⇒ Object
Custom ‘post_attr_hash` method for months.
-
#read(collection) ⇒ Object
Read archive data from collection.
- #read_attrs(collection, attr) ⇒ Object
- #read_dates(collection) ⇒ Object
-
#years(documents) ⇒ Object
Custom ‘post_attr_hash` method for years.
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 |
# 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" => "/: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
120 121 122 |
# File 'lib/jekyll-archives-v2.rb', line 120 def days(month_documents) date_attr_hash(month_documents, "%d") end |
#enabled?(collection, archive) ⇒ Boolean
Checks if archive type is enabled in config
105 106 107 |
# File 'lib/jekyll-archives-v2.rb', line 105 def enabled?(collection, archive) @config[collection]["enabled"] == true || @config[collection]["enabled"] == "all" || (@config[collection]["enabled"].is_a?(Array) && @config[collection]["enabled"].include?(archive)) end |
#generate(site) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jekyll-archives-v2.rb', line 40 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
115 116 117 |
# File 'lib/jekyll-archives-v2.rb', line 115 def months(year_documents) date_attr_hash(year_documents, "%m") end |
#read(collection) ⇒ Object
Read archive data from collection
59 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 |
# File 'lib/jekyll-archives-v2.rb', line 59 def read(collection) if @config[collection]["enabled"].is_a?(Array) if enabled?(collection, "year") || enabled?(collection, "month") || enabled?(collection, "day") read_dates(collection) end # read all attributes that are not year, month, or day attributes = @config[collection]["enabled"].select { |attr| !["year", "month", "day"].include?(attr) } attributes.each do |attr| read_attrs(collection, attr) end elsif @config[collection]["enabled"] == true || @config[collection]["enabled"] == "all" read_dates(collection) # 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
86 87 88 89 90 |
# File 'lib/jekyll-archives-v2.rb', line 86 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jekyll-archives-v2.rb', line 92 def read_dates(collection) years(@collections[collection]).each do |year, y_documents| append_enabled_date_type({ :year => year }, "year", collection, y_documents) months(y_documents).each do |month, m_documents| append_enabled_date_type({ :year => year, :month => month }, "month", collection, m_documents) days(m_documents).each do |day, d_documents| append_enabled_date_type({ :year => year, :month => month, :day => day }, "day", collection, d_documents) end end end end |
#years(documents) ⇒ Object
Custom ‘post_attr_hash` method for years
110 111 112 |
# File 'lib/jekyll-archives-v2.rb', line 110 def years(documents) date_attr_hash(documents.docs, "%Y") end |