Class: Jekyll::Archives::Archives
- Inherits:
-
Generator
- Object
- Generator
- Jekyll::Archives::Archives
- Defined in:
- lib/jekyll-archives.rb
Constant Summary collapse
- DEFAULTS =
{ "layout" => "archive", "enabled" => [], "permalinks" => { "year" => "/:year/", "month" => "/:year/:month/", "day" => "/:year/:month/:day/", "tag" => "/tag/:name/", "category" => "/category/:name/", }, }.freeze
Instance Method Summary collapse
- #categories ⇒ Object
- #days(month_posts) ⇒ Object
-
#enabled?(archive) ⇒ Boolean
Checks if archive type is enabled in config.
- #generate(site) ⇒ Object
-
#initialize(config = nil) ⇒ Archives
constructor
A new instance of Archives.
- #months(year_posts) ⇒ Object
-
#read ⇒ Object
Read archive data from posts.
- #read_categories ⇒ Object
- #read_dates ⇒ Object
- #read_tags ⇒ Object
- #tags ⇒ Object
-
#years ⇒ Object
Custom ‘post_attr_hash` method for years.
Constructor Details
Instance Method Details
#categories ⇒ Object
89 90 91 |
# File 'lib/jekyll-archives.rb', line 89 def categories @site.post_attr_hash("categories") end |
#days(month_posts) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/jekyll-archives.rb', line 114 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 end |
#enabled?(archive) ⇒ Boolean
Checks if archive type is enabled in config
79 80 81 82 83 |
# File 'lib/jekyll-archives.rb', line 79 def enabled?(archive) @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array @config["enabled"].include? archive end end |
#generate(site) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/jekyll-archives.rb', line 30 def generate(site) @site = site @posts = site.posts @archives = [] @site.config["jekyll-archives"] = @config read @site.pages.concat(@archives) @site.config["archives"] = @archives end |
#months(year_posts) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/jekyll-archives.rb', line 107 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 |
#read ⇒ Object
Read archive data from posts
44 45 46 47 48 |
# File 'lib/jekyll-archives.rb', line 44 def read read_categories read_dates end |
#read_categories ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/jekyll-archives.rb', line 58 def read_categories if enabled? "categories" categories.each do |title, posts| @archives << Archive.new(@site, title, "category", posts) end end end |
#read_dates ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jekyll-archives.rb', line 66 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 end end |
#read_tags ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/jekyll-archives.rb', line 50 def if enabled? "tags" .each do |title, posts| @archives << Archive.new(@site, title, "tag", posts) end end end |
#tags ⇒ Object
85 86 87 |
# File 'lib/jekyll-archives.rb', line 85 def @site.post_attr_hash("tags") end |
#years ⇒ Object
Custom ‘post_attr_hash` method for years
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/jekyll-archives.rb', line 94 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 } end hash.each_value { |posts| posts.sort!.reverse! } hash end |