Skip to content

Commit ac71b45

Browse files
committed
Update archive.rb
1 parent 3f97948 commit ac71b45

File tree

1 file changed

+187
-76
lines changed

1 file changed

+187
-76
lines changed

lib/jekyll-archives/archive.rb

Lines changed: 187 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,187 @@
1-
--- archive.rb
2-
+++ (clipboard)
3-
@@ -17,7 +17,6 @@
4-
name
5-
path
6-
url
7-
- permalink
8-
]
9-
10-
# Initialize a new Archive page
11-
@@ -35,19 +34,24 @@
12-
@config = site.config['jekyll-archives']
13-
14-
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
15-
- if title.is_a? String
16-
- @slug = Utils.slugify(title)
17-
+ if title.to_s.length
18-
+ @slug = Utils.slugify(title.to_s)
19-
end
20-
21-
# Use ".html" for file extension and url for path
22-
@ext = File.extname(relative_path)
23-
- @path = site.in_dest_dir(relative_path)
24-
+ @path = relative_path
25-
@name = File.basename(relative_path, @ext)
26-
27-
@data = {
28-
"layout" => layout
29-
}
30-
@content = ""
31-
+
32-
+ # set up dependencies
33-
+ posts.each do |post|
34-
+ site.regenerator.add_dependency(self.path, post.path)
35-
+ end
36-
end
37-
38-
# The template of the permalink.
39-
@@ -91,10 +95,6 @@
40-
raise ArgumentError.new "Template \"#{template}\" provided is invalid."
41-
end
42-
43-
- def permalink
44-
- data && data.is_a?(Hash) && data['permalink']
45-
- end
46-
-
47-
# Add any necessary layouts to this post
48-
#
49-
# layouts - The Hash of {"name" => "layout"}.
50-
@@ -109,15 +109,6 @@
51-
do_layout(payload, layouts)
52-
end
53-
54-
- # Add dependencies for incremental mode
55-
- def add_dependencies
56-
- archive_path = site.in_dest_dir(relative_path)
57-
- site.regenerator.add(archive_path)
58-
- @posts.each do |post|
59-
- site.regenerator.add_dependency(archive_path, post.path)
60-
- end
61-
- end
62-
-
63-
# Convert this Convertible's data to a Hash suitable for use by Liquid.
64-
#
65-
# Returns the Hash representation of this Convertible.
66-
@@ -169,10 +160,6 @@
67-
path
68-
end
69-
70-
- def regenerate?
71-
- site.regenerator.regenerate?(self)
72-
- end
73-
-
74-
# Returns the object as a debug String.
75-
def inspect
76-
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
1+
module Jekyll
2+
module Archives
3+
class Archive
4+
include Convertible
5+
6+
attr_accessor :posts, :type, :name, :slug
7+
attr_accessor :data, :content, :output
8+
attr_accessor :path, :ext
9+
attr_accessor :site
10+
11+
# Attributes for Liquid templates
12+
ATTRIBUTES_FOR_LIQUID = %w[
13+
posts
14+
type
15+
title
16+
date
17+
name
18+
path
19+
url
20+
permalink
21+
]
22+
23+
# Initialize a new Archive page
24+
#
25+
# site - The Site object.
26+
# title - The name of the tag/category or a Hash of the year/month/day in case of date.
27+
# e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
28+
# type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
29+
# posts - The array of posts that belong in this archive.
30+
def initialize(site, title, type, posts)
31+
@site = site
32+
@posts = posts
33+
@type = type
34+
@title = title
35+
@config = site.config['jekyll-archives']
36+
37+
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
38+
if title.is_a? String
39+
@slug = Utils.slugify(title)
40+
end
41+
42+
# Use ".html" for file extension and url for path
43+
@ext = File.extname(relative_path)
44+
@path = site.in_dest_dir(relative_path)
45+
@name = File.basename(relative_path, @ext)
46+
47+
@data = {
48+
"layout" => layout
49+
}
50+
@content = ""
51+
end
52+
53+
# The template of the permalink.
54+
#
55+
# Returns the template String.
56+
def template
57+
@config['permalinks'][type]
58+
end
59+
60+
# The layout to use for rendering
61+
#
62+
# Returns the layout as a String
63+
def layout
64+
if @config['layouts'] && @config['layouts'][type]
65+
@config['layouts'][type]
66+
else
67+
@config['layout']
68+
end
69+
end
70+
71+
# Returns a hash of URL placeholder names (as symbols) mapping to the
72+
# desired placeholder replacements. For details see "url.rb".
73+
def url_placeholders
74+
if @title.is_a? Hash
75+
@title.merge({ :type => @type })
76+
else
77+
{ :name => @slug, :type => @type }
78+
end
79+
end
80+
81+
# The generated relative url of this page. e.g. /about.html.
82+
#
83+
# Returns the String url.
84+
def url
85+
@url ||= URL.new({
86+
:template => template,
87+
:placeholders => url_placeholders,
88+
:permalink => nil
89+
}).to_s
90+
rescue ArgumentError
91+
raise ArgumentError.new "Template \"#{template}\" provided is invalid."
92+
end
93+
94+
def permalink
95+
data && data.is_a?(Hash) && data['permalink']
96+
end
97+
98+
# Add any necessary layouts to this post
99+
#
100+
# layouts - The Hash of {"name" => "layout"}.
101+
# site_payload - The site payload Hash.
102+
#
103+
# Returns nothing.
104+
def render(layouts, site_payload)
105+
payload = Utils.deep_merge_hashes({
106+
"page" => to_liquid
107+
}, site_payload)
108+
109+
do_layout(payload, layouts)
110+
end
111+
112+
# Add dependencies for incremental mode
113+
def add_dependencies
114+
archive_path = site.in_dest_dir(relative_path)
115+
site.regenerator.add(archive_path)
116+
@posts.each do |post|
117+
site.regenerator.add_dependency(archive_path, post.path)
118+
end
119+
end
120+
121+
# Convert this Convertible's data to a Hash suitable for use by Liquid.
122+
#
123+
# Returns the Hash representation of this Convertible.
124+
def to_liquid(attrs = nil)
125+
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
126+
[attribute, send(attribute)]
127+
}]
128+
129+
Utils.deep_merge_hashes(data, further_data)
130+
end
131+
132+
# Produce a title object suitable for Liquid based on type of archive.
133+
#
134+
# Returns a String (for tag and category archives) and nil for
135+
# date-based archives.
136+
def title
137+
if @title.is_a? String
138+
@title
139+
end
140+
end
141+
142+
# Produce a date object if a date-based archive
143+
#
144+
# Returns a Date.
145+
def date
146+
if @title.is_a? Hash
147+
args = @title.values.map { |s| s.to_i }
148+
Date.new(*args)
149+
end
150+
end
151+
152+
# Obtain destination path.
153+
#
154+
# dest - The String path to the destination dir.
155+
#
156+
# Returns the destination file path String.
157+
def destination(dest)
158+
path = Jekyll.sanitized_path(dest, URL.unescape_path(url))
159+
path = File.join(path, "index.html") if url =~ /\/$/
160+
path
161+
end
162+
163+
# Obtain the write path relative to the destination directory
164+
#
165+
# Returns the destination relative path String.
166+
def relative_path
167+
path = URL.unescape_path(url).gsub(/^\//, '')
168+
path = File.join(path, "index.html") if url =~ /\/$/
169+
path
170+
end
171+
172+
def regenerate?
173+
site.regenerator.regenerate?(self)
174+
end
175+
176+
# Returns the object as a debug String.
177+
def inspect
178+
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
179+
end
180+
181+
# Returns the Boolean of whether this Page is HTML or not.
182+
def html?
183+
true
184+
end
185+
end
186+
end
187+
end

0 commit comments

Comments
 (0)