Skip to content

Commit b62fa76

Browse files
committed
Add support for months and days
1 parent 9bf6a90 commit b62fa76

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

lib/jekyll-archives.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Archives < Jekyll::Generator
1010
DEFAULTS = {
1111
'layout' => 'archive',
1212
'permalinks' => {
13-
'year' => '/archive/:name/',
13+
'year' => '/:year/',
14+
'month' => '/:year/:month/',
15+
'day' => '/:year/:month/:day/',
1416
'tag' => '/tag/:name/',
1517
'category' => '/category/:name/'
1618
}
@@ -50,8 +52,14 @@ def read
5052
categories.each do |name, posts|
5153
@archives << Archive.new(@site, name, "category", posts)
5254
end
53-
years.each do |name, posts|
54-
@archives << Archive.new(@site, name, "year", posts)
55+
years.each do |year, posts|
56+
@archives << Archive.new(@site, { :year => year }, "year", posts)
57+
months(posts).each do |month, posts|
58+
@archives << Archive.new(@site, { :year => year, :month => month }, "month", posts)
59+
days(posts).each do |day, posts|
60+
@archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts)
61+
end
62+
end
5563
end
5664
end
5765

@@ -105,7 +113,21 @@ def categories
105113
# Custom `post_attr_hash` method for years
106114
def years
107115
hash = Hash.new { |h, key| h[key] = [] }
108-
@posts.each { |p| hash[p.date.year.to_s] << p }
116+
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
117+
hash.values.each { |posts| posts.sort!.reverse! }
118+
hash
119+
end
120+
121+
def months(year_posts)
122+
hash = Hash.new { |h, key| h[key] = [] }
123+
year_posts.each { |p| hash[p.date.strftime("%m")] << p }
124+
hash.values.each { |posts| posts.sort!.reverse! }
125+
hash
126+
end
127+
128+
def days(month_posts)
129+
hash = Hash.new { |h, key| h[key] = [] }
130+
month_posts.each { |p| hash[p.date.strftime("%d")] << p }
109131
hash.values.each { |posts| posts.sort!.reverse! }
110132
hash
111133
end

lib/jekyll-archives/archive.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class Archive
1717
# Initialize a new Archive page
1818
#
1919
# site - The Site object.
20-
# name - The name of the archive (e.g. "2014" or "my-category" or "my-tag").
21-
# type - The type of archive. Can be one of "year", "category", or "tag"
20+
# name - The name of the tag/category or a Hash of the year/month/day in case of date.
21+
# e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
22+
# type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
2223
# posts - The array of posts that belong in this archive.
2324
def initialize(site, name, type, posts)
2425
@site = site
@@ -46,7 +47,11 @@ def template
4647
# Returns a hash of URL placeholder names (as symbols) mapping to the
4748
# desired placeholder replacements. For details see "url.rb".
4849
def url_placeholders
49-
{ :name => @name, :type => @type.to_s }
50+
if @name.is_a? Hash
51+
@name.merge({ :type => @type })
52+
else
53+
{ :name => @name, :type => @type }
54+
end
5055
end
5156

5257
# The generated relative url of this page. e.g. /about.html.
File renamed without changes.
File renamed without changes.

test/test_jekyll_archives.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ class TestJekyllArchives < Minitest::Test
1010

1111
should "generate archive pages by year" do
1212
@archives.generate(@site)
13-
assert archive_exists? @site, "/archive/2014/"
14-
assert archive_exists? @site, "/archive/2013/"
13+
assert archive_exists? @site, "/2014/"
14+
assert archive_exists? @site, "/2013/"
15+
end
16+
17+
should "generate archive pages by month" do
18+
@archives.generate(@site)
19+
assert archive_exists? @site, "/2014/08/"
20+
assert archive_exists? @site, "/2014/03/"
21+
end
22+
23+
should "generate archive pages by day" do
24+
@archives.generate(@site)
25+
assert archive_exists? @site, "/2014/08/17/"
26+
assert archive_exists? @site, "/2013/08/16/"
1527
end
1628

1729
should "generate archive pages by tag" do
@@ -54,7 +66,7 @@ class TestJekyllArchives < Minitest::Test
5466
@site = fixture_site({
5567
"jekyll-archives" => {
5668
"permalinks" => {
57-
"year" => "/:name/",
69+
"year" => "/year/:year/",
5870
"tag" => "/tag-:name.html",
5971
"category" => "/category-:name.html"
6072
}
@@ -64,8 +76,8 @@ class TestJekyllArchives < Minitest::Test
6476
end
6577

6678
should "use the right permalink" do
67-
assert archive_exists? @site, "/2014/"
68-
assert archive_exists? @site, "/2013/"
79+
assert archive_exists? @site, "/year/2014/"
80+
assert archive_exists? @site, "/year/2013/"
6981
assert archive_exists? @site, "/tag-test.html"
7082
assert archive_exists? @site, "/tag-new.html"
7183
assert archive_exists? @site, "/category-plugins.html"
@@ -79,7 +91,7 @@ class TestJekyllArchives < Minitest::Test
7991
end
8092

8193
should "populate the {{ site.archives }} tag in Liquid" do
82-
assert_equal 6, read_file("/length.html").to_i
94+
assert_equal 12, read_file("/length.html").to_i
8395
end
8496
end
8597
end

0 commit comments

Comments
 (0)