@@ -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
0 commit comments