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.
- checksums.yaml +4 -4
- data/lib/jekyll-tally-tags.rb +205 -0
- data/lib/jekyll-tally-tags/docs_to_items.rb +151 -0
- data/lib/jekyll-tally-tags/item.rb +90 -0
- data/lib/jekyll-tally-tags/items_to_page.rb +128 -0
- data/lib/jekyll-tally-tags/methods.rb +44 -0
- data/lib/jekyll-tally-tags/pages/base_page.rb +284 -0
- data/lib/jekyll-tally-tags/pages/day_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/month_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/multiple_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/single_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/week_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/weeks_page.rb +14 -0
- data/lib/jekyll-tally-tags/pages/year_page.rb +70 -0
- data/lib/jekyll-tally-tags/{counter.rb → utils.rb} +173 -152
- data/lib/jekyll-tally-tags/version.rb +72 -54
- metadata +16 -25
- data/.gitignore +0 -8
- data/.idea/.gitignore +0 -8
- data/.idea/inspectionProfiles/Project_Default.xml +0 -6
- data/.idea/jekyll-tally-tags.iml +0 -44
- data/.idea/misc.xml +0 -4
- data/.idea/modules.xml +0 -8
- data/.idea/vcs.xml +0 -6
- data/.rubocop.yml +0 -13
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -84
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -74
- data/LICENSE.txt +0 -21
- data/README.md +0 -43
- data/Rakefile +0 -5
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/jekyll-tally-tags.gemspec +0 -28
- data/lib/jekyll-tally-tags/classify.rb +0 -196
- data/lib/jekyll-tally-tags/hooks_doc.rb +0 -134
- data/lib/jekyll-tally-tags/hooks_logs.rb +0 -57
- data/lib/jekyll-tally-tags/subject.rb +0 -143
@@ -0,0 +1,70 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module TallyTags
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class YearPage < TallyTags::BasePage
|
7
|
+
|
8
|
+
def get_content
|
9
|
+
docs_to_content(get_docs)
|
10
|
+
end
|
11
|
+
|
12
|
+
# @param [Array<Document>] docs
|
13
|
+
def docs_to_content(docs)
|
14
|
+
yaml_mode = true
|
15
|
+
md_table_mode = true
|
16
|
+
dates = docs.collect { |doc| doc.data[SLUG] }.uniq
|
17
|
+
align = yaml_mode ? "" : ":----:"
|
18
|
+
sep = yaml_mode ? " " : " | "
|
19
|
+
yml_temp_hash = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = { VALUES => [], MD_URL => [] } } }
|
20
|
+
dates.each do |date|
|
21
|
+
# 找到当前遍历的 符合 `date` 的所有 `docs`
|
22
|
+
docs.find_all { |doc| doc.data[SLUG] == date }.each do |doc|
|
23
|
+
template_key = doc.data[TEMPLATE]
|
24
|
+
keys = doc.data[CONTENTS].map { |hash| hash[KEY] }
|
25
|
+
values = doc.data[CONTENTS].map { |hash| hash[VALUE] }
|
26
|
+
urls = doc.data[CONTENTS].map { |hash| hash[MD_URL] ? hash[MD_URL] : hash[VALUE] }
|
27
|
+
yml_temp_hash[date][{ template_key => keys }][VALUES] << values
|
28
|
+
yml_temp_hash[date][{ template_key => keys }][MD_URL] << urls
|
29
|
+
end
|
30
|
+
# 计算 对齐空格数
|
31
|
+
yml_temp_hash[date].each do |temp_keys, hash|
|
32
|
+
values_list = hash[VALUES]
|
33
|
+
lengths = []
|
34
|
+
keys = temp_keys.values[0]
|
35
|
+
keys.each_index do |index|
|
36
|
+
# k_length = get_length(keys[index])
|
37
|
+
v_max = values_list.collect() { |values| values[index] }.max_by() { |value| get_length(value) }
|
38
|
+
lengths[index] = [get_length(v_max), get_length(align)].max()
|
39
|
+
end
|
40
|
+
yml_temp_hash[date][temp_keys][MAX_LENGTH] = lengths
|
41
|
+
end
|
42
|
+
end
|
43
|
+
content = ""
|
44
|
+
if yaml_mode
|
45
|
+
content += "```yml\n"
|
46
|
+
yml_temp_hash.keys.reverse_each do |date|
|
47
|
+
content += "#{date}:\n"
|
48
|
+
templates_hash = yml_temp_hash[date]
|
49
|
+
templates_hash.each_key do |temp_keys|
|
50
|
+
temp = temp_keys.keys[0]
|
51
|
+
content += " #{temp}:\n"
|
52
|
+
keys = temp_keys.values[0]
|
53
|
+
hash = templates_hash[temp_keys]
|
54
|
+
lengths = hash[MAX_LENGTH]
|
55
|
+
# content += " - #{center_values(keys, lengths, sep)}\n"
|
56
|
+
hash[VALUES].each_index do |index|
|
57
|
+
values = hash[VALUES][index]
|
58
|
+
content += " - #{center_values(values, lengths, sep)}\n"
|
59
|
+
end
|
60
|
+
content += "\n"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
content += "```"
|
64
|
+
end
|
65
|
+
content
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -1,43 +1,15 @@
|
|
1
|
-
|
2
|
-
module TallyTags
|
3
|
-
class Counter
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
# @param [Document] doc
|
7
|
-
# @param [String] key
|
8
|
-
# @param [Object] value
|
9
|
-
def self.set_doc_data(doc, key, value)
|
10
|
-
doc.data[key] = value
|
11
|
-
doc.data[DATA][key] = value
|
12
|
-
end
|
13
|
-
|
14
|
-
# 两处添加
|
15
|
-
# @param [Document] doc
|
16
|
-
# @param [String] key
|
17
|
-
# @param [Object] value
|
18
|
-
def self.add_doc_data(doc, key, value)
|
19
|
-
doc.data[key] = [] if !doc.data[key]
|
20
|
-
doc.data[DATA][key] = [] if !doc.data[DATA][key]
|
21
|
-
doc.data[key] << value
|
22
|
-
doc.data[DATA][key] << value
|
23
|
-
end
|
24
|
-
|
25
|
-
# @param [Float] number
|
26
|
-
# @param [String] format
|
27
|
-
def self.to_f_s(number, format)
|
28
|
-
if number - number.round != 0
|
29
|
-
format % number.round(2).to_s
|
30
|
-
else
|
31
|
-
format % number.round.to_s
|
32
|
-
end
|
33
|
-
end
|
3
|
+
require "jekyll"
|
34
4
|
|
5
|
+
module Jekyll
|
6
|
+
module TallyTags
|
7
|
+
class Utils
|
8
|
+
# 把配置变成数组配置
|
35
9
|
# @param [Array, Object]
|
36
10
|
# @return [Array]
|
37
11
|
def self.to_array(single_or_array, default)
|
38
|
-
|
39
|
-
return default
|
40
|
-
end
|
12
|
+
return default unless single_or_array
|
41
13
|
if single_or_array.is_a?(Array)
|
42
14
|
single_or_array
|
43
15
|
else
|
@@ -45,16 +17,128 @@ module Jekyll
|
|
45
17
|
end
|
46
18
|
end
|
47
19
|
|
48
|
-
# @param [
|
49
|
-
# @param [
|
20
|
+
# @param [Hash<Object, Hash<Array>>] hash
|
21
|
+
# @param [Object] key
|
22
|
+
# @param [Hash<Array>] inner_hash
|
23
|
+
def self.inner_merge(hash, key, inner_hash)
|
24
|
+
if hash.include?(key)
|
25
|
+
hash[key].merge!(inner_hash) { |_, o, n| o + n }
|
26
|
+
else
|
27
|
+
hash[key] = inner_hash
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param [Array<String>] values
|
32
|
+
# @param [Array<String>] formatters
|
33
|
+
def self.format_values(values, formatters)
|
34
|
+
results = []
|
35
|
+
formatters.each do |formatter|
|
36
|
+
# 如果是数字的话
|
37
|
+
if formatter.is_a?(Integer)
|
38
|
+
results << values[formatter]
|
39
|
+
next
|
40
|
+
end
|
41
|
+
|
42
|
+
# @type [Hash<Integer => Integer>]
|
43
|
+
params = {} #下标 数组
|
44
|
+
# @type [Hash<Integer => String>]
|
45
|
+
methods = {} #方法 数组
|
46
|
+
|
47
|
+
splits = self.partition_all(formatter)
|
48
|
+
split_hash = {}
|
49
|
+
splits.each_index do |index|
|
50
|
+
split = splits[index]
|
51
|
+
# 保存到 `hash` 内
|
52
|
+
split_hash[index] = split
|
53
|
+
if split.match?(/^:[a-zA-Z0-9_]+/)
|
54
|
+
# 判断是不是满足初始条件
|
55
|
+
if split.match?(/^:[0-9]+/)
|
56
|
+
value_index = split.match(/[0-9]+/)[0].to_i
|
57
|
+
params[index] = values[value_index]
|
58
|
+
else
|
59
|
+
methods[index] = split
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
result = ""
|
65
|
+
if methods.empty?
|
66
|
+
splits.each_index do |index|
|
67
|
+
if params.has_key?(index)
|
68
|
+
result += params[index]
|
69
|
+
else
|
70
|
+
result += splits[index]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
else
|
74
|
+
methods.keys.sort.reverse_each do |index|
|
75
|
+
param_count = methods[index].match(/[0-9]/)[0].to_i
|
76
|
+
method_match = methods[index].match(/[a-zA-Z]+/).to_s
|
77
|
+
method = Methods.method(method_match)
|
78
|
+
args = []
|
79
|
+
(1..param_count).each do |i|
|
80
|
+
if params.include?(index + i)
|
81
|
+
args[i - 1] = params.delete(index + i) # 在对应位置上删掉该参数
|
82
|
+
end
|
83
|
+
if methods.include?(index + i)
|
84
|
+
args[i - 1] = methods.delete(index + i) # 在对应位置上删掉该参数
|
85
|
+
end
|
86
|
+
split_hash.delete(index + i)
|
87
|
+
end
|
88
|
+
methods[index] = method.call(*args)
|
89
|
+
end
|
90
|
+
split_hash.keys.sort.each do |index|
|
91
|
+
if methods.has_key?(index)
|
92
|
+
result += methods[index].to_s
|
93
|
+
else
|
94
|
+
if params.has_key?(index)
|
95
|
+
result += params[index]
|
96
|
+
else
|
97
|
+
result += split_hash[index]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
results << result
|
103
|
+
end
|
104
|
+
results
|
105
|
+
end
|
106
|
+
|
107
|
+
# @example
|
108
|
+
# ":sub_2和:2:3" => [":sub_2", "和", ":2", ":3"]
|
109
|
+
# @param [String]
|
110
|
+
# @return [Array<String>]
|
111
|
+
def self.partition_all(formatter)
|
112
|
+
result = []
|
113
|
+
temps = formatter.partition(/:\w+/)
|
114
|
+
(0..temps.size - 1).each do |index|
|
115
|
+
temp = temps[index]
|
116
|
+
if index == temps.size - 1
|
117
|
+
unless temp.empty?
|
118
|
+
result += self.partition_all(temp)
|
119
|
+
end
|
120
|
+
else
|
121
|
+
if temp && !temp.empty?
|
122
|
+
result << temp
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
result
|
127
|
+
end
|
128
|
+
|
129
|
+
# @param [Array<Array<String>>] combines
|
130
|
+
# @param [Array<Hash>] items
|
50
131
|
# @param [String] find
|
51
132
|
# @param [String] to
|
52
|
-
def self.
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
133
|
+
def self.link_combines(combines, items, find, to)
|
134
|
+
combines.each do |combine|
|
135
|
+
items.each do |item|
|
136
|
+
if item[find] & combine
|
137
|
+
unless item[to]
|
138
|
+
item[to] = []
|
139
|
+
end
|
140
|
+
unless item[to].include?(combine)
|
141
|
+
item[to] << combine
|
58
142
|
end
|
59
143
|
end
|
60
144
|
end
|
@@ -64,11 +148,11 @@ module Jekyll
|
|
64
148
|
# @param [Array<String>] items 原始数据
|
65
149
|
# @param [Integer, Array<Integer>] deep_key 合并类型
|
66
150
|
# @return [Array<Array<String>>]
|
67
|
-
def self.
|
151
|
+
def self.combine_tags(items, deep_key)
|
68
152
|
hash = {}
|
69
153
|
is_deep = false
|
70
154
|
if items.size > 2
|
71
|
-
deep_key = ALL
|
155
|
+
deep_key = ALL unless deep_key
|
72
156
|
# all 情况
|
73
157
|
if deep_key.is_a?(String)
|
74
158
|
if deep_key == ALL
|
@@ -120,7 +204,7 @@ module Jekyll
|
|
120
204
|
# @param [Integer] e 结束下标
|
121
205
|
# @return [Array<Array<String>>]
|
122
206
|
def self.merge(items, deep_items, into_items, deep, cur_deep, s, e)
|
123
|
-
|
207
|
+
unless deep_items
|
124
208
|
deep_items = []
|
125
209
|
into_items = []
|
126
210
|
cur_deep = 0
|
@@ -138,122 +222,59 @@ module Jekyll
|
|
138
222
|
deep_items
|
139
223
|
end
|
140
224
|
|
141
|
-
# @param [
|
142
|
-
# @param [
|
143
|
-
def self.
|
144
|
-
|
225
|
+
# @param [Configuration] site_config
|
226
|
+
# @param [String] field
|
227
|
+
def self.get_tally_config(site_config, field = nil)
|
228
|
+
config = site_config[TALLY]
|
229
|
+
if field
|
230
|
+
config ? config[field] : nil
|
231
|
+
else
|
232
|
+
config
|
233
|
+
end
|
145
234
|
end
|
146
235
|
|
147
|
-
#
|
148
|
-
# @param [
|
149
|
-
|
150
|
-
|
151
|
-
|
236
|
+
# 获取 `Tally` 里模板配置
|
237
|
+
# @param [Site] site
|
238
|
+
# @return [Hash{String => Hash{String => Hash}}]
|
239
|
+
def self.get_templates(site_config)
|
240
|
+
configs = site_config[TALLY]
|
241
|
+
return nil unless configs && !configs.empty?
|
242
|
+
# 然后获取默认配置 没有也是可以的
|
243
|
+
# @type [Hash<String => Array<Integer, String>>]
|
244
|
+
default_template = configs[DEFAULT]
|
152
245
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
end
|
246
|
+
# 初始化默认的模板
|
247
|
+
if default_template
|
248
|
+
default_template.collect { |t| Utils.to_array(t, nil) }
|
249
|
+
end
|
158
250
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
251
|
+
# 先判断有没有对应的配置
|
252
|
+
# @type [Hash<String => Array<Integer, String>>]
|
253
|
+
templates = configs[TEMPLATES]
|
254
|
+
# 必须有模板才可以解析
|
255
|
+
return {
|
256
|
+
DEFAULT => default_template
|
257
|
+
} unless templates && !templates.empty?
|
164
258
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
result = []
|
171
|
-
temps = formatter.partition(/:\w+/)
|
172
|
-
(0..temps.size - 1).each do |index|
|
173
|
-
temp = temps[index]
|
174
|
-
if index == temps.size - 1
|
175
|
-
if !temp.empty?
|
176
|
-
result += self.partition_all(temp)
|
177
|
-
end
|
178
|
-
else
|
179
|
-
if temp && !temp.empty?
|
180
|
-
result << temp
|
181
|
-
end
|
259
|
+
# 与默认模板合并
|
260
|
+
templates.each_key do |template_key|
|
261
|
+
template = templates[template_key].merge!(default_template)
|
262
|
+
template.each_key do |key|
|
263
|
+
template[key] = Utils.to_array(template[key], default_template[key])
|
182
264
|
end
|
183
265
|
end
|
184
|
-
|
266
|
+
templates
|
185
267
|
end
|
186
268
|
|
187
|
-
# @param [
|
188
|
-
# @param [
|
189
|
-
def self.
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
next
|
196
|
-
end
|
197
|
-
|
198
|
-
# @type [Hash<Integer => Integer>]
|
199
|
-
params = {} #下标 数组
|
200
|
-
# @type [Hash<Integer => String>]
|
201
|
-
methods = {} #方法 数组
|
202
|
-
|
203
|
-
splits = self.partition_all(formatter)
|
204
|
-
split_hash = {}
|
205
|
-
splits.each_index do |index|
|
206
|
-
split = splits[index]
|
207
|
-
# 保存到 `hash` 内
|
208
|
-
split_hash[index] = split
|
209
|
-
if split.match?(/^:[a-zA-Z0-9_]+/)
|
210
|
-
# 判断是不是满足初始条件
|
211
|
-
if split.match?(/^:[0-9]+/)
|
212
|
-
value_index = split.match(/[0-9]+/)[0].to_i
|
213
|
-
params[index] = values[value_index]
|
214
|
-
else
|
215
|
-
methods[index] = split
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
result = ""
|
221
|
-
if methods.empty?
|
222
|
-
splits.each_index do |index|
|
223
|
-
if params.has_key?(index)
|
224
|
-
result += params[index]
|
225
|
-
else
|
226
|
-
result += splits[index]
|
227
|
-
end
|
228
|
-
end
|
229
|
-
else
|
230
|
-
methods.keys.sort.reverse_each do |index|
|
231
|
-
param_count = methods[index].match(/[0-9]/)[0].to_i
|
232
|
-
method_match = methods[index].match(/[a-zA-Z]+/).to_s
|
233
|
-
method = self.method(method_match)
|
234
|
-
args = []
|
235
|
-
(1..param_count).each do |i|
|
236
|
-
if params.include?(index + i)
|
237
|
-
args[i - 1] = params.delete(index + i) # 在对应位置上删掉该参数
|
238
|
-
end
|
239
|
-
if methods.include?(index + i)
|
240
|
-
args[i - 1] = methods.delete(index + i) # 在对应位置上删掉该参数
|
241
|
-
end
|
242
|
-
split_hash.delete(index + i)
|
243
|
-
end
|
244
|
-
methods[index] = method.call(*args)
|
245
|
-
end
|
246
|
-
split_hash.keys.sort.each do |index|
|
247
|
-
if methods.has_key?(index)
|
248
|
-
result += methods[index].to_s
|
249
|
-
else
|
250
|
-
result += split_hash[index]
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
results << result
|
269
|
+
# @param [Configuration] site_config
|
270
|
+
# @param [String] field
|
271
|
+
def self.get_permalink_config(site_config, field = nil)
|
272
|
+
config = site_config[CLASSIFY]
|
273
|
+
if field
|
274
|
+
config ? config[field] : nil
|
275
|
+
else
|
276
|
+
config
|
255
277
|
end
|
256
|
-
results
|
257
278
|
end
|
258
279
|
end
|
259
280
|
end
|