Skip to content
This repository was archived by the owner on Dec 8, 2017. It is now read-only.
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
50 changes: 37 additions & 13 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ 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 @@ -84,24 +92,40 @@ 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)
# Helper method for tags
# Receives an array of strings
# Returns an array of strings without dashes
def remove_dashes(tags)
cleaned_tags = []
tags.each do |tag|
cleaned_tags << tag.gsub(/-/, ' ').squeeze
end
cleaned_tags
end

# Write archives to their destination
def write
@archives.each do |archive|
archive.write(@site.dest) if archive.regenerate?
archive.add_dependencies
end
# Helper method for tags
# Receives a post, and an external hash
# Assigns posts associated with particular tags to the provided hash.
def post_attr_tags(post, hash)
post.data['tags'] ||= []
post.data['tags'] = remove_dashes(post.data['tags'])
post.data['tags'].each { |t| hash[t] << post } if post.data['tags']
end

# Custom `post_attr_hash` method for tags
def tags
@site.post_attr_hash('tags')
hash = Hash.new { |h, key| h[key] = [] }

# In Jekyll 3, Collection#each should be called on the #docs array directly.
if Jekyll::VERSION >= '3.0.0'
@posts.docs.each do |p|
post_attr_tags(p, hash)
end
else
@posts.each { |p| post_attr_tags(p, hash) }
end
hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def categories
Expand All @@ -113,7 +137,7 @@ def years
hash = Hash.new { |h, key| h[key] = [] }

# In Jekyll 3, Collection#each should be called on the #docs array directly.
if Jekyll::VERSION >= '3.0.0'
if Jekyll::VERSION >= '3.0.0'
@posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
else
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
Expand Down
53 changes: 2 additions & 51 deletions lib/jekyll-archives/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Archive < Jekyll::Page
name
path
url
permalink
).freeze

# Initialize a new Archive page
Expand All @@ -31,8 +30,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.is_a? String
@slug = Utils.slugify(title)
if title.to_s.length
@slug = Utils.slugify(title.to_s)
end

# Use ".html" for file extension and url for path
Expand Down Expand Up @@ -87,46 +86,6 @@ 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 @@ -156,14 +115,6 @@ 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