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

Refactor some weird code #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 24 additions & 58 deletions lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,82 +40,48 @@ def generate(site)
@site.config["archives"] = @archives
end

def enabled?(archive)
@config["enabled"] == true || @config["enabled"] == "all" ||
(@config["enabled"].is_a?(Array) && @config["enabled"].include?(archive))
end

# Read archive data from posts
def read
read_tags
read_categories
read_dates
read_tags if enabled?("tags")
read_categories if enabled?("categories")
read_posts_per_year if enabled?("year")
read_posts_per_month if enabled?("month")
read_posts_per_day if enabled?("day")
end

def read_tags
if enabled? "tags"
tags.each do |title, posts|
@archives << Archive.new(@site, title, "tag", posts)
end
@archives += @site.post_attr_hash("tags").map do |title, posts|
Archive.new(@site, title, "tag", posts)
end
end

def read_categories
if enabled? "categories"
categories.each do |title, posts|
@archives << Archive.new(@site, title, "category", posts)
end
@archives += @site.post_attr_hash("categories").map do |title, posts|
Archive.new(@site, title, "category", posts)
end
end

def read_dates
years.each do |year, posts|
@archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
months(posts).each do |month, posts|
@archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
days(posts).each do |day, posts|
@archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
end
end
def read_posts_per_year
@archives += @posts.docs.group_by { |p| p.date.year }.map do |year, posts_for_year|
Archive.new(@site, { :year => year }, "year", posts_for_year.sort.reverse)
end
end

# Checks if archive type is enabled in config
def enabled?(archive)
@config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
@config["enabled"].include? archive
end
end

def tags
@site.post_attr_hash("tags")
end

def categories
@site.post_attr_hash("categories")
end

# Custom `post_attr_hash` method for years
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"
@posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
else
@posts.each { |p| hash[p.date.strftime("%Y")] << p }
def read_posts_per_month
@archives += @posts.docs.group_by { |p| [p.date.year, p.date.month] }.map do |(year, month), posts_for_month|
Archive.new(@site, { :year => year, :month => month }, "month", posts_for_month.sort.reverse)
end
hash.each_value { |posts| posts.sort!.reverse! }
hash
end

def months(year_posts)
hash = Hash.new { |h, key| h[key] = [] }
year_posts.each { |p| hash[p.date.strftime("%m")] << p }
hash.each_value { |posts| posts.sort!.reverse! }
hash
end

def days(month_posts)
hash = Hash.new { |h, key| h[key] = [] }
month_posts.each { |p| hash[p.date.strftime("%d")] << p }
hash.each_value { |posts| posts.sort!.reverse! }
hash
def read_posts_per_day
@archives += @posts.docs.group_by { |p| [p.date.year, p.date.month, p.date.day] }.map do |(year, month, day), posts_for_day|
Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts_for_day.sort.reverse)
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/jekyll-archives/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def layout
# desired placeholder replacements. For details see "url.rb".
def url_placeholders
if @title.is_a? Hash
@title.merge(:type => @type)
{
:year => @title[:year].to_s,
:month => @title[:month].to_s.rjust(2, "0"),
:day => @title[:day].to_s.rjust(2, "0"),
:type => @type,
}
else
{ :name => @slug, :type => @type }
end
Expand Down