lib/jekyll-plugin-gkarchive.rb in jekyll-plugin-gkarchive-0.2.0 vs lib/jekyll-plugin-gkarchive.rb in jekyll-plugin-gkarchive-0.3.0

- old
+ new

@@ -4,128 +4,72 @@ # # Description: Automatically generate post archives by dates, tags, and categories. # # Download: https://github.com/jekyll/jekyll-archives # +# Sample Config +# jekyll-archives: +# layout: hc-archive +# category: +# - account-settings +# - deployment +# permalinks: +# category: '/hc-category/:name/' +# # See the documentation for full configuration and usage instructions. # frozen_string_literal: true require "jekyll" module Jekyll module Archives # Internal requires - autoload :Archive, "jekyll-plugin-gkarchive/archive" - autoload :VERSION, "jekyll-plugin-gkarchive/version" + # autoload :Archive, "jekyll-archives/archive" + # autoload :VERSION, "jekyll-plugin-gkarchive/version" class Archives < Jekyll::Generator safe true DEFAULTS = { "layout" => "archive", - "enabled" => [], + "categories" => [], "permalinks" => { - "year" => "/:year/", - "month" => "/:year/:month/", - "day" => "/:year/:month/:day/", - "tag" => "/tag/:name/", "category" => "/category/:name/", }, }.freeze def initialize(config = nil) @config = Utils.deep_merge_hashes(DEFAULTS, config.fetch("jekyll-archives", {})) end def generate(site) @site = site - @posts = site.collection.docs - @archives = [] + #@posts = site.posts + site.collections.each do |coll_name, coll_data| + if( !coll_data.nil? && coll_name == 'articles') + @posts = coll_data + end + end + @archives = [] @site.config["jekyll-archives"] = @config - read + read_categories @site.pages.concat(@archives) @site.config["archives"] = @archives end - # Read archive data from posts - def read - read_tags - read_categories - read_dates - end - def read_tags - if enabled? "tags" - tags.each do |title, posts| - @archives << Archive.new(@site, title, "tag", posts) - end - end - end - def read_categories - if enabled? "categories" - categories.each do |title, posts| - @archives << Archive.new(@site, title, "category", posts) - end - end - end - def read_dates - years.each do |year, posts| - @archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year" - months(posts).each do |month, posts| - @archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month" - days(posts).each do |day, posts| - @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day" - end - end + posts = [] + @posts.docs.each do |key, value| + posts << key end - end + @config["category"].each { |cat| @archives << Archive.new(@site, cat, "category", posts) } - # Checks if archive type is enabled in config - def enabled?(archive) - @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array - @config["enabled"].include? archive - end end - def tags - @site.post_attr_hash("tags") - end - - def categories - @site.post_attr_hash("categories") - end - - # Custom `post_attr_hash` method for years - def years - hash = Hash.new { |h, key| h[key] = [] } - - # In Jekyll 3, Collection#each should be called on the #docs array directly. - if Jekyll::VERSION >= "3.0.0" - @posts.docs.each { |p| hash[p.date.strftime("%Y")] << p } - else - @posts.each { |p| hash[p.date.strftime("%Y")] << p } - end - hash.each_value { |posts| posts.sort!.reverse! } - hash - end - - def months(year_posts) - hash = Hash.new { |h, key| h[key] = [] } - year_posts.each { |p| hash[p.date.strftime("%m")] << p } - hash.each_value { |posts| posts.sort!.reverse! } - hash - end - - def days(month_posts) - hash = Hash.new { |h, key| h[key] = [] } - month_posts.each { |p| hash[p.date.strftime("%d")] << p } - hash.each_value { |posts| posts.sort!.reverse! } - hash - end end end end \ No newline at end of file