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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9f3b50581391cccac7bc2d547b81e9743ac18b50b67d1f45edb1ce33375bf5d
4
- data.tar.gz: 6315e0acc2cb6ef41502c9c2f01802eda8cdc902f59a00fdd1a73f27e5671a41
3
+ metadata.gz: 5903dcfca2ed55cd34e7a86a40ca2565b2aa7981f2a3fe81d04a35d6a2bbe558
4
+ data.tar.gz: 4ece7a9e9aaa7ddbf1cfe54efea8f03d7c534c59e273d6f2cb501d71c7131346
5
5
  SHA512:
6
- metadata.gz: fa45959ee8ef70b6bb4fd57471e726c64291a778d53daf4a1742afabf4f6adc040b0c0ed7fc9ccbefaaf12fb9420b8da4d71b0ea817ab126d10d7d9619e4c9f4
7
- data.tar.gz: f3cade603df833c774ab36c095132e81b9f0083186dd823716d0909a7b7ab5a34f92745050aa11d89a93107deca98c8ee807d4c9085c3c648f266e6259b581be
6
+ metadata.gz: dfb1fa03b7ced48a5f6ee40cf228602166f4f063a877cc558ad6a61b045e0917b111ac4ce4ac4dc32fc02aa2a0c9afb064b8dac974017b0bb521eeea2704002c
7
+ data.tar.gz: 19dc53b00954e5f764310c8ee2438b6d7463dca92ccac0b3b32ca67f03146429030c14586cfb5a5b957737c390729c0885db5469f9cae2648588579ad5a419da
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+ require "jekyll-tally-tags/version"
5
+
6
+ module Jekyll
7
+ module TallyTags
8
+
9
+ autoload :DocsToItems, "jekyll-tally-tags/docs_to_items"
10
+ autoload :Item, "jekyll-tally-tags/item"
11
+ autoload :ItemsToPage, "jekyll-tally-tags/items_to_page"
12
+ autoload :Methods, "jekyll-tally-tags/methods"
13
+ autoload :Utils, "jekyll-tally-tags/utils"
14
+ autoload :BasePage, "jekyll-tally-tags/pages/base_page"
15
+ autoload :DayPage, "jekyll-tally-tags/pages/day_page"
16
+ autoload :MonthPage, "jekyll-tally-tags/pages/month_page"
17
+ autoload :MultiplePage, "jekyll-tally-tags/pages/multiple_page"
18
+ autoload :SinglePage, "jekyll-tally-tags/pages/single_page"
19
+ autoload :WeeksPage, "jekyll-tally-tags/pages/weeks_page"
20
+ autoload :WeekPage, "jekyll-tally-tags/pages/week_page"
21
+ autoload :YearPage, "jekyll-tally-tags/pages/year_page"
22
+
23
+ DEBUG = false
24
+
25
+ Jekyll::Hooks.register :site, :after_init do |_|
26
+ puts 'site after_init 在网站初始化时,但是在设置和渲染之前,适合用来修改网站的配置项' if DEBUG
27
+ # 修改正则让 `2021-01-01.md` 就算无后缀也可读
28
+ Jekyll::Document::DATE_FILENAME_MATCHER = NO_NAME
29
+ end
30
+
31
+ Jekyll::Hooks.register :site, :post_read do |site|
32
+ puts 'site post_read 在网站数据从磁盘中读取并加载之后' if DEBUG
33
+ DocsToItems.read(site)
34
+ end
35
+
36
+ class ItemsToPage < Jekyll::Generator
37
+ safe true
38
+ # @param [Configuration] config
39
+ def initialize(config = nil)
40
+ @config = Utils.get_permalink_config(config)
41
+ @enabled = Utils.get_permalink_config(config, ENABLED)
42
+ end
43
+
44
+ # @param [Site] site
45
+ def generate(site)
46
+ # 判断是否为空
47
+ return if @config.nil?
48
+ # 开始赋值
49
+ @site = site
50
+ @posts = site.posts
51
+ @pages = []
52
+
53
+ read_all(@config[PERMALINKS], @site.posts.docs)
54
+ # 把所有的拼接到到 `pages` 里面
55
+ @site.pages.concat(@pages)
56
+ # 配置里面也放一份
57
+ @site.config[PAGES] = @pages
58
+ end
59
+
60
+ # @param [Hash{String => String}] permalinks
61
+ # @param [Array<Document>] scan_docs
62
+ def read_all(permalinks, scan_docs)
63
+ permalinks.each do |key, permalink|
64
+ # 判断 链接是否包含 :xxx :(\w+)
65
+ matches = []
66
+ permalink.scan(/:(\w+)/) { |match| matches << match[0] }
67
+ read_loop(key, {}, Array.new(scan_docs), matches, 0)
68
+ end
69
+ end
70
+
71
+ # @param [String] permalink_key
72
+ # @param [Hash{Symbol => String}] titles
73
+ # @param [Array<Document>] docs
74
+ # @param [Array<String>] matches
75
+ # @param [Integer] index
76
+ def read_loop(permalink_key, titles, docs, matches, index)
77
+ if index > matches.size - 1
78
+ return
79
+ end
80
+ match = matches[index]
81
+ # 找到对应的 docs
82
+ if DATE_HASH.keys.include?(match)
83
+ docs_hash = date_to_docs_hash(docs, DATE_HASH[match])
84
+ read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
85
+ else
86
+ begin
87
+ docs_hash = tags_to_docs_hash(docs, match)
88
+ read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
89
+ rescue
90
+ Jekyll.logger.warn CLASSIFY, "没有该keys ':#{match}'"
91
+ end
92
+ end
93
+ end
94
+
95
+ # @param [Hash{String => Array<Document>}] docs_hash
96
+ # @param [Symbol] symbol
97
+ # @param [String] permalink_key
98
+ # @param [Hash{Symbol => String}] titles
99
+ # @param [Array<String>] matches
100
+ # @param [Integer] index
101
+ def read_any(docs_hash, symbol, permalink_key, titles, matches, index)
102
+ docs_hash.each do |key, docs|
103
+ new_titles = titles.merge({ symbol => key })
104
+ # 开启了该字段 同时 是匹配的最后一项的时候 写入数组
105
+ if enabled?(permalink_key) && index == matches.size
106
+ clz = SinglePage
107
+ case permalink_key
108
+ when YEAR
109
+ clz = YearPage
110
+ when MONTH
111
+ clz = MonthPage
112
+ when DAY
113
+ clz = DayPage
114
+ when WEEK
115
+ clz = WeekPage
116
+ when WEEKS
117
+ clz = WeekPage
118
+ end
119
+ @pages << clz.new(@site, new_titles, permalink_key, docs.keep_if { |doc| doc.data[TEMPLATE] })
120
+ end
121
+ read_loop(permalink_key, new_titles, docs, matches, index)
122
+ end
123
+ end
124
+
125
+ private
126
+
127
+ # @param [String] type
128
+ def enabled?(type)
129
+ @enabled == true || @enabled == ALL || (@enabled.is_a?(Array) && @enabled.include?(type))
130
+ end
131
+
132
+ # @param [Array<Document>] docs
133
+ # @param [String] attr
134
+ # @return [Hash{String => Array<Document>}]
135
+ def tags_to_docs_hash(docs, attr)
136
+ hash = Hash.new { |h, key| h[key] = [] }
137
+ docs.each do |doc|
138
+ doc.data[attr]&.each { |key| hash[key] << doc }
139
+ end
140
+ hash.each_value { |docs| docs.sort!.reverse! }
141
+ hash
142
+ end
143
+
144
+ # @param [Array<Document>] docs `yaml` 头部信息
145
+ # @param [String] id ISO-8601
146
+ # @return [Hash{String=>Array<Document>}]
147
+ def date_to_docs_hash(docs, id)
148
+ hash = Hash.new { |h, k| h[k] = [] }
149
+ docs.each { |doc| hash[doc.date.strftime(id)] << doc }
150
+ hash.each_value { |docs| docs.sort!.reverse! }
151
+ hash
152
+ end
153
+
154
+ end
155
+
156
+ Jekyll::Hooks.register :site, :after_reset do |_|
157
+ puts 'site after_reset 网站重置之后' if DEBUG
158
+ end
159
+ Jekyll::Hooks.register :site, :pre_render do |_|
160
+ puts 'site pre_render 在渲染整个网站之前' if DEBUG
161
+ end
162
+ Jekyll::Hooks.register :site, :post_render do |_|
163
+ puts 'site post_render 在渲染整个网站之后,但是在写入任何文件之前' if DEBUG
164
+ end
165
+ Jekyll::Hooks.register :site, :post_write do |_|
166
+ puts 'site post_write 在将整个网站写入磁盘之后' if DEBUG
167
+ end
168
+ Jekyll::Hooks.register :pages, :post_init do |_|
169
+ puts 'pages post_init 每次页面被初始化的时候' if DEBUG
170
+ end
171
+ Jekyll::Hooks.register :pages, :pre_render do |_|
172
+ puts 'pages pre_render 在渲染页面之前' if DEBUG
173
+ end
174
+ Jekyll::Hooks.register :pages, :post_render do |_|
175
+ puts 'pages post_render 在页面渲染之后,但是在页面写入磁盘之前' if DEBUG
176
+ end
177
+ Jekyll::Hooks.register :pages, :post_write do |_|
178
+ puts 'pages post_write 在页面写入磁盘之后' if DEBUG
179
+ end
180
+ Jekyll::Hooks.register :posts, :post_init do |_|
181
+ puts 'posts post_init 每次博客被初始化的时候' if DEBUG
182
+ end
183
+ Jekyll::Hooks.register :posts, :pre_render do |_|
184
+ puts 'posts pre_render 在博客被渲染之前' if DEBUG
185
+ end
186
+ Jekyll::Hooks.register :posts, :post_render do |_|
187
+ puts 'posts post_render 在博客渲染之后,但是在被写入磁盘之前' if DEBUG
188
+ end
189
+ Jekyll::Hooks.register :posts, :post_write do |_|
190
+ puts 'posts post_write 在博客被写入磁盘之后' if DEBUG
191
+ end
192
+ Jekyll::Hooks.register :documents, :post_init do |_|
193
+ puts 'documents post_init 每次文档被初始化的时候' if DEBUG
194
+ end
195
+ Jekyll::Hooks.register :documents, :pre_render do |_|
196
+ puts 'documents pre_render 在渲染文档之前' if DEBUG
197
+ end
198
+ Jekyll::Hooks.register :documents, :post_render do |_|
199
+ puts 'documents post_render 在渲染文档之后,但是在被写入磁盘之前' if DEBUG
200
+ end
201
+ Jekyll::Hooks.register :documents, :post_write do |_|
202
+ puts 'documents post_write 在文档被写入磁盘之后' if DEBUG
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ module Jekyll
6
+ module TallyTags
7
+ class DocsToItems
8
+ # @param [Site] site
9
+ def self.read(site)
10
+ templates = Utils.get_templates(site.config)
11
+ return unless templates
12
+ date_hash = insert_to_date_hash(templates, site.posts.docs)
13
+ # 排序
14
+ date_hash.sort_by { |a, b| a }
15
+ items = read_csv(date_hash, templates)
16
+ items = combine_tags(items, site)
17
+ create_items(items, site)
18
+ end
19
+
20
+ # @param [Array<Hash{String => Object}>] items
21
+ # @param [Site] site
22
+ def self.create_items(items, site)
23
+ item_objects = []
24
+ items.each do |item|
25
+ item_objects << Item.new(item, site)
26
+ end
27
+ site.posts.docs += item_objects
28
+ end
29
+
30
+ # @param [Array<Hash>] items
31
+ # @param [Site] site
32
+ def self.combine_tags(items, site)
33
+ configs = site.config.fetch(TALLY, {})
34
+ return nil unless configs && !configs.empty?
35
+ if configs[COMBINE] && configs[COMBINE].is_a?(Array)
36
+ configs[COMBINE].each do |config|
37
+ find = config[FIND]
38
+ to = config[TO]
39
+ deep = config[DEEP]
40
+ finds = []
41
+ items.each do |item|
42
+ if item.keys.include?(find)
43
+ # 找到所有 `key`
44
+ finds += item[find]
45
+ end
46
+ end
47
+ finds.uniq!
48
+ if finds.size >= 2
49
+ items.each { |item| item[to] = [] }
50
+ combines = Utils.combine_tags(finds, deep)
51
+ Utils.link_combines(combines, items, find, to)
52
+ end
53
+ end
54
+ end
55
+ items
56
+ end
57
+
58
+ # @param [Hash{Date => Hash{String => Array<String>}}] date_hash
59
+ # @param [Hash{String => Hash}] templates
60
+ # @return [Array[Hash{String => Object}]]
61
+ def self.read_csv(date_hash, templates)
62
+ items = []
63
+ date_hash.each do |date, hash|
64
+ hash.each do |temp, csv_list|
65
+ keys = nil
66
+ csv_list.each_with_index do |csv, index|
67
+ # 正则处理 把",,,"这样的 分割成" , , , "
68
+ csv.gsub!(/[,|,|\s]+/, " , ")
69
+ # lstrip rstrip 去掉前后空格
70
+ values = csv.split(',').each(&:lstrip!).each(&:rstrip!)
71
+ if !keys && index == 0
72
+ keys = values
73
+ next
74
+ end
75
+ dates = {
76
+ DATES => date.to_s,
77
+ YEAR => date.strftime("%Y"),
78
+ MONTH => date.strftime("%m"),
79
+ DAY => date.strftime("%d"),
80
+ WEEKS => date.strftime("%W"),
81
+ WEEK => date.strftime("%w"),
82
+ }
83
+ dates_array = {}
84
+ dates.each { |k, v| dates_array[k] = [v] }
85
+ item = {
86
+ ID => items.size,
87
+ PERMALINK => "/#{ID}/#{items.size}",
88
+ SLUG => dates[DATES],
89
+ DATE => date.to_time,
90
+ TEMPLATE => temp,
91
+ KEYS => keys,
92
+ KEYS_EXTEND => dates.keys + keys,
93
+ VALUES => values,
94
+ VALUES_EXTEND => dates.values + values,
95
+ LAYOUT => DEFAULT
96
+ }.merge(dates_array)
97
+
98
+ # 附加内容
99
+ templates[temp].keep_if { |k, _| k != KEYS }.each do |key, formatters|
100
+ item[key] = Utils.format_values(values, formatters)
101
+ end
102
+ items << item
103
+ end
104
+ end
105
+ end
106
+ items
107
+ end
108
+
109
+ # @example 转为下面形式
110
+ # ``` yml
111
+ # 2001-01-01:
112
+ # - template_key:
113
+ # - A B C D E F G
114
+ # - a b c d e f g
115
+ # ```
116
+ # @param [Hash{String => Hash}] templates
117
+ # @param [Array<Document>] docs
118
+ # @return [Hash{Date => Hash}]
119
+ def self.insert_to_date_hash(templates, docs)
120
+ date_hash = Hash.new { |h, k| h[k] = {} }
121
+ docs.each do |doc|
122
+ need_delete = false
123
+ # 找出 第一层就是模板的
124
+ items = doc.data.select { |k, _| templates.keys.include?(k) }
125
+ # 合并到第二层去
126
+ Utils.inner_merge(date_hash, doc.date.to_date, items)
127
+ # 把所有的日期合并到一起
128
+ doc.data.each do |key, value|
129
+ if key.is_a?(Date)
130
+ need_delete = true
131
+ Utils.inner_merge(date_hash, key.to_date, value)
132
+ end
133
+ end
134
+ # 这个文档可以被处理
135
+ if need_delete || items
136
+ docs.delete(doc)
137
+ end
138
+ end
139
+ # 把模板内自带有 `keys` 的加入进来
140
+ date_hash.each do |date, hash|
141
+ hash.each do |temp_key, csv_list|
142
+ if templates[temp_key][KEYS]
143
+ csv_list.insert(0, templates[temp_key][KEYS].join(" "))
144
+ end
145
+ end
146
+ end
147
+ date_hash
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ module Jekyll
6
+ module TallyTags
7
+ class Item < Document
8
+ # @param [Hash] item
9
+ # @param [Site] site
10
+ def initialize(item, site)
11
+ @data = item
12
+ @site = site
13
+ @path = "#{site.collections_path}/_posts/#{item[DATE].to_date.to_s}-#{item[ID]}.md"
14
+ @extname = File.extname(@path)
15
+ @collection = site.posts
16
+ @type = @collection.label.to_sym
17
+
18
+ @has_yaml_header = nil
19
+
20
+ if draft?
21
+ categories_from_path("_drafts")
22
+ else
23
+ categories_from_path(collection.relative_directory)
24
+ end
25
+
26
+ data.default_proc = proc do |_, key|
27
+ site.frontmatter_defaults.find(relative_path, type, key)
28
+ end
29
+
30
+ # 加入 link
31
+ permalinks = Utils.get_permalink_config(site.config, PERMALINKS)
32
+ item[PERMALINKS] = Hash.new { |h, k| h[k] = [] }
33
+ permalinks.each do |key, permalink|
34
+ permalink.scan(/:(\w+)/) do |match_data|
35
+ match = match_data[0]
36
+ if item.keys.include?(match)
37
+ item[match].each_with_index do |t, index|
38
+ permalink = item[PERMALINKS][key][index] ? item[PERMALINKS][key][index] : String.new(permalink)
39
+ item[PERMALINKS][key][index] = permalink.gsub(":#{match}", t.is_a?(Array) ? t.join('-') : t)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ # 获取当前配置
45
+ template = Utils.get_templates(site.config)[item[TEMPLATE]]
46
+ template = {}.merge(template)
47
+ template.keep_if do |key, _|
48
+ key != TITLE && key != COUNT
49
+ end
50
+ # 分析内容
51
+ contents = []
52
+ item[KEYS].each_with_index do |key, index|
53
+ contents[index] = {
54
+ KEY => key,
55
+ VALUE => item[VALUES][index],
56
+ }
57
+ template.each do |tag, indexes|
58
+ if indexes.include? index
59
+ contents[index][SYMBOL] = tag
60
+ # FIXME: 这里乱序就会出问题
61
+ contents[index][PERMALINK] = item[PERMALINKS][tag].delete_at(0)
62
+ contents[index][MD_URL] = to_url(contents[index][VALUE], contents[index][PERMALINK])
63
+ end
64
+ end
65
+ end
66
+
67
+ year = item[YEAR][0]
68
+ month = item[MONTH][0]
69
+ day = item[DAY][0]
70
+ week = item[WEEK][0].to_i
71
+ permalinks = item[PERMALINKS]
72
+ # 加入时间
73
+ date_hash = {
74
+ KEY => "日期",
75
+ VALUE => "#{year}/#{month}/#{day}-#{WEEK_VALUE[week]}",
76
+ SYMBOL => DATE,
77
+ MD_URL => "#{to_url(year, permalinks[YEAR][0])}/#{to_url(month, permalinks[MONTH][0])}/#{to_url(day, permalinks[DAY][0])}-#{to_url(WEEK_VALUE[week], permalinks[WEEK][0])}"
78
+ }
79
+ item[CONTENTS] = [] + contents
80
+ contents.insert(0, date_hash)
81
+ item[DATE_TITLE] = date_hash
82
+ item[DATE_CONTENTS] = contents
83
+ end
84
+
85
+ def to_url(text, permalink)
86
+ "[#{text}](#{permalink})"
87
+ end
88
+ end
89
+ end
90
+ end