class Jekyll::ArchivesV2::Archives
def append_enabled_date_type(meta, type, collection, documents)
collection - the name of the collection
type - The type of date archive.
meta - A Hash of the year / month / day as applicable for date.
has been enabled by configuration.
Initialize a new Archive page and append to base array if the associated date `type`
def append_enabled_date_type(meta, type, collection, documents) @archives << Archive.new(@site, meta, type, collection, documents) if enabled?(collection, type) end
def date_attr_hash(documents, id)
documents - Array of documents to be considered for archiving.
Custom `post_attr_hash` for date type archives.
def date_attr_hash(documents, id) hash = Hash.new { |hsh, key| hsh[key] = [] } documents.each { |document| hash[document.date.strftime(id)] << document } hash.each_value { |documents| documents.sort!.reverse! } hash end
def days(month_documents)
def days(month_documents) date_attr_hash(month_documents, "%d") end
def doc_attr_hash(documents, doc_attr)
documents - Array of documents to be considered for archiving.
Custom `post_attr_hash` for any collection.
def doc_attr_hash(documents, doc_attr) # Build a hash map based on the specified document attribute ( doc_attr => # array of elements from collection ) then sort each array in reverse order. hash = Hash.new { |h, key| h[key] = [] } documents.docs.each { |document| document.data[doc_attr]&.each { |t| hash[t] << document } } hash.each_value { |documents| documents.sort!.reverse! } hash end
def enabled?(collection, archive)
def enabled?(collection, archive) @config[collection]["enabled"] == true || @config[collection]["enabled"] == "all" || (@config[collection]["enabled"].is_a?(Array) && @config[collection]["enabled"].include?(archive)) end
def generate(site)
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
def initialize(config = {})
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
def months(year_documents)
def months(year_documents) date_attr_hash(year_documents, "%m") end
def read(collection)
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
def read_attrs(collection, attr)
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
def read_dates(collection)
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
def years(documents)
def years(documents) date_attr_hash(documents.docs, "%Y") end