Skip to content

Commit 84f7766

Browse files
committed
Merge pull request #9 from jekyll/date-object
Pass Date object to Liquid instead of a Hash
2 parents 88b831d + 8bbfaf6 commit 84f7766

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

lib/jekyll-archives.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ def read
5454

5555
def read_tags
5656
if enabled? "tags"
57-
tags.each do |name, posts|
58-
@archives << Archive.new(@site, name, "tag", posts)
57+
tags.each do |title, posts|
58+
@archives << Archive.new(@site, title, "tag", posts)
5959
end
6060
end
6161
end
6262

6363
def read_categories
6464
if enabled? "categories"
65-
categories.each do |name, posts|
66-
@archives << Archive.new(@site, name, "category", posts)
65+
categories.each do |title, posts|
66+
@archives << Archive.new(@site, title, "category", posts)
6767
end
6868
end
6969
end

lib/jekyll-archives/archive.rb

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,34 @@ class Archive
1111
ATTRIBUTES_FOR_LIQUID = %w[
1212
posts
1313
type
14+
title
1415
name
16+
path
17+
url
1518
]
1619

1720
# Initialize a new Archive page
1821
#
1922
# site - The Site object.
20-
# name - The name of the tag/category or a Hash of the year/month/day in case of date.
23+
# title - The name of the tag/category or a Hash of the year/month/day in case of date.
2124
# e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
2225
# type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
2326
# posts - The array of posts that belong in this archive.
24-
def initialize(site, name, type, posts)
27+
def initialize(site, title, type, posts)
2528
@site = site
2629
@posts = posts
2730
@type = type
28-
@name = name
31+
@title = title
2932

3033
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
31-
if name.is_a? String
32-
@slug = name.split(" ").map { |w|
33-
w.downcase.gsub(/[^\w]/, '')
34-
}.join("-")
34+
if title.is_a? String
35+
@slug = Utils.slugify(title)
3536
end
3637

3738
# Use ".html" for file extension and url for path
38-
@ext = ".html"
39-
@path = url
39+
@ext = File.extname(relative_path)
40+
@path = relative_path
41+
@name = File.basename(relative_path, @ext)
4042

