class Jekyll::ArchivesV2::Archives
def append_enabled_date_type(meta, type, collection_name, documents)
collection_name - 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_name, documents) @archives << Archive.new(@site, meta, type, collection_name, documents) if enabled?(collection_name, type) end
def categories(documents)
def categories(documents) doc_attr_hash(documents, "categories") 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_name, archive)
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
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" => "/#{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
def months(year_documents)
def months(year_documents) date_attr_hash(year_documents, "%m") end
def read(collection_name)
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
def read_categories(collection_name)
def read_categories(collection_name) categories(@collections[collection_name]).each do |title, documents| @archives << Archive.new(@site, title, "category", collection_name, documents) end end
def read_dates(collection_name)
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
def read_tags(collection_name)
def read_tags(collection_name) tags(@collections[collection_name]).each do |title, documents| @archives << Archive.new(@site, title, "tag", collection_name, documents) end end
def tags(documents)
def tags(documents) doc_attr_hash(documents, "tags") end
def years(documents)
def years(documents) date_attr_hash(documents.docs, "%Y") end