jekyll-archives-mlc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 055ab3532b6c54da1c34b367a73fd9ccf3a59e58fbcdb70ff48ee56a5ad8c0b9
4
+ data.tar.gz: eaecc7fffc0e06fcf4e9bf482fcc3f61eec8492072a9b8986c82990211d08501
5
+ SHA512:
6
+ metadata.gz: 05a8f551dd1ed2823b4e5516909721a8c27e717368ea1c1026324d101cd3796c01e52deacf4fd008c660d6cd76a9d5ef982aa0775fed82d1fec1130359ea04a8
7
+ data.tar.gz: d9146445d67977d6e05d7690c8eb265935c702ed2ece85037b50d6f52ae76d5bac5b6b91b9452b726041f8b2a5e120b860194cafd7c530116ae77bb1c01cdd87
@@ -0,0 +1,136 @@
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
+ ).freeze
19
+
20
+ # Initialize a new Archive page
21
+ #
22
+ # site - The Site object.
23
+ # title - The name of the tag/category or a Hash of the year/month/day in case of date.
24
+ # e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
25
+ # type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
26
+ # posts - The array of posts that belong in this archive.
27
+ def initialize(site, title, type, posts)
28
+ @site = site
29
+ @posts = posts
30
+ @type = type
31
+ @title = title
32
+ @config = site.config["jekyll-archives"]
33
+ @slug = slugify_string_title
34
+
35
+ # Use ".html" for file extension and url for path
36
+ @ext = File.extname(relative_path)
37
+ @path = relative_path
38
+ @name = File.basename(relative_path, @ext)
39
+
40
+ @data = {
41
+ "layout" => layout,
42
+ }
43
+ @content = ""
44
+ end
45
+
46
+ # The template of the permalink.
47
+ #
48
+ # Returns the template String.
49
+ def template
50
+ @config.dig("permalinks", type)
51
+ end
52
+
53
+ # The layout to use for rendering
54
+ #
55
+ # Returns the layout as a String
56
+ def layout
57
+ @config.dig("layouts", type) || @config["layout"]
58
+ end
59
+
60
+ # Returns a hash of URL placeholder names (as symbols) mapping to the
61
+ # desired placeholder replacements. For details see "url.rb".
62
+ def url_placeholders
63
+ if @title.is_a? Hash
64
+ @title.merge(:type => @type)
65
+ else
66
+ { :name => @slug, :type => @type }
67
+ end
68
+ end
69
+
70
+ # The generated relative url of this page. e.g. /about.html.
71
+ #
72
+ # Returns the String url.
73
+ def url
74
+ @url ||= URL.new(
75
+ :template => template,
76
+ :placeholders => url_placeholders,
77
+ :permalink => nil
78
+ ).to_s
79
+ rescue ArgumentError
80
+ raise ArgumentError, "Template \"#{template}\" provided is invalid."
81
+ end
82
+
83
+ def permalink
84
+ data&.is_a?(Hash) && data["permalink"]
85
+ end
86
+
87
+ # Produce a title object suitable for Liquid based on type of archive.
88
+ #
89
+ # Returns a String (for tag and category archives) and nil for
90
+ # date-based archives.
91
+ def title
92
+ @title if @title.is_a? String
93
+ end
94
+
95
+ # Produce a date object if a date-based archive
96
+ #
97
+ # Returns a Date.
98
+ def date
99
+ return unless @title.is_a?(Hash)
100
+
101
+ @date ||= begin
102
+ args = @title.values.map(&:to_i)
103
+ Date.new(*args)
104
+ end
105
+ end
106
+
107
+ # Obtain the write path relative to the destination directory
108
+ #
109
+ # Returns the destination relative path String.
110
+ def relative_path
111
+ @relative_path ||= begin
112
+ path = URL.unescape_path(url).gsub(%r!^/!, "")
113
+ path = File.join(path, "index.html") if url.end_with?("/")
114
+ path
115
+ end
116
+ end
117
+
118
+ # Returns the object as a debug String.
119
+ def inspect
120
+ "#<Jekyll:Archive @type=#{@type} @title=#{@title} @data=#{@data.inspect}>"
121
+ end
122
+
123
+ private
124
+
125
+ # Generate slug if @title attribute is a string.
126
+ #
127
+ # Note: mode other than those expected by Jekyll returns the given string after
128
+ # downcasing it.
129
+ def slugify_string_title
130
+ return unless title.is_a?(String)
131
+
132
+ Utils.slugify(title, :mode => @config["slug_mode"])
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Archives
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,140 @@
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
+ DEFAULTS = {
15
+ "layout" => "archive",
16
+ "enabled" => [],
17
+ "permalinks" => {
18
+ "year" => "/:year/",
19
+ "month" => "/:year/:month/",
20
+ "day" => "/:year/:month/:day/",
21
+ "tag" => "/tag/:name/",
22
+ "category" => "/category/:name/",
23
+ },
24
+ }.freeze
25
+
26
+ def initialize(config = {})
27
+ archives_config = config.fetch("jekyll-archives", {})
28
+ if archives_config.is_a?(Hash)
29
+ @config = Utils.deep_merge_hashes(DEFAULTS, archives_config)
30
+ else
31
+ @config = nil
32
+ Jekyll.logger.warn "Archives:", "Expected a hash but got #{archives_config.inspect}"
33
+ Jekyll.logger.warn "", "Archives will not be generated for this site."
34
+ end
35
+ @enabled = @config && @config["enabled"]
36
+ end
37
+
38
+ def generate(site)
39
+ return if @config.nil?
40
+
41
+ @site = site
42
+ @posts = site.posts
43
+ @archives = []
44
+
45
+ @site.config["jekyll-archives"] = @config
46
+
47
+ read
48
+ @site.pages.concat(@archives)
49
+
50
+ @site.config["archives"] = @archives
51
+ end
52
+
53
+ # Read archive data from posts
54
+ def read
55
+ read_tags
56
+ read_categories
57
+ read_dates
58
+ end
59
+
60
+ def read_tags
61
+ if enabled? "tags"
62
+ tags.each do |title, posts|
63
+ @archives << Archive.new(@site, title, "tag", posts)
64
+ end
65
+ end
66
+ end
67
+
68
+ def read_categories
69
+ if enabled? "categories"
70
+ categories.each do |title, posts|
71
+ @archives << Archive.new(@site, title, "category", posts)
72
+ end
73
+ end
74
+ end
75
+
76
+ def read_dates
77
+ years.each do |year, y_posts|
78
+ append_enabled_date_type({ :year => year }, "year", y_posts)
79
+ months(y_posts).each do |month, m_posts|
80
+ append_enabled_date_type({ :year => year, :month => month }, "month", m_posts)
81
+ days(m_posts).each do |day, d_posts|
82
+ append_enabled_date_type({ :year => year, :month => month, :day => day }, "day", d_posts)
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ # Checks if archive type is enabled in config
89
+ def enabled?(archive)
90
+ @enabled == true || @enabled == "all" || (@enabled.is_a?(Array) && @enabled.include?(archive))
91
+ end
92
+
93
+ def tags
94
+ @site.tags
95
+ end
96
+
97
+ def categories
98
+ @site.categories
99
+ end
100
+
101
+ # Custom `post_attr_hash` method for years
102
+ def years
103
+ date_attr_hash(@posts.docs, "%Y")
104
+ end
105
+
106
+ # Custom `post_attr_hash` method for months
107
+ def months(year_posts)
108
+ date_attr_hash(year_posts, "%m")
109
+ end
110
+
111
+ # Custom `post_attr_hash` method for days
112
+ def days(month_posts)
113
+ date_attr_hash(month_posts, "%d")
114
+ end
115
+
116
+ private
117
+
118
+ # Initialize a new Archive page and append to base array if the associated date `type`
119
+ # has been enabled by configuration.
120
+ #
121
+ # meta - A Hash of the year / month / day as applicable for date.
122
+ # type - The type of date archive.
123
+ # posts - The array of posts that belong in the date archive.
124
+ def append_enabled_date_type(meta, type, posts)
125
+ @archives << Archive.new(@site, meta, type, posts) if enabled?(type)
126
+ end
127
+
128
+ # Custom `post_attr_hash` for date type archives.
129
+ #
130
+ # posts - Array of posts to be considered for archiving.
131
+ # id - String used to format post date via `Time.strptime` e.g. %Y, %m, etc.
132
+ def date_attr_hash(posts, id)
133
+ hash = Hash.new { |hsh, key| hsh[key] = [] }
134
+ posts.each { |post| hash[post.date.strftime(id)] << post }
135
+ hash.each_value { |posts| posts.sort!.reverse! }
136
+ hash
137
+ end
138
+ end
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-archives-mlc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alfred Xing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.6'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rdoc
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop-jekyll
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.9'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.9'
103
+ - !ruby/object:Gem::Dependency
104
+ name: shoulda
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Automatically generate post archives by dates, tags, and categories.
118
+ email:
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - lib/jekyll-archives.rb
124
+ - lib/jekyll-archives/archive.rb
125
+ - lib/jekyll-archives/version.rb
126
+ homepage: https://github.com/bijianing/jekyll-archives-mlc
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 2.3.0
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.1.6
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Post archives for Jekyll.
149
+ test_files: []