Skip to content

Commit 43935c0

Browse files
committed
Refactor some weird code
1 parent c832738 commit 43935c0

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

lib/jekyll-archives.rb

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,82 +40,48 @@ def generate(site)
4040
@site.config["archives"] = @archives
4141
end
4242

43+
def enabled?(archive)
44+
@config['enabled'] == true || @config['enabled'] == 'all' ||
45+
(@config['enabled'].is_a?(Array) && @config['enabled'].include?(archive))
46+
end
47+
4348
# Read archive data from posts
4449
def read
45-
read_tags
46-
read_categories
47-
read_dates
50+
read_tags if enabled?('tags')
51+
read_categories if enabled?('categories')
52+
read_posts_per_year if enabled?('year')
53+
read_posts_per_month if enabled?('month')
54+
read_posts_per_day if enabled?('day')
4855
end
4956

5057
def read_tags
51-
if enabled? "tags"
52-
tags.each do |title, posts|
53-
@archives << Archive.new(@site, title, "tag", posts)
54-
end
58+
@archives += @site.post_attr_hash('tags').map do |title, posts|
59+
Archive.new(@site, title, 'tag', posts)
5560
end
5661
end
5762

5863
def read_categories
59-
if enabled? "categories"
60-
categories.each do |title, posts|
61-
@archives << Archive.new(@site, title, "category", posts)
62-
end
64+
@archives += @site.post_attr_hash('categories').map do |title, posts|
65+
Archive.new(@site, title, 'category', posts)
6366
end
6467
end
6568

66-
def read_dates
67-
years.each do |year, posts|
68-
@archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
69-
months(posts).each do |month, posts|
70-
@archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
71-
days(posts).each do |day, posts|
72-
@archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
73-
end
74-
end
69+
def read_posts_per_year
70+
@archives += @posts.docs.group_by { |p| p.date.year }.map do |year, posts_for_year|
71+
Archive.new(@site, { :year => year }, 'year', posts_for_year.sort.reverse)
7572
end
7673
end
7774

78-
# Checks if archive type is enabled in config
79-
def enabled?(archive)
80-
@config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
81-
@config["enabled"].include? archive
82-
end
83-
end
84-
85-
def tags
86-
@site.post_attr_hash("tags")
87-
end
88-
89-
def categories
90-
@site.post_attr_hash("categories")
91-
end
92-
93-
# Custom `post_attr_hash` method for years
94-
def years
95-
hash = Hash.new { |h, key| h[key] = [] }
96-
97-
# In Jekyll 3, Collection#each should be called on the #docs array directly.
98-
if Jekyll::VERSION >= "3.0.0"
99-
@posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
100-
else
101-
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
75+
def read_posts_per_month
76+
@archives += @posts.docs.group_by { |p| [p.date.year, p.date.month] }.map do |(year, month), posts_for_month|
77+
Archive.new(@site, { :year => year, :month => month }, 'month', posts_for_month.sort.reverse)
10278
end
103-
hash.each_value { |posts| posts.sort!.reverse! }
104-
hash
10579
end
10680

107-
def months(year_posts)
108-
hash = Hash.new { |h, key| h[key] = [] }
109-
year_posts.each { |p| hash[p.date.strftime("%m")] << p }
110-
hash.each_value { |posts| posts.sort!.reverse! }
111-
hash
112-
end
113-
114-
def days(month_posts)
115-
hash = Hash.new { |h, key| h[key] = [] }
116-
month_posts.each { |p| hash[p.date.strftime("%d")] << p }
117-
hash.each_value { |posts| posts.sort!.reverse! }
118-
hash
81+
def read_posts_per_day
82+
@archives += @posts.docs.group_by { |p| [p.date.year, p.date.month, p.date.day] }.map do |(year, month, day), posts_for_day|
83+
Archive.new(@site, { :year => year, :month => month, :day => day }, 'day', posts_for_day.sort.reverse)
84+
end
11985
end
12086
end
12187
end

lib/jekyll-archives/archive.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ def layout
6868
# desired placeholder replacements. For details see "url.rb".
6969
def url_placeholders
7070
if @title.is_a? Hash
71-
@title.merge(:type => @type)
71+
{
72+
:year => @title[:year].to_s,
73+
:month => @title[:month].to_s.rjust(2, '0'),
74+
:day => @title[:day].to_s.rjust(2, '0'),
75+
:type => @type
76+
}
7277
else
7378
{ :name => @slug, :type => @type }
7479
end

0 commit comments

Comments
 (0)