jekyll-tally-tags 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jekyll-tally-tags.rb +205 -0
  3. data/lib/jekyll-tally-tags/docs_to_items.rb +151 -0
  4. data/lib/jekyll-tally-tags/item.rb +90 -0
  5. data/lib/jekyll-tally-tags/items_to_page.rb +128 -0
  6. data/lib/jekyll-tally-tags/methods.rb +44 -0
  7. data/lib/jekyll-tally-tags/pages/base_page.rb +284 -0
  8. data/lib/jekyll-tally-tags/pages/day_page.rb +14 -0
  9. data/lib/jekyll-tally-tags/pages/month_page.rb +14 -0
  10. data/lib/jekyll-tally-tags/pages/multiple_page.rb +14 -0
  11. data/lib/jekyll-tally-tags/pages/single_page.rb +14 -0
  12. data/lib/jekyll-tally-tags/pages/week_page.rb +14 -0
  13. data/lib/jekyll-tally-tags/pages/weeks_page.rb +14 -0
  14. data/lib/jekyll-tally-tags/pages/year_page.rb +70 -0
  15. data/lib/jekyll-tally-tags/{counter.rb → utils.rb} +173 -152
  16. data/lib/jekyll-tally-tags/version.rb +72 -54
  17. metadata +16 -25
  18. data/.gitignore +0 -8
  19. data/.idea/.gitignore +0 -8
  20. data/.idea/inspectionProfiles/Project_Default.xml +0 -6
  21. data/.idea/jekyll-tally-tags.iml +0 -44
  22. data/.idea/misc.xml +0 -4
  23. data/.idea/modules.xml +0 -8
  24. data/.idea/vcs.xml +0 -6
  25. data/.rubocop.yml +0 -13
  26. data/CHANGELOG.md +0 -5
  27. data/CODE_OF_CONDUCT.md +0 -84
  28. data/Gemfile +0 -12
  29. data/Gemfile.lock +0 -74
  30. data/LICENSE.txt +0 -21
  31. data/README.md +0 -43
  32. data/Rakefile +0 -5
  33. data/bin/console +0 -15
  34. data/bin/setup +0 -8
  35. data/jekyll-tally-tags.gemspec +0 -28
  36. data/lib/jekyll-tally-tags/classify.rb +0 -196
  37. data/lib/jekyll-tally-tags/hooks_doc.rb +0 -134
  38. data/lib/jekyll-tally-tags/hooks_logs.rb +0 -57
  39. data/lib/jekyll-tally-tags/subject.rb +0 -143
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Danyow
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,43 +0,0 @@
1
- # Jekyll::Tally::Tags
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jekyll/tally/tags`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'jekyll-tally-tags'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install jekyll-tally-tags
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll-tally-tags. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jekyll-tally-tags/blob/master/CODE_OF_CONDUCT.md).
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the Jekyll::TallyTags project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jekyll-tally-tags/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
-
5
- task default: :rubocop
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "bundler/setup"
5
- require "jekyll/tally/tags"
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require "irb"
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/jekyll-tally-tags/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "jekyll-tally-tags"
7
- spec.version = Jekyll::TallyTags::VERSION
8
- spec.authors = ["Danyow"]
9
- spec.email = ["i.zuucol@gmail.com"]
10
-
11
- spec.summary = "一个基于 *Tags* 的计数插件."
12
- spec.description = "开箱即用!"
13
- spec.homepage = "https://github.com/danyow/jekyll-tally-tags"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.4.0"
16
-
17
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
-
19
- # Specify which files should be added to the gem when it is released.
20
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
- end
24
-
25
- spec.add_dependency "jekyll", ">= 3.6", "< 5.0"
26
-
27
- spec.add_development_dependency "bundler"
28
- end
@@ -1,196 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jekyll'
4
-
5
- module Jekyll
6
- module TallyTags
7
-
8
- class Classify < Jekyll::Generator
9
-
10
- safe true
11
- # 默认配置
12
- DEFAULT = {
13
- "layout" => "classify",
14
- "enabled" => [],
15
- "permalinks" => {
16
- "year" => "/:class/:year/",
17
- "year_week" => "/:class/:year/:week/",
18
- "month" => "/:class/:year/:month/",
19
- "month_week" => "/:class/:year/:month/:week",
20
- "day" => "/:class/:year/:month/:day/",
21
- "week" => "/:class/:week/",
22
- },
23
- }.freeze
24
-
25
- # @param [Configuration] config
26
- def initialize(config = nil)
27
- classify_config = config.fetch(CLASSIFY, {})
28
- if classify_config.is_a?(Hash)
29
- @config = Utils.deep_merge_hashes(DEFAULT, classify_config)
30
- else
31
- @config = nil
32
- Jekyll.logger.warn CLASSIFY, "预估是 `Hash`, 但获得的是 #{classify_config.inspect} 类型."
33
- end
34
- @enabled = @config && @config[ENABLED]
35
- end
36
-
37
- # @param [Site] site
38
- def generate(site)
39
- # 判断是否为空
40
- return if @config.nil?
41
- # 开始赋值
42
- @site = site
43
- @posts = site.posts
44
- @subjects = []
45
-
46
- @site.config[CLASSIFY] = @config
47
- read_all(@config[PERMALINKS], @site.posts.docs)
48
- # 把所有的拼接到到 `pages` 里面
49
- @site.pages.concat(@subjects)
50
- # 配置里面也放一份
51
- @site.config[SUBJECTS] = @subjects
52
- end
53
-
54
- # @param [Hash{String => String}] permalinks
55
- # @param [Array<Document>] scan_docs
56
- def read_all(permalinks, scan_docs)
57
- permalinks.each do |key, permalink|
58
- # 判断 链接是否包含 :xxx :(\w+)
59
- matches = []
60
- permalink.scan(/:(\w+)/) { |match| matches << match[0] }
61
- read_loop(key, {}, Array.new(scan_docs), matches, 0)
62
- end
63
- end
64
-
65
- # @param [String] permalink_key
66
- # @param [Hash{Symbol => String}] titles
67
- # @param [Array<Document>] docs
68
- # @param [Array<String>] matches
69
- # @param [Integer] index
70
- def read_loop(permalink_key, titles, docs, matches, index)
71
- if index > matches.size - 1
72
- return
73
- end
74
- match = matches[index]
75
- # 找到对应的 docs
76
- method = self.method("#{READ_METHOD}#{match}")
77
- if method
78
- # 获取 `docs_hash` 和 `symbol`
79
- docs_hash, symbol = method.call(docs)
80
- read_any(docs_hash, symbol, permalink_key, titles, matches, index + 1)
81
- # method.call(permalink_key, titles, docs, matches, index + 1)
82
- else
83
- Jekyll.logger.warn CLASSIFY, "不正确的 #{PERMALINKS} 参数:#{match}"
84
- end
85
- end
86
-
87
- # @param [Hash{String => Array<Document>}] docs_hash
88
- # @param [Symbol] symbol
89
- # @param [String] permalink_key
90
- # @param [Hash{Symbol => String}] titles
91
- # @param [Array<String>] matches
92
- # @param [Integer] index
93
- def read_any(docs_hash, symbol, permalink_key, titles, matches, index)
94
- docs_hash.each do |key, docs|
95
- new_titles = titles.merge({ symbol => key })
96
- # 开启了该字段 同时 是匹配的最后一项的时候 写入数组
97
- if enabled?(permalink_key) && index == matches.size
98
- @subjects << Subject.new(@site, new_titles, permalink_key, new_titles.values, docs)
99
- end
100
- read_loop(permalink_key, new_titles, docs, matches, index)
101
- end
102
- end
103
-
104
- # @param [Array<Document>] scan_docs
105
- # @return [Hash{String => Array<Document>}, Symbol]
106
- def read_year(scan_docs)
107
- return date_attr_hash(scan_docs, "%Y"), :year
108
- end
109
-
110
- # @param [Array<Document>] scan_docs
111
- # @return [Hash{String => Array<Document>}, Symbol]
112
- def read_month(scan_docs)
113
- return date_attr_hash(scan_docs, "%m"), :month
114
- end
115
-
116
- # @param [Array<Document>] scan_docs
117
- # @return [Hash{String => Array<Document>}, Symbol]
118
- def read_day(scan_docs)
119
- return date_attr_hash(scan_docs, "%d"), :day
120
- end
121
-
122
- # @param [Array<Document>] scan_docs
123
- # @return [Hash{String => Array<Document>}, Symbol]
124
- def read_week(scan_docs)
125
- return date_attr_hash(scan_docs, "%w"), :week
126
- end
127
-
128
- # @param [Array<Document>] scan_docs
129
- # @return [Hash{String => Array<Document>}, Symbol]
130
- def read_subject(scan_docs)
131
- return docs_attr_hash(scan_docs, SUBJECT), :subject
132
- end
133
-
134
- # @param [Array<Document>] scan_docs
135
- # @return [Hash{String => Array<Document>}, Symbol]
136
- def read_subjects(scan_docs)
137
- return docs_attrs_hash(scan_docs, SUBJECTS), :subjects
138
- end
139
-
140
- # @param [Array<Document>] scan_docs
141
- # @return [Hash{String => Array<Document>}, Symbol]
142
- def read_class(scan_docs)
143
- return docs_attr_hash(scan_docs, CLASS), :class
144
- end
145
-
146
- # @param [Array<Document>] scan_docs
147
- # @return [Hash{String => Array<Document>}, Symbol]
148
- def read_classes(scan_docs)
149
- return docs_attrs_hash(scan_docs, CLASSES), :classes
150
- end
151
-
152
- private
153
-
154
- # @param [String] type
155
- def enabled?(type)
156
- @enabled == true || @enabled == ALL || (@enabled.is_a?(Array) && @enabled.include?(type))
157
- end
158
-
159
- # @param [Array<Document>] docs
160
- # @param [String] attr
161
- # @return [Hash{String => Array<Document>}]
162
- def docs_attr_hash(docs, attr)
163
- hash = Hash.new { |h, key| h[key] = [] }
164
- docs.each do |doc|
165
- doc.data[attr]&.each { |key| hash[key] << doc }
166
- end
167
- hash.each_value { |docs| docs.sort!.reverse! }
168
- hash
169
- end
170
-
171
- # @param [Array<Document>] docs
172
- # @param [String] attr
173
- # @return [Hash{String => Array<Document>}]
174
- def docs_attrs_hash(docs, attr)
175
- hash = Hash.new { |h, key| h[key] = [] }
176
- docs.each do |doc|
177
- doc.data[attr]&.each { |keys| hash[keys.join(",")] << doc }
178
- end
179
- hash.each_value { |docs| docs.sort!.reverse! }
180
- hash
181
- end
182
-
183
- # @param [Array<Document>] docs `yaml` 头部信息
184
- # @param [String] id ISO-8601
185
- # @return [Hash{String=>Array<Document>}]
186
- def date_attr_hash(docs, id)
187
- hash = Hash.new { |h, k| h[k] = [] }
188
- docs.each { |doc| hash[doc.date.strftime(id)] << doc }
189
- hash.each_value { |docs| docs.sort!.reverse! }
190
- hash
191
- end
192
-
193
- end
194
-
195
- end
196
- end
@@ -1,134 +0,0 @@
1
- module Jekyll
2
- module TallyTags
3
-
4
- Jekyll::Hooks.register :site, :after_init do |_|
5
- # 修改正则让 `2021-01-01.md` 就算无后缀也可读
6
- old = Jekyll::Document::DATE_FILENAME_MATCHER
7
- new = NO_NAME
8
- if old != new
9
- Jekyll::Document::DATE_FILENAME_MATCHER = new
10
- end
11
- end
12
-
13
- Jekyll::Hooks.register :site, :post_read do |site|
14
-
15
- # 首先获取所有在 `tally` 内 `list` 的值
16
- tally_configs = site.config.fetch(TALLY, {})
17
- next unless tally_configs && !tally_configs.empty?
18
- # 然后获取默认配置 没有也是可以的
19
- # @type [Hash<String => Array<Integer, String>>]
20
- default_template = tally_configs[DEFAULT]
21
-
22
- if default_template
23
- # 初始化默认的模板
24
- default_template.each_key do |key|
25
- default_template[key] = Counter.to_array(default_template[key], nil)
26
- end
27
- end
28
-
29
- # 先判断有没有对应的配置
30
- # @type [Hash<String => Array<Integer, String>>]
31
- templates = tally_configs[TEMPLATES]
32
- # 必须有模板才可以解析
33
- next unless templates && !templates.empty?
34
-
35
- # @type [Array<Document>]
36
- docs = site.posts.docs # 文章里的文档 (也就是 `yaml`)
37
- id = 0 # id
38
-
39
- no_scan_docs = [] # 无法扫描的文档
40
- scanned_docs = [] # 创建新的文档
41
-
42
- # 先遍历模板
43
- templates.each_key do |template_key|
44
- template = templates[template_key]
45
- template.each_key do |key|
46
- # 依据默认模板生成新的模板
47
- template[key] = Counter.to_array(template[key], default_template[key])
48
- end
49
- end
50
-
51
- # 后便利文档
52
- docs.each do |doc|
53
- # 判断这个文档里面有没有对应的 `template`
54
- doc_has_template = doc.data.keys & templates.keys
55
- if !doc_has_template || doc_has_template.empty?
56
- no_scan_docs << doc
57
- next
58
- end
59
-
60
- doc_has_template.each do |template_key|
61
- csv_list = doc[template_key]
62
- template = templates[template_key]
63
- # 下一步 如果 有值 且 不为空
64
- next unless csv_list && !csv_list.empty?
65
-
66
- # 如果不是数组的话
67
- if !csv_list.is_a?(Array)
68
- Jekyll.logger.warn(template_key, "#{doc.path}里的数据不为数组, 将不解析该字段")
69
- next
70
- end
71
-
72
- keys = []
73
- csv_list.each_index do |csv_index|
74
- csv = csv_list[csv_index]
75
- # 正则处理 把",,,"这样的 分割成" , , , "
76
- csv.gsub!(/[,|,|\s]+?/, " , ")
77
- # lstrip rstrip 去掉前后空格
78
- # @type [Array<String>]
79
- values = csv.split(',').each(&:lstrip!).each(&:rstrip!)
80
- # 判断有没有 `keys` 如果没有 第一行就作为 `keys` 因为第一行作为 `keys` 就是像极了 `csv`
81
- if (!template[KEYS] || template[KEYS].empty?) && csv_index == 0
82
- keys = values
83
- next
84
- end
85
- # 初始化数据
86
- datum = { ID => id, PERMALINK => "/#{ID}/#{id}" }
87
- # 对当前 `template` 所有 `key` 遍历
88
- template.each_key do |key|
89
- datum[key] = Counter.formatValues(values, template[key])
90
- end
91
-
92
- # 对 `values` 所有 内容 遍历
93
- values.each_index do |index|
94
- datum[keys[index]] = values[index]
95
- end
96
-
97
- # 可能会死循环 `Document.new` 还会发消息
98
- # TODO: 后续 采用 `Document` 子类
99
- new_doc = Document.new(doc.path, site: site, collection: site.posts)
100
- new_doc.data.replace(doc.data)
101
- new_doc.data[DATA] = {}
102
- # 重新赋值
103
- datum.each_key do |key|
104
- Counter.set_doc_data(new_doc, key, datum[key])
105
- end
106
- scanned_docs << new_doc
107
- id += 1
108
- end
109
- end
110
- end
111
- # @type [Array<Document>]
112
- all_docs = no_scan_docs + scanned_docs
113
- # 判断是不是需要 开启组合模式
114
- combine_configs = tally_configs[COMBINE]
115
- if combine_configs && combine_configs.is_a?(Array)
116
- combine_configs.each do |config|
117
- find_key = config[FIND]
118
- combine_key = config[TO]
119
- deep_key = config[DEEP]
120
- all_find_keys = []
121
- all_docs.each do |doc|
122
- doc.data[combine_key] = []
123
- # 找到所有 `key`
124
- all_find_keys += doc.data[find_key]
125
- end
126
- merges = Counter.merge_all(all_find_keys.uniq!, deep_key)
127
- Counter.combine_merge_list(merges, all_docs, find_key, combine_key)
128
- end
129
- end
130
-
131
- site.posts.docs = all_docs
132
- end
133
- end
134
- end