sutty-archives 2.3.0 → 2.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fef8ed446f2c81a26428e355f7b4c14923fdd56b9367db006f13464f909cac8
4
- data.tar.gz: a2a0af5d85a97a779b0ca73f2300612ea333d2c2d99accd12bdc822673ba0874
3
+ metadata.gz: b8a095196aeb5056b025411ea4903f3f216c17ee98156f6510505b7b13c99d76
4
+ data.tar.gz: f0de3000044bece4ddc3304a340c4118dfdb72b049ee73fce0903167632e3eb8
5
5
  SHA512:
6
- metadata.gz: 4933c5145a31350e62411b303ecba29f8b49a50e63baf4412ef02f6bb96f65cc82ac31febf86c459303ead1d1c7ccdcf1ce2586d323b03c53d26c7be2cdc42ab
7
- data.tar.gz: 811cf946c59dbd7c297cfe201843f76f87e043a2d964434ae4e380dbb190eec710ee040526ef270d584aafb4df6a1956dd1d392593831d67f5c9a8957f4f1856
6
+ metadata.gz: 64d92442969a17a69f35af7c89e7496eeb8ff57ea7bf89cd8c8f8225eaeaa62157cbbd78e339cbfa4d87c01dcdf933b2c29ae1bde1b22352891a4d1ff322767d
7
+ data.tar.gz: 0ca2b534faeb9e035fd7fe95d488e4532732a9c83d4cc5f98ad8b1902ee6f0e2655275dc409e3deff3aea7823f0eede938f24e432a00e57d36b2381cb9b08eca
@@ -18,6 +18,7 @@ module Jekyll
18
18
 
