Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable incremental operation #58

Merged
merged 8 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ module Archives
autoload :Archive, 'jekyll-archives/archive'
autoload :VERSION, 'jekyll-archives/version'

if (Jekyll.const_defined? :Hooks)
Jekyll::Hooks.register :site, :after_reset do |site|
# We need to disable incremental regen for Archives to generate with the
# correct content
site.regenerator.instance_variable_set(:@disabled, true)
end
end

class Archives < Jekyll::Generator
safe true

Expand Down Expand Up @@ -92,6 +84,22 @@ def enabled?(archive)
end
end

# Renders the archives into the layouts
def render
payload = @site.site_payload
@archives.each do |archive|
archive.render(@site.layouts, payload)
end
end

# Write archives to their destination
def write
@archives.each do |archive|
archive.write(@site.dest) if archive.regenerate?
archive.add_dependencies
end
end

def tags
@site.post_attr_hash('tags')
end
Expand Down
53 changes: 51 additions & 2 deletions lib/jekyll-archives/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Archive < Jekyll::Page
name
path
url
permalink
).freeze

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

# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
if title.to_s.length
@slug = Utils.slugify(title.to_s)
if title.is_a? String
@slug = Utils.slugify(title)
end

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

def permalink
data && data.is_a?(Hash) && data['permalink']
end

# Add any necessary layouts to this post
#
# layouts - The Hash of {"name" => "layout"}.
# site_payload - The site payload Hash.
#
# Returns nothing.
def render(layouts, site_payload)
payload = Utils.deep_merge_hashes({
"page" => to_liquid
}, site_payload)

do_layout(payload, layouts)
end

# Add dependencies for incremental mode
def add_dependencies
if defined? site.regenerator
archive_path = site.in_dest_dir(relative_path)
site.regenerator.add(archive_path)
@posts.each do |post|
site.regenerator.add_dependency(archive_path, post.path)
end
end
end

# Convert this Convertible's data to a Hash suitable for use by Liquid.
#
# Returns the Hash representation of this Convertible.
def to_liquid(attrs = nil)
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
[attribute, send(attribute)]
}]

Utils.deep_merge_hashes(data, further_data)
end

# Produce a title object suitable for Liquid based on type of archive.
#
# Returns a String (for tag and category archives) and nil for
Expand Down Expand Up @@ -115,6 +156,14 @@ def relative_path
path
end

def regenerate?
if defined? site.regenerator
site.regenerator.regenerate?(self)
else
true
end
end

# Returns the object as a debug String.
def inspect
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
Expand Down