4143
@data = {
4244
"layout" => site.config['jekyll-archives']['layout']
@@ -54,8 +56,8 @@ def template
5456
# Returns a hash of URL placeholder names (as symbols) mapping to the
5557
# desired placeholder replacements. For details see "url.rb".
5658
def url_placeholders
57-
if @name.is_a? Hash
58-
@name.merge({ :type => @type })
59+
if @title.is_a? Hash
60+
@title.merge({ :type => @type })
5961
else
6062
{ :name => @slug, :type => @type }
6163
end
@@ -99,13 +101,25 @@ def to_liquid(attrs = nil)
99101
Utils.deep_merge_hashes(data, further_data)
100102
end
101103

104+
# Produce a title object suitable for Liquid based on type of archive.
105+
#
106+
# Returns the title as a Date (for date-based archives) or a
107+
# String (for tag and category archives)
108+
def title
109+
if @title.is_a? Hash
110+
args = @title.values.map { |s| s.to_i }
111+
Date.new(*args)
112+
else
113+
@title
114+
end
115+
end
116+
102117
# Obtain destination path.
103118
#
104119
# dest - The String path to the destination dir.
105120
#
106121
# Returns the destination file path String.
107122
def destination(dest)
108-
@dest ||= dest
109123
path = Jekyll.sanitized_path(dest, URL.unescape_path(url))
110124
path = File.join(path, "index.html") if url =~ /\/$/
111125
path
@@ -115,13 +129,14 @@ def destination(dest)
115129
#
116130
# Returns the destination relative path String.
117131
def relative_path
118-
path = Pathname.new(destination(@dest)).relative_path_from Pathname.new(@dest)
132+
path = URL.unescape_path(url).gsub(/^\//, '')
133+
path = File.join(path, "index.html") if url =~ /\/$/
119134
path
120135
end
121136

122137
# Returns the object as a debug String.
123138
def inspect
124-
"#<Jekyll:Archive @type=#{@type.to_s} @name=#{@name}>"
139+
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title}>"
125140
end
126141

127142
# Returns the Boolean of whether this Page is HTML or not.

test/test_jekyll_archives.rb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@ class TestJekyllArchives < Minitest::Test
1414

1515
should "generate archive pages by year" do
1616
@archives.generate(@site)
17-
assert archive_exists? @site, "/2014/"
18-
assert archive_exists? @site, "/2013/"
17+
assert archive_exists? @site, "2014/index.html"
18+
assert archive_exists? @site, "2013/index.html"
1919
end
2020

2121
should "generate archive pages by month" do
2222
@archives.generate(@site)
23-
assert archive_exists? @site, "/2014/08/"
24-
assert archive_exists? @site, "/2014/03/"
23+
assert archive_exists? @site, "2014/08/index.html"
24+
assert archive_exists? @site, "2014/03/index.html"
2525
end
2626

2727
should "generate archive pages by day" do
2828
@archives.generate(@site)
29-
assert archive_exists? @site, "/2014/08/17/"
30-
assert archive_exists? @site, "/2013/08/16/"
29+
assert archive_exists? @site, "2014/08/17/index.html"
30+
assert archive_exists? @site, "2013/08/16/index.html"
3131
end
3232

3333
should "generate archive pages by tag" do
3434
@archives.generate(@site)
35-
assert archive_exists? @site, "/tag/test-tag/"
36-
assert archive_exists? @site, "/tag/tagged/"
37-
assert archive_exists? @site, "/tag/new/"
35+
assert archive_exists? @site, "tag/test-tag/index.html"
36+
assert archive_exists? @site, "tag/tagged/index.html"
37+
assert archive_exists? @site, "tag/new/index.html"
3838
end
3939

4040
should "generate archive pages by category" do
4141
@archives.generate(@site)
42-
assert archive_exists? @site, "/category/plugins/"
42+
assert archive_exists? @site, "category/plugins/index.html"
4343
end
4444

4545
should "generate archive pages with a layout" do
4646
@site.process
47-
assert_equal "Test", read_file("/tag/test-tag/index.html")
47+
assert_equal "Test", read_file("tag/test-tag/index.html")
4848
end
4949
end
5050

@@ -62,7 +62,7 @@ class TestJekyllArchives < Minitest::Test
6262

6363
should "use custom layout" do
6464
@site.process
65-
assert_equal "Test too", read_file("/tag/test-tag/index.html")
65+
assert_equal "Test too", read_file("tag/test-tag/index.html")
6666
end
6767
end
6868

@@ -82,11 +82,11 @@ class TestJekyllArchives < Minitest::Test
8282
end
8383

8484
should "use the right permalink" do
85-
assert archive_exists? @site, "/year/2014/"
86-
assert archive_exists? @site, "/year/2013/"
87-
assert archive_exists? @site, "/tag-test-tag.html"
88-
assert archive_exists? @site, "/tag-new.html"
89-
assert archive_exists? @site, "/category-plugins.html"
85+
assert archive_exists? @site, "year/2014/index.html"
86+
assert archive_exists? @site, "year/2013/index.html"
87+
assert archive_exists? @site, "tag-test-tag.html"
88+
assert archive_exists? @site, "tag-new.html"
89+
assert archive_exists? @site, "category-plugins.html"
9090
end
9191
end
9292

@@ -101,7 +101,7 @@ class TestJekyllArchives < Minitest::Test
101101
end
102102

103103
should "populate the {{ site.archives }} tag in Liquid" do
104-
assert_equal 12, read_file("/length.html").to_i
104+
assert_equal 12, read_file("length.html").to_i
105105
end
106106
end
107107

@@ -112,7 +112,7 @@ class TestJekyllArchives < Minitest::Test
112112
end
113113

114114
should "not generate any archives" do
115-
assert_equal 0, read_file("/length.html").to_i
115+
assert_equal 0, read_file("length.html").to_i
116116
end
117117
end
118118

@@ -127,16 +127,16 @@ class TestJekyllArchives < Minitest::Test
127127
end
128128

129129
should "generate the enabled archives" do
130-
assert archive_exists? @site, "/tag/test-tag/"
131-
assert archive_exists? @site, "/tag/tagged/"
132-
assert archive_exists? @site, "/tag/new/"
130+
assert archive_exists? @site, "tag/test-tag/index.html"
131+
assert archive_exists? @site, "tag/tagged/index.html"
132+
assert archive_exists? @site, "tag/new/index.html"
133133
end
134134

135135
should "not generate the disabled archives" do
136-
assert !archive_exists?(@site, "/2014/")
137-
assert !archive_exists?(@site, "/2014/08/")
138-
assert !archive_exists?(@site, "/2013/08/16/")
139-
assert !archive_exists?(@site, "/category/plugins/")
136+
assert !archive_exists?(@site, "2014/index.html")
137+
assert !archive_exists?(@site, "2014/08/index.html")
138+
assert !archive_exists?(@site, "2013/08/16/index.html")
139+
assert !archive_exists?(@site, "category/plugins/index.html")
140140
end
141141
end
142142
end

0 commit comments

Comments
 (0)