sutty-archives 2.2.2 → 2.5.0

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: 504d81958bc2f61213d04212cd3988a17131869a0dff5e294f0782e353515f43
4
- data.tar.gz: cb58a0438cdd6bbf51c33ac2bea2f33e5862498039d81d74c9bb56d94fe48c44
3
+ metadata.gz: 3918e23c7f0f639664eed1261c58a5973db93970f24e1af22ec7d030cf1496b6
4
+ data.tar.gz: 4711190c26d0aa7f9faed300fac83a95f9539c32a688f7f80debbf5785c8a4fe
5
5
  SHA512:
6
- metadata.gz: b25f4cc57b9b894980af8c95a65d3e6c35b5ef68de2c91c983d11a7c53300f96116592f20a364e7aae34c71aa028ac98ebcbe421324a9b43b2f5cbf851889f26
7
- data.tar.gz: 4259f6adafecb055f17404b46b1ba0a2033bc68905a174bca902dfdd09151ab9eb5f293d35ad37ae190e04de6c740b71107389a43734ac31c30180d64acc6a8b
6
+ metadata.gz: 6b4afccab4860c41e8e8114384ab24c3f0046d0d26200a91f4525198e6e21e20c123585442377ac96fa78fc7bffc1f75c87be5a8158360a5bf129704865da592
7
+ data.tar.gz: b70a075de1cf8488000c3de90ec38ed3bb2c932bf62d49076a6aeec84fc150f4bdb648c2cd1e079b397263aec7185b3c327cd41f4c872e51698b182cbe8a11e1
@@ -18,15 +18,18 @@ module Jekyll
18
18
 
19
19
  DEFAULTS = {
20
20
  "layout" => "archive",
21
+ "group" => false,
21
22
  "enabled" => [],
23
+ "titles" => {},
24
+ "replace" => false,
22
25
  "permalinks" => {
23
26
  "year" => "/:year/",
24
27
  "month" => "/:year/:month/",
25
28
  "day" => "/:year/:month/:day/",
26
29
  "tag" => "/tag/:name/",
27
30
  "category" => "/category/:name/",
28
- "category-tag" => "/category/:category/tag/:tag/"
29
- },
31
+ "category-tag" => "/category/:category/tag/:tag/",
32
+ }
30
33
  }.freeze
31
34
 
32
35
  def initialize(config = {})
@@ -54,14 +57,15 @@ module Jekyll
54
57
  read
55
58
  @site.pages.concat(@archives)
56
59
 
57
- @site.config["archives"] = @archives
60
+ @site.config["archives"] = @archives unless group?
58
61
  end
59
62
 
60
63
  # Read archive data from posts
61
64
  def read
65
+ read_combined if combined?
62
66
  read_attrs
63
- read_categories_tags
64
67
  read_dates
68
+ read_types if group?
65
69
  end
66
70
 
67
71
  # Read and group by attributes, using the legacy names when
@@ -74,6 +78,12 @@ module Jekyll
74
78
  next unless attr_title_enabled? attr, title
75
79
 
76
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
77
87
  end
78
88
  end
79
89
  end
@@ -90,17 +100,57 @@ module Jekyll
90
100
  end
91
101
  end
92
102
 
93
- def read_categories_tags
94
- if enabled? "categories-tags"
95
- @site.categories.each do |category, posts|
96
- posts.map { |post| post.data['tags'] }.flatten.uniq.compact.each do |tag|
97
- category_tag_posts = posts.select do |post|
98
- post.data['categories'].include?(category) &&
99
- post.data['tags'].include?(tag)
100
- end
101
-
102
- @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
103
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
104
154
  end
105
155
  end
106
156
  end
@@ -117,6 +167,21 @@ module Jekyll
117
167
  end
118
168
  end
119
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
+
120
185
  # Return the front matter attributes to archive by, using the
121
186
  # legacy names unless specified and leaving out the date
122
187
  # attributes.
@@ -131,6 +196,16 @@ module Jekyll
131
196
  @enabled == true || @enabled == "all" || (@enabled.is_a?(Array) && @enabled.include?(archive))
132
197
  end
133
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
+
134
209
  # Custom `post_attr_hash` method for years
135
210
  def years
136
211
  date_attr_hash(@posts.docs, "%Y")
@@ -40,10 +40,24 @@ module Jekyll
40
40
  @path = relative_path
41
41
  @name = File.basename(relative_path, @ext)
42
42
 
43
- @data = {
44
- "layout" => layout,
45
- }
43
+ @data = { "layout" => layout }
44
+
45
+ if title.is_a?(Hash) && !date?
46
+ @data.merge! title
47
+ end
48
+
46
49
  @content = ""
50
+
51
+ # Replace the value with the archive except for date and
52
+ # category-tag. For category-tag, do we want to replace the
53
+ # category or the tag?
54
+ return unless replace?
55
+ @posts.each do |post|
56
+ case post.data[attribute]
57
+ when Array then post.data[attribute].map!{|v| (v == title) ? self : v }
58
+ else post.data[attribute] = self
59
+ end
60
+ end
47
61
  end
48
62
 
49
63
  # The template of the permalink.
@@ -51,7 +65,7 @@ module Jekyll
51
65
  # Returns the template String.
52
66
  def template
53
67
  t = @config.dig("permalinks", type)
54
- t = t.is_a?(Hash) ? t[title] : t
68
+ t = t.is_a?(Hash) ? t[@title] : t
55
69
 
56
70
  t || "/#{type}/:name/"
57
71
  end
@@ -69,9 +83,9 @@ module Jekyll
69
83
  if @title.is_a? Hash
70
84
  placeholders = @title.merge(:type => @type)
71
85
 
72
- if @type == 'category-tag'
73
- %i[category tag].each do |k|
74
- placeholders[k] = Utils.slugify(@title[k], mode: @config.dig('slug'))
86
+ unless date?
87
+ placeholders.transform_values! do |v|
88
+ Utils.slugify(v, mode: @config.dig('slug'))
75
89
  end
76
90
  end
77
91
 
@@ -104,8 +118,8 @@ module Jekyll
104
118
  # date-based archives.
105
119
  def title
106
120
  if @title.is_a? String
107
- @title
108
- elsif @type == 'category-tag'
121
+ @config.dig('titles', @title) || @title
122
+ elsif !date?
109
123
  @title.values.join(@config.fetch('separator', ' / '))
110
124
  end
111
125
  end
@@ -152,6 +166,18 @@ module Jekyll
152
166
 
153
167
  private
154
168
 
169
+ def replace?
170
+ @config['replace'] && !@type.is_a?(Hash)
171
+ end
172
+
173
+ def attribute
174
+ case type
175
+ when 'tag' then 'tags'
176
+ when 'category' then 'categories'
177
+ else type
178
+ end
179
+ end
180
+
155
181
  # Generate slug if @title attribute is a string.
156
182
  #
157
183
  # Note: mode other than those expected by Jekyll returns the given string after
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Archives
5
- VERSION = "2.2.2"
5
+ VERSION = "2.5.0"
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.2.2
4
+ version: 2.5.0
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-07-21 00:00:00.000000000 Z
12
+ date: 2020-10-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jekyll
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
146
  requirements: []
147
- rubygems_version: 3.0.3
147
+ rubygems_version: 3.1.2
148
148
  signing_key:
149
149
  specification_version: 4
150
150
  summary: Post archives for Jekyll. Fork of jekyll-archives.