19
19
  DEFAULTS = {
20
20
  "layout" => "archive",
21
+ "group" => false,
21
22
  "enabled" => [],
22
23
  "titles" => {},
23
24
  "replace" => false,
@@ -56,14 +57,15 @@ module Jekyll
56
57
  read
57
58
  @site.pages.concat(@archives)
58
59
 
59
- @site.config["archives"] = @archives
60
+ @site.config["archives"] = @archives unless group?
60
61
  end
61
62
 
62
63
  # Read archive data from posts
63
64
  def read
65
+ read_combined if combined?
64
66
  read_attrs
65
- read_categories_tags
66
67
  read_dates
68
+ read_types if group?
67
69
  end
68
70
 
69
71
  # Read and group by attributes, using the legacy names when
@@ -76,6 +78,12 @@ module Jekyll
76
78
  next unless attr_title_enabled? attr, title
77
79
 
78
80
  @archives << Archive.new(@site, title, attr, posts)
81
+
82
+ next unless group?
83
+
84
+ @site.config['archives'] ||= {}
85
+ @site.config['archives'][attr] ||= []
86
+ @site.config['archives'][attr] << @archives.last
79
87
  end
80
88
  end
81
89
  end
@@ -92,17 +100,57 @@ module Jekyll
92
100
  end
93
101
  end
94
102
 
95
- def read_categories_tags
96
- if enabled? "categories-tags"
97
- @site.categories.each do |category, posts|
98
- posts.map { |post| post.data['tags'] }.flatten.uniq.compact.each do |tag|
99
- category_tag_posts = posts.select do |post|
100
- post.data['categories'].include?(category) &&
101
- post.data['tags'].include?(tag)
102
- end
103
-
104
- @archives << Archive.new(@site, { category: category, tag: tag }, "category-tag", category_tag_posts)
103
+ # Archive posts by a combination of attributes.
104
+ def read_combined
105
+ # We can have many combination archives
106
+ @config['combined'].each do |type, combined|
107
+ # This is an empty map so we can have all keys on the result
108
+ empty_combination = Hash[combined.map{|k| [k,nil]}]
109
+
110
+ # We collect all posts
111
+ @posts.docs.map do |post|
112
+ # Array of Jekyll::Document and Hash of values, some may be
113
+ # empty.
114
+ [ post, empty_combination.merge(**post.data.slice(*combined)) ]
115
+ end.select do |combination|
116
+ # We skip Documents that don't have any of the attributes
117
+ combination.last.none? do |_, e|
118
+ # TODO: Move to its own method
119
+ e.nil? || (e.respond_to?(:empty?) && e.empty?) || (e.respond_to?(:blank?) && e.blank?) || (e.respond_to?(:zero?) && e.zero?)
120
+ end
121
+ end.map do |post, data|
122
+ # Since the values can be Arrays, we'll generate all
123
+ # combinations
124
+ data.values.reduce do |memo, value|
125
+ # XXX: Ensure value is an Array, it could be a String or other
126
+ # value.
127
+ (memo.is_a?(Array) ? memo : [memo]).product([value].flatten)
128
+ end.map(&:flatten).map do |v|
129
+ # Recreate a Hash of attribute and single value for each
130
+ # combination
131
+ data.keys.zip(v)
132
+ end.map do |hash|
133
+ # Recreate the first collection of Document and Hash for
134
+ # each combination.
135
+ [ post, Hash[hash] ]
136
+ end
137
+ end.flatten(1).group_by(&:last).transform_values(&:flatten).transform_values do |v|
138
+ # Create groups by value combinations and select only
139
+ # Documents
140
+ v.select do |p|
141
+ p.is_a? Jekyll::Document
105
142
  end
143
+ end.each do |combination, posts|
144
+ # Then create Archives for them.
145
+ archive = Archive.new(@site, combination, type, posts)
146
+
147
+ @archives << archive
148
+
149
+ next unless group?
150
+
151
+ @site.config['archives'] ||= {}
152
+ @site.config['archives'][type] ||= []
153
+ @site.config['archives'][type] << archive
106
154
  end
107
155
  end
108
156
  end
@@ -119,6 +167,21 @@ module Jekyll
119
167
  end
120
168
  end
121
169
 
170
+ # Generates Archives by Archive type
171
+ def read_types
172
+ @config['enabled'].each do |type|
173
+ type = LEGACY_ATTRS[type] if LEGACY_ATTRS[type]
174
+
175
+ @archives << Archive.new(@site, type, 'group', @site.config['archives'][type].map(&:posts).flatten.uniq)
176
+ end
177
+
178
+ return unless combined?
179
+
180
+ @config['combined'].each do |type, _|
181
+ @archives << Archive.new(@site, type, 'group', @site.config['archives'][type].map(&:posts).flatten.uniq)
182
+ end
183
+ end
184
+
122
185
  # Return the front matter attributes to archive by, using the
123
186
  # legacy names unless specified and leaving out the date
124
187
  # attributes.
@@ -133,6 +196,16 @@ module Jekyll
133
196
  @enabled == true || @enabled == "all" || (@enabled.is_a?(Array) && @enabled.include?(archive))
134
197
  end
135
198
 
199
+ # Checks if we want grouped archives
200
+ def group?
201
+ @config['group']
202
+ end
203
+
204
+ # Check if we have combined archives
205
+ def combined?
206
+ @config['combined'].is_a?(Hash)
207
+ end
208
+
136
209
  # Custom `post_attr_hash` method for years
137
210
  def years
138
211
  date_attr_hash(@posts.docs, "%Y")
@@ -42,8 +42,16 @@ module Jekyll
42
42
 
43
43
  @data = { "layout" => layout }
44
44
 
45
+ if title.is_a?(Hash) && !date?
46
+ @data.merge! title
47
+ end
48
+
45
49
  @content = ""
46
50
 
51
+ data.default_proc = proc do |_, key|
52
+ site.frontmatter_defaults.find(relative_path, type, key)
53
+ end
54
+
47
55
  # Replace the value with the archive except for date and
48
56
  # category-tag. For category-tag, do we want to replace the
49
57
  # category or the tag?
@@ -79,9 +87,9 @@ module Jekyll
79
87
  if @title.is_a? Hash
80
88
  placeholders = @title.merge(:type => @type)
81
89
 
82
- if @type == 'category-tag'
83
- %i[category tag].each do |k|
84
- placeholders[k] = Utils.slugify(@title[k], mode: @config.dig('slug'))
90
+ unless date?
91
+ placeholders.transform_values! do |v|
92
+ Utils.slugify(v, mode: @config.dig('slug'))
85
93
  end
86
94
  end
87
95
 
@@ -115,7 +123,7 @@ module Jekyll
115
123
  def title
116
124
  if @title.is_a? String
117
125
  @config.dig('titles', @title) || @title
118
- elsif @type == 'category-tag'
126
+ elsif !date?
119
127
  @title.values.join(@config.fetch('separator', ' / '))
120
128
  end
121
129
  end
@@ -162,8 +170,9 @@ module Jekyll
162
170
 
163
171
  private
164
172
 
173
+ # Layout is a special attribute that can't be replaced
165
174
  def replace?
166
- @config['replace'] && !(date? || @type == 'category-tag')
175
+ @config['replace'] && !@type.is_a?(Hash) && attribute != 'layout'
167
176
  end
168
177
 
169
178
  def attribute
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Archives
5
- VERSION = "2.3.0"
5
+ VERSION = "2.5.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sutty-archives
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alfred Xing
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-09-29 00:00:00.000000000 Z
12
+ date: 2020-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jekyll