sutty-archives 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 504d81958bc2f61213d04212cd3988a17131869a0dff5e294f0782e353515f43
4
+ data.tar.gz: cb58a0438cdd6bbf51c33ac2bea2f33e5862498039d81d74c9bb56d94fe48c44
5
+ SHA512:
6
+ metadata.gz: b25f4cc57b9b894980af8c95a65d3e6c35b5ef68de2c91c983d11a7c53300f96116592f20a364e7aae34c71aa028ac98ebcbe421324a9b43b2f5cbf851889f26
7
+ data.tar.gz: 4259f6adafecb055f17404b46b1ba0a2033bc68905a174bca902dfdd09151ab9eb5f293d35ad37ae190e04de6c740b71107389a43734ac31c30180d64acc6a8b
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ module Jekyll
6
+ module Archives
7
+ # Internal requires
8
+ autoload :Archive, "jekyll-archives/archive"
9
+ autoload :VERSION, "jekyll-archives/version"
10
+
11
+ class Archives < Jekyll::Generator
12
+ safe true
13
+
14
+ DATE_ATTRS = %w[year month day].freeze
15
+ # Map between the front matter attribute and names used by
16
+ # jekyll-archive
17
+ LEGACY_ATTRS = { 'tags' => 'tag', 'categories' => 'category' }.freeze
18
+
19
+ DEFAULTS = {
20
+ "layout" => "archive",
21
+ "enabled" => [],
22
+ "permalinks" => {
23
+ "year" => "/:year/",
24
+ "month" => "/:year/:month/",
25
+ "day" => "/:year/:month/:day/",
26
+ "tag" => "/tag/:name/",
27
+ "category" => "/category/:name/",
28
+ "category-tag" => "/category/:category/tag/:tag/"
29
+ },
30
+ }.freeze
31
+
32
+ def initialize(config = {})
33
+ archives_config = config.fetch("jekyll-archives", {})
34
+ if archives_config.is_a?(Hash)
35
+ @config = Utils.deep_merge_hashes(DEFAULTS, archives_config)
36
+ else
37
+ @config = nil
38
+ Jekyll.logger.warn "Archives:", "Expected a hash but got #{archives_config.inspect}"
39
+ Jekyll.logger.warn "", "Archives will not be generated for this site."
40
+ end
41
+ @enabled = @config && @config["enabled"]
42
+ @post_attr_hash = {}
43
+ end
44
+
45
+ def generate(site)
46
+ return if @config.nil?
47
+
48
+ @site = site
49
+ @posts = site.posts
50
+ @archives = []
51
+
52
+ @site.config["jekyll-archives"] = @config
53
+
54
+ read
55
+ @site.pages.concat(@archives)
56
+
57
+ @site.config["archives"] = @archives
58
+ end
59
+
60
+ # Read archive data from posts
61
+ def read
62
+ read_attrs
63
+ read_categories_tags
64
+ read_dates
65
+ end
66
+
67
+ # Read and group by attributes, using the legacy names when
68
+ # needed.
69
+ def read_attrs
70
+ attrs.each do |attr|
71
+ post_attr_hash(attr).each do |title, posts|
72
+ attr = LEGACY_ATTRS[attr] if LEGACY_ATTRS[attr]
73
+
74
+ next unless attr_title_enabled? attr, title
75
+
76
+ @archives << Archive.new(@site, title, attr, posts)
77
+ end
78
+ end
79
+ end
80
+
81
+ # If the permalink is a hash of value => template, check if it
82
+ # isn't disabled.
83
+ def attr_title_enabled?(attr, value)
84
+ permalinks = @config.dig('permalinks', attr)
85
+
86
+ if permalinks.is_a?(Hash)
87
+ !!permalinks.dig(value)
88
+ else
89
+ true
90
+ end
91
+ end
92
+
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
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ def read_dates
109
+ years.each do |year, y_posts|
110
+ append_enabled_date_type({ :year => year }, "year", y_posts)
111
+ months(y_posts).each do |month, m_posts|
112
+ append_enabled_date_type({ :year => year, :month => month }, "month", m_posts)
113
+ days(m_posts).each do |day, d_posts|
114
+ append_enabled_date_type({ :year => year, :month => month, :day => day }, "day", d_posts)
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ # Return the front matter attributes to archive by, using the
121
+ # legacy names unless specified and leaving out the date
122
+ # attributes.
123
+ def attrs
124
+ return LEGACY_ATTRS.keys unless @enabled.is_a? Array
125
+
126
+ @attrs ||= @enabled - DATE_ATTRS
127
+ end
128
+
129
+ # Checks if archive type is enabled in config
130
+ def enabled?(archive)
131
+ @enabled == true || @enabled == "all" || (@enabled.is_a?(Array) && @enabled.include?(archive))
132
+ end
133
+
134
+ # Custom `post_attr_hash` method for years
135
+ def years
136
+ date_attr_hash(@posts.docs, "%Y")
137
+ end
138
+
139
+ # Custom `post_attr_hash` method for months
140
+ def months(year_posts)
141
+ date_attr_hash(year_posts, "%m")
142
+ end
143
+
144
+ # Custom `post_attr_hash` method for days
145
+ def days(month_posts)
146
+ date_attr_hash(month_posts, "%d")
147
+ end
148
+
149
+ private
150
+
151
+ # Initialize a new Archive page and append to base array if the associated date `type`
152
+ # has been enabled by configuration.
153
+ #
154
+ # meta - A Hash of the year / month / day as applicable for date.
155
+ # type - The type of date archive.
156
+ # posts - The array of posts that belong in the date archive.
157
+ def append_enabled_date_type(meta, type, posts)
158
+ @archives << Archive.new(@site, meta, type, posts) if enabled?(type)
159
+ end
160
+
161
+ # Custom `post_attr_hash` for date type archives.
162
+ #
163
+ # posts - Array of posts to be considered for archiving.
164
+ # id - String used to format post date via `Time.strptime` e.g. %Y, %m, etc.
165
+ def date_attr_hash(posts, id)
166
+ hash = Hash.new { |hsh, key| hsh[key] = [] }
167
+ posts.each { |post| hash[post.date.strftime(id)] << post }
168
+ hash.each_value { |posts| posts.sort!.reverse! }
169
+ hash
170
+ end
171
+
172
+ # Custom `post_attr_hash` to group by any attribute
173
+ def post_attr_hash(post_attr)
174
+ @post_attr_hash[post_attr] ||= begin
175
+ hash = Hash.new { |h, key| h[key] = [] }
176
+ @site.posts.docs.each do |p|
177
+ d = p.data[post_attr]
178
+
179
+ next if d.nil?
180
+
181
+ if d.respond_to? :each
182
+ d.each { |t| hash[t] << p }
183
+ else
184
+ hash[d.to_s] << p
185
+ end
186
+ end
187
+ hash.each_value { |posts| posts.sort!.reverse! }
188
+ hash
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Archives
5
+ class Archive < Jekyll::Page
6
+ attr_accessor :posts, :type, :slug
7
+
8
+ # Attributes for Liquid templates
9
+ ATTRIBUTES_FOR_LIQUID = %w(
10
+ posts
11
+ type
12
+ title
13
+ date
14
+ name
15
+ path
16
+ url
17
+ permalink
18
+ slug
19
+ category
20
+ tag
21
+ ).freeze
22
+
23
+ # Initialize a new Archive page
24
+ #
25
+ # site - The Site object.
26
+ # title - The name of the tag/category or a Hash of the year/month/day in case of date.
27
+ # e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
28
+ # type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
29
+ # posts - The array of posts that belong in this archive.
30
+ def initialize(site, title, type, posts)
31
+ @site = site
32
+ @posts = posts
33
+ @type = type
34
+ @title = title
35
+ @config = site.config["jekyll-archives"]
36
+ @slug = slugify_string_title
37
+
38
+ # Use ".html" for file extension and url for path
39
+ @ext = File.extname(relative_path)
40
+ @path = relative_path
41
+ @name = File.basename(relative_path, @ext)
42
+
43
+ @data = {
44
+ "layout" => layout,
45
+ }
46
+ @content = ""
47
+ end
48
+
49
+ # The template of the permalink.
50
+ #
51
+ # Returns the template String.
52
+ def template
53
+ t = @config.dig("permalinks", type)
54
+ t = t.is_a?(Hash) ? t[title] : t
55
+
56
+ t || "/#{type}/:name/"
57
+ end
58
+
59
+ # The layout to use for rendering
60
+ #
61
+ # Returns the layout as a String
62
+ def layout
63
+ @config.dig("layouts", type) || @config["layout"]
64
+ end
65
+
66
+ # Returns a hash of URL placeholder names (as symbols) mapping to the
67
+ # desired placeholder replacements. For details see "url.rb".
68
+ def url_placeholders
69
+ if @title.is_a? Hash
70
+ placeholders = @title.merge(:type => @type)
71
+
72
+ if @type == 'category-tag'
73
+ %i[category tag].each do |k|
74
+ placeholders[k] = Utils.slugify(@title[k], mode: @config.dig('slug'))
75
+ end
76
+ end
77
+
78
+ placeholders
79
+ else
80
+ { :name => @slug, :type => @type }
81
+ end
82
+ end
83
+
84
+ # The generated relative url of this page. e.g. /about.html.
85
+ #
86
+ # Returns the String url.
87
+ def url
88
+ @url ||= URL.new(
89
+ :template => template,
90
+ :placeholders => url_placeholders,
91
+ :permalink => nil
92
+ ).to_s
93
+ rescue ArgumentError
94
+ raise ArgumentError, "Template \"#{template}\" provided is invalid."
95
+ end
96
+
97
+ def permalink
98
+ data&.is_a?(Hash) && data["permalink"]
99
+ end
100
+
101
+ # Produce a title object suitable for Liquid based on type of archive.
102
+ #
103
+ # Returns a String (for tag and category archives) and nil for
104
+ # date-based archives.
105
+ def title
106
+ if @title.is_a? String
107
+ @title
108
+ elsif @type == 'category-tag'
109
+ @title.values.join(@config.fetch('separator', ' / '))
110
+ end
111
+ end
112
+
113
+ # Produce a date object if a date-based archive
114
+ #
115
+ # Returns a Date.
116
+ def date
117
+ return unless date?
118
+
119
+ @date ||= begin
120
+ args = @title.values.map(&:to_i)
121
+ Date.new(*args)
122
+ end
123
+ end
124
+
125
+ def category
126
+ @title[:category] if @title.is_a? Hash
127
+ end
128
+
129
+ def tag
130
+ @title[:tag] if @title.is_a? Hash
131
+ end
132
+
133
+ # Obtain the write path relative to the destination directory
134
+ #
135
+ # Returns the destination relative path String.
136
+ def relative_path
137
+ @relative_path ||= begin
138
+ path = URL.unescape_path(url).gsub(%r!^/!, "")
139
+ path = File.join(path, "index.html") if url.end_with?("/")
140
+ path
141
+ end
142
+ end
143
+
144
+ # Returns the object as a debug String.
145
+ def inspect
146
+ "#<Jekyll:Archive @type=#{@type} @title=#{@title} @data=#{@data.inspect}>"
147
+ end
148
+
149
+ def date?
150
+ %w[year month day].include? @type
151
+ end
152
+
153
+ private
154
+
155
+ # Generate slug if @title attribute is a string.
156
+ #
157
+ # Note: mode other than those expected by Jekyll returns the given string after
158
+ # downcasing it.
159
+ def slugify_string_title
160
+ return unless title.is_a?(String)
161
+
162
+ mode = @config["slug_mode"] || @config["slug"]
163
+
164
+ Utils.slugify(title, :mode => mode)
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Archives
5
+ VERSION = "2.2.2"
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'jekyll-archives'
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sutty-archives
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Alfred Xing
8
+ - f
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-07-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jekyll
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '3.6'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '5.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '3.6'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: bundler
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: minitest
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: rdoc
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: rubocop-jekyll
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ - !ruby/object:Gem::Dependency
105
+ name: shoulda
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ description: Automatically generate post archives by dates, and any front matter attribute.
119
+ email:
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - lib/jekyll-archives.rb
125
+ - lib/jekyll-archives/archive.rb
126
+ - lib/jekyll-archives/version.rb
127
+ - lib/sutty-archives.rb
128
+ homepage: https://0xacab.org/sutty/jekyll/sutty-archives
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 2.3.0
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubygems_version: 3.0.3
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Post archives for Jekyll. Fork of jekyll-archives.
151
+ test_files: []