Skip to content

Commit b7d4e1a

Browse files
authored
Merge pull request #58 from mnot/master
Merge pull request 58
2 parents 90bfe84 + 67e56d7 commit b7d4e1a

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

lib/jekyll-archives.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ module Archives
66
autoload :Archive, 'jekyll-archives/archive'
77
autoload :VERSION, 'jekyll-archives/version'
88

9-
if (Jekyll.const_defined? :Hooks)
10-
Jekyll::Hooks.register :site, :after_reset do |site|
11-
# We need to disable incremental regen for Archives to generate with the
12-
# correct content
13-
site.regenerator.instance_variable_set(:@disabled, true)
14-
end
15-
end
16-
179
class Archives < Jekyll::Generator
1810
safe true
1911

@@ -92,6 +84,22 @@ def enabled?(archive)
9284
end
9385
end
9486

87+
# Renders the archives into the layouts
88+
def render
89+
payload = @site.site_payload
90+
@archives.each do |archive|
91+
archive.render(@site.layouts, payload)
92+
end
93+
end
94+
95+
# Write archives to their destination
96+
def write
97+
@archives.each do |archive|
98+
archive.write(@site.dest) if archive.regenerate?
99+
archive.add_dependencies
100+
end
101+
end
102+
95103
def tags
96104
@site.post_attr_hash('tags')
97105
end

lib/jekyll-archives/archive.rb

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Archive < Jekyll::Page
1313
name
1414
path
1515
url
16+
permalink
1617
).freeze
1718

1819
# Initialize a new Archive page
@@ -30,8 +31,8 @@ def initialize(site, title, type, posts)
3031
@config = site.config['jekyll-archives']
3132

3233
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
33-
if title.to_s.length
34-
@slug = Utils.slugify(title.to_s)
34+
if title.is_a? String
35+
@slug = Utils.slugify(title)
3536
end
3637

3738
# Use ".html" for file extension and url for path
@@ -86,6 +87,46 @@ def url
8687
raise ArgumentError.new "Template \"#{template}\" provided is invalid."
8788
end
8889

90+
def permalink
91+
data && data.is_a?(Hash) && data['permalink']
92+
end
93+
94+
# Add any necessary layouts to this post
95+
#
96+
# layouts - The Hash of {"name" => "layout"}.
97+
# site_payload - The site payload Hash.
98+
#
99+
# Returns nothing.
100+
def render(layouts, site_payload)
101+
payload = Utils.deep_merge_hashes({
102+
"page" => to_liquid
103+
}, site_payload)
104+
105+
do_layout(payload, layouts)
106+
end
107+
108+
# Add dependencies for incremental mode
109+
def add_dependencies
110+
if defined? site.regenerator
111+
archive_path = site.in_dest_dir(relative_path)
112+
site.regenerator.add(archive_path)
113+
@posts.each do |post|
114+
site.regenerator.add_dependency(archive_path, post.path)
115+
end
116+
end
117+
end
118+
119+
# Convert this Convertible's data to a Hash suitable for use by Liquid.
120+
#
121+
# Returns the Hash representation of this Convertible.
122+
def to_liquid(attrs = nil)
123+
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
124+
[attribute, send(attribute)]
125+
}]
126+
127+
Utils.deep_merge_hashes(data, further_data)
128+
end
129+
89130
# Produce a title object suitable for Liquid based on type of archive.
90131
#
91132
# Returns a String (for tag and category archives) and nil for
@@ -115,6 +156,14 @@ def relative_path
115156
path
116157
end
117158

159+
def regenerate?
160+
if defined? site.regenerator
161+
site.regenerator.regenerate?(self)
162+
else
163+
true
164+
end
165+
end
166+
118167
# Returns the object as a debug String.
119168
def inspect
120169
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"

0 commit comments

Comments
 (0)