From 43935c06ba8a10f6a4f7c1d7b7afef4199b19495 Mon Sep 17 00:00:00 2001 From: Aleksandar Ivanov Date: Thu, 1 Mar 2018 00:29:30 +0200 Subject: [PATCH 1/2] Refactor some weird code --- lib/jekyll-archives.rb | 82 ++++++++++------------------------ lib/jekyll-archives/archive.rb | 7 ++- 2 files changed, 30 insertions(+), 59 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index b67ade7..1af0671 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -40,82 +40,48 @@ def generate(site) @site.config["archives"] = @archives end + def enabled?(archive) + @config['enabled'] == true || @config['enabled'] == 'all' || + (@config['enabled'].is_a?(Array) && @config['enabled'].include?(archive)) + end + # Read archive data from posts def read - read_tags - read_categories - read_dates + read_tags if enabled?('tags') + read_categories if enabled?('categories') + read_posts_per_year if enabled?('year') + read_posts_per_month if enabled?('month') + read_posts_per_day if enabled?('day') end def read_tags - if enabled? "tags" - tags.each do |title, posts| - @archives << Archive.new(@site, title, "tag", posts) - end + @archives += @site.post_attr_hash('tags').map do |title, posts| + Archive.new(@site, title, 'tag', posts) end end def read_categories - if enabled? "categories" - categories.each do |title, posts| - @archives << Archive.new(@site, title, "category", posts) - end + @archives += @site.post_attr_hash('categories').map do |title, posts| + Archive.new(@site, title, 'category', posts) 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 + def read_posts_per_year + @archives += @posts.docs.group_by { |p| p.date.year }.map do |year, posts_for_year| + Archive.new(@site, { :year => year }, 'year', posts_for_year.sort.reverse) end end - # 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 } + def read_posts_per_month + @archives += @posts.docs.group_by { |p| [p.date.year, p.date.month] }.map do |(year, month), posts_for_month| + Archive.new(@site, { :year => year, :month => month }, 'month', posts_for_month.sort.reverse) 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 + def read_posts_per_day + @archives += @posts.docs.group_by { |p| [p.date.year, p.date.month, p.date.day] }.map do |(year, month, day), posts_for_day| + Archive.new(@site, { :year => year, :month => month, :day => day }, 'day', posts_for_day.sort.reverse) + end end end end diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index b5f9492..97c91a7 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -68,7 +68,12 @@ def layout # desired placeholder replacements. For details see "url.rb". def url_placeholders if @title.is_a? Hash - @title.merge(:type => @type) + { + :year => @title[:year].to_s, + :month => @title[:month].to_s.rjust(2, '0'), + :day => @title[:day].to_s.rjust(2, '0'), + :type => @type + } else { :name => @slug, :type => @type } end From 6de88d5b0790e35276c141a7fb00eec262978acc Mon Sep 17 00:00:00 2001 From: Aleksandar Ivanov Date: Thu, 1 Mar 2018 00:37:11 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A4=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/jekyll-archives.rb | 28 ++++++++++++++-------------- lib/jekyll-archives/archive.rb | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 1af0671..1c5a5e5 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -41,46 +41,46 @@ def generate(site) end def enabled?(archive) - @config['enabled'] == true || @config['enabled'] == 'all' || - (@config['enabled'].is_a?(Array) && @config['enabled'].include?(archive)) + @config["enabled"] == true || @config["enabled"] == "all" || + (@config["enabled"].is_a?(Array) && @config["enabled"].include?(archive)) end # Read archive data from posts def read - read_tags if enabled?('tags') - read_categories if enabled?('categories') - read_posts_per_year if enabled?('year') - read_posts_per_month if enabled?('month') - read_posts_per_day if enabled?('day') + read_tags if enabled?("tags") + read_categories if enabled?("categories") + read_posts_per_year if enabled?("year") + read_posts_per_month if enabled?("month") + read_posts_per_day if enabled?("day") end def read_tags - @archives += @site.post_attr_hash('tags').map do |title, posts| - Archive.new(@site, title, 'tag', posts) + @archives += @site.post_attr_hash("tags").map do |title, posts| + Archive.new(@site, title, "tag", posts) end end def read_categories - @archives += @site.post_attr_hash('categories').map do |title, posts| - Archive.new(@site, title, 'category', posts) + @archives += @site.post_attr_hash("categories").map do |title, posts| + Archive.new(@site, title, "category", posts) end end def read_posts_per_year @archives += @posts.docs.group_by { |p| p.date.year }.map do |year, posts_for_year| - Archive.new(@site, { :year => year }, 'year', posts_for_year.sort.reverse) + Archive.new(@site, { :year => year }, "year", posts_for_year.sort.reverse) end end def read_posts_per_month @archives += @posts.docs.group_by { |p| [p.date.year, p.date.month] }.map do |(year, month), posts_for_month| - Archive.new(@site, { :year => year, :month => month }, 'month', posts_for_month.sort.reverse) + Archive.new(@site, { :year => year, :month => month }, "month", posts_for_month.sort.reverse) end end def read_posts_per_day @archives += @posts.docs.group_by { |p| [p.date.year, p.date.month, p.date.day] }.map do |(year, month, day), posts_for_day| - Archive.new(@site, { :year => year, :month => month, :day => day }, 'day', posts_for_day.sort.reverse) + Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts_for_day.sort.reverse) end end end diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index 97c91a7..74fd2a8 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -69,10 +69,10 @@ def layout def url_placeholders if @title.is_a? Hash { - :year => @title[:year].to_s, - :month => @title[:month].to_s.rjust(2, '0'), - :day => @title[:day].to_s.rjust(2, '0'), - :type => @type + :year => @title[:year].to_s, + :month => @title[:month].to_s.rjust(2, "0"), + :day => @title[:day].to_s.rjust(2, "0"), + :type => @type, } else { :name => @slug, :type => @type }