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,128 @@
|
|
1
|
+
# # frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# require "jekyll"
|
4
|
+
#
|
5
|
+
# module Jekyll
|
6
|
+
# module TallyTags
|
7
|
+
#
|
8
|
+
# class ItemsToPage < Generator
|
9
|
+
# safe true
|
10
|
+
# # @param [Configuration] config
|
11
|
+
# def initialize(config = nil)
|
12
|
+
# @config = Utils.get_permalink_config(config)
|
13
|
+
# @enabled = Utils.get_permalink_config(config, ENABLED)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # @param [Site] site
|
17
|
+
# def generate(site)
|
18
|
+
# # 判断是否为空
|
19
|
+
# return if @config.nil?
|
20
|
+
# # 开始赋值
|
21
|
+
# @site = site
|
22
|
+
# @posts = site.posts
|
23
|
+
# @pages = []
|
24
|
+
#
|
25
|
+
# read_all(@config[PERMALINKS], @site.posts.docs)
|
26
|
+
# # 把所有的拼接到到 `pages` 里面
|
27
|
+
# @site.pages.concat(@pages)
|
28
|
+
# # 配置里面也放一份
|
29
|
+
# @site.config[PAGES] = @pages
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # @param [Hash{String => String}] permalinks
|
33
|
+
# # @param [Array<Document>] scan_docs
|
34
|
+
# def read_all(permalinks, scan_docs)
|
35
|
+
# permalinks.each do |key, permalink|
|
36
|
+
# # 判断 链接是否包含 :xxx :(\w+)
|
37
|
+
# matches = []
|
38
|
+
# permalink.scan(/:(\w+)/) { |match| matches << match[0] }
|
39
|
+
# read_loop(key, {}, Array.new(scan_docs), matches, 0)
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# # @param [String] permalink_key
|
44
|
+
# # @param [Hash{Symbol => String}] titles
|
45
|
+
# # @param [Array<Document>] docs
|
46
|
+
# # @param [Array<String>] matches
|
47
|
+
# # @param [Integer] index
|
48
|
+
# def read_loop(permalink_key, titles, docs, matches, index)
|
49
|
+
# if index > matches.size - 1
|
50
|
+
# return
|
51
|
+
# end
|
52
|
+
# match = matches[index]
|
53
|
+
# # 找到对应的 docs
|
54
|
+
# if DATE_HASH.keys.include?(match)
|
55
|
+
# docs_hash = date_to_docs_hash(docs, DATE_HASH[match])
|
56
|
+
# read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
|
57
|
+
# else
|
58
|
+
# begin
|
59
|
+
# docs_hash = tags_to_docs_hash(docs, match)
|
60
|
+
# read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
|
61
|
+
# rescue
|
62
|
+
# Jekyll.logger.warn CLASSIFY, "没有该keys ':#{match}'"
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# # @param [Hash{String => Array<Document>}] docs_hash
|
68
|
+
# # @param [Symbol] symbol
|
69
|
+
# # @param [String] permalink_key
|
70
|
+
# # @param [Hash{Symbol => String}] titles
|
71
|
+
# # @param [Array<String>] matches
|
72
|
+
# # @param [Integer] index
|
73
|
+
# def read_any(docs_hash, symbol, permalink_key, titles, matches, index)
|
74
|
+
# docs_hash.each do |key, docs|
|
75
|
+
# new_titles = titles.merge({ symbol => key })
|
76
|
+
# # 开启了该字段 同时 是匹配的最后一项的时候 写入数组
|
77
|
+
# if enabled?(permalink_key) && index == matches.size
|
78
|
+
# clz = SinglePage
|
79
|
+
# case permalink_key
|
80
|
+
# when YEAR
|
81
|
+
# clz = YearPage
|
82
|
+
# when MONTH
|
83
|
+
# clz = MonthPage
|
84
|
+
# when DAY
|
85
|
+
# clz = DayPage
|
86
|
+
# when WEEK
|
87
|
+
# clz = WeekPage
|
88
|
+
# when WEEKS
|
89
|
+
# clz = WeekPage
|
90
|
+
# end
|
91
|
+
# @pages << clz.new(@site, new_titles, permalink_key, docs.keep_if { |doc| doc.data[TEMPLATE] })
|
92
|
+
# end
|
93
|
+
# read_loop(permalink_key, new_titles, docs, matches, index)
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# private
|
98
|
+
#
|
99
|
+
# # @param [String] type
|
100
|
+
# def enabled?(type)
|
101
|
+
# @enabled == true || @enabled == ALL || (@enabled.is_a?(Array) && @enabled.include?(type))
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# # @param [Array<Document>] docs
|
105
|
+
# # @param [String] attr
|
106
|
+
# # @return [Hash{String => Array<Document>}]
|
107
|
+
# def tags_to_docs_hash(docs, attr)
|
108
|
+
# hash = Hash.new { |h, key| h[key] = [] }
|
109
|
+
# docs.each do |doc|
|
110
|
+
# doc.data[attr]&.each { |key| hash[key] << doc }
|
111
|
+
# end
|
112
|
+
# hash.each_value { |docs| docs.sort!.reverse! }
|
113
|
+
# hash
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# # @param [Array<Document>] docs `yaml` 头部信息
|
117
|
+
# # @param [String] id ISO-8601
|
118
|
+
# # @return [Hash{String=>Array<Document>}]
|
119
|
+
# def date_to_docs_hash(docs, id)
|
120
|
+
# hash = Hash.new { |h, k| h[k] = [] }
|
121
|
+
# docs.each { |doc| hash[doc.date.strftime(id)] << doc }
|
122
|
+
# hash.each_value { |docs| docs.sort!.reverse! }
|
123
|
+
# hash
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# end
|
127
|
+
# end
|
128
|
+
# end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "jekyll"
|
4
|
+
|
5
|
+
module Jekyll
|
6
|
+
module TallyTags
|
7
|
+
class Methods
|
8
|
+
|
9
|
+
# @param [Integer] a
|
10
|
+
# @param [Integer] b
|
11
|
+
def self.sum(a, b)
|
12
|
+
self.to_f_s(a.to_f + b.to_f)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param [Integer] a
|
16
|
+
# @param [Integer] b
|
17
|
+
def self.div(a, b)
|
18
|
+
self.to_f_s(a.to_f / b.to_f)
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [Integer] a
|
22
|
+
# @param [Integer] b
|
23
|
+
def self.mul(a, b)
|
24
|
+
self.to_f_s(a.to_f * b.to_f)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param [Integer] a
|
28
|
+
# @param [Integer] b
|
29
|
+
def self.sub(a, b)
|
30
|
+
self.to_f_s(a.to_f - b.to_f)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param [Float] number
|
34
|
+
def self.to_f_s(number)
|
35
|
+
if number - number.round != 0
|
36
|
+
number.round(2).to_s
|
37
|
+
else
|
38
|
+
number.round.to_s
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,284 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'jekyll'
|
4
|
+
|
5
|
+
module Jekyll
|
6
|
+
module TallyTags
|
7
|
+
class BasePage < Jekyll::Page
|
8
|
+
attr_accessor :docs, :type, :slug
|
9
|
+
|
10
|
+
ATTRIBUTES_FOR_LIQUID = %w(
|
11
|
+
docs
|
12
|
+
type
|
13
|
+
title
|
14
|
+
date
|
15
|
+
name
|
16
|
+
path
|
17
|
+
url
|
18
|
+
permalink
|
19
|
+
sum
|
20
|
+
content
|
21
|
+
).freeze
|
22
|
+
# 初始化
|
23
|
+
# @param [Site] site
|
24
|
+
# @param [Hash{Symbol => String}] titles
|
25
|
+
# @param [String] type
|
26
|
+
# @param [Array<Document>] docs
|
27
|
+
def initialize(site, titles, type, docs)
|
28
|
+
@site = site
|
29
|
+
@docs = docs
|
30
|
+
@type = type
|
31
|
+
@title = modify_title_hash(titles)
|
32
|
+
|
33
|
+
@config = Utils.get_permalink_config(site.config)
|
34
|
+
@slug = slugify_string_title
|
35
|
+
|
36
|
+
@ext = File.extname(relative_path)
|
37
|
+
@path = relative_path
|
38
|
+
@name = File.basename(relative_path, @ext)
|
39
|
+
|
40
|
+
@data = {
|
41
|
+
LAYOUT => layout,
|
42
|
+
PERMALINK => @config[PERMALINKS][type]
|
43
|
+
}
|
44
|
+
|
45
|
+
# avg
|
46
|
+
|
47
|
+
@content = get_content
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_content
|
51
|
+
docs_to_yml(get_docs)
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [Array<Float>]
|
55
|
+
def sum
|
56
|
+
sum = []
|
57
|
+
get_docs.each do |doc|
|
58
|
+
if doc.data[COUNT]
|
59
|
+
doc.data[COUNT].each_with_index do |count, index|
|
60
|
+
sum[index] = 0 if index == 0
|
61
|
+
sum[index] += count.to_f
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
sum
|
66
|
+
end
|
67
|
+
|
68
|
+
def avg
|
69
|
+
avg = []
|
70
|
+
dates = @docs.collect { |t| t.date.to_date }.uniq
|
71
|
+
sum.each_index do |index|
|
72
|
+
avg[index] = Methods.to_f_s(sum[index] / dates.size)
|
73
|
+
end
|
74
|
+
avg
|
75
|
+
end
|
76
|
+
|
77
|
+
# @return [String]
|
78
|
+
def template
|
79
|
+
@config.dig(PERMALINKS, type)
|
80
|
+
end
|
81
|
+
|
82
|
+
# @return [String]
|
83
|
+
def layout
|
84
|
+
@config.dig(LAYOUTS, type) || @config[LAYOUT]
|
85
|
+
end
|
86
|
+
|
87
|
+
# @return [Hash{ String => String}] eg: {:categories => "ruby", :title => "something"}
|
88
|
+
def url_placeholders
|
89
|
+
if @title.is_a? Hash
|
90
|
+
@title.merge(:type => @type)
|
91
|
+
else
|
92
|
+
{ :name => @slug, :type => @type }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# @return [String]
|
97
|
+
def url
|
98
|
+
u = @url ||= URL.new(
|
99
|
+
:template => template,
|
100
|
+
:placeholders => url_placeholders,
|
101
|
+
:permalink => nil
|
102
|
+
).to_s
|
103
|
+
rescue ArgumentError
|
104
|
+
raise ArgumentError, "提供的模板 \"#{template}\" 无效."
|
105
|
+
end
|
106
|
+
|
107
|
+
def filters
|
108
|
+
@filters
|
109
|
+
end
|
110
|
+
|
111
|
+
# @return [String]
|
112
|
+
def permalink
|
113
|
+
data.is_a?(Hash) && data[PERMALINK]
|
114
|
+
end
|
115
|
+
|
116
|
+
# @return [String]
|
117
|
+
def title
|
118
|
+
@title if @title.is_a?(String)
|
119
|
+
end
|
120
|
+
|
121
|
+
# @param [Hash<Symbol => String, Array>]
|
122
|
+
def modify_title_hash(titles)
|
123
|
+
temp = {}
|
124
|
+
titles.each do |k, v|
|
125
|
+
if v.is_a?(Array)
|
126
|
+
temp[k] = v.join(",")
|
127
|
+
else
|
128
|
+
temp[k] = v
|
129
|
+
end
|
130
|
+
end
|
131
|
+
temp
|
132
|
+
end
|
133
|
+
|
134
|
+
# @return [Date]
|
135
|
+
def date
|
136
|
+
# if @title.is_a?(Hash)
|
137
|
+
# args = @title.values.map(&:to_i)
|
138
|
+
# Date.new(*args)
|
139
|
+
# end
|
140
|
+
# TODO: 修复日期
|
141
|
+
"2011/01/11"
|
142
|
+
end
|
143
|
+
|
144
|
+
# @return [String]
|
145
|
+
def relative_path
|
146
|
+
@relative_path ||=
|
147
|
+
begin
|
148
|
+
path = URL.unescape_path(url).gsub(%r!^/!, "")
|
149
|
+
path = File.join(path, "index.md") if url.end_with?("/")
|
150
|
+
path
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# @return [String]
|
155
|
+
def inspect
|
156
|
+
"#<TagsLib:Subject @type=#{@type} @title=#{@title} @data=#{@data.inspect}>"
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
# @return [Array<Document>]
|
162
|
+
def get_docs
|
163
|
+
@docs
|
164
|
+
end
|
165
|
+
|
166
|
+
def slugify_string_title
|
167
|
+
return unless title.is_a?(String)
|
168
|
+
Jekyll::Utils.slugify(title, :mode => @config[SLUG_MODE])
|
169
|
+
end
|
170
|
+
|
171
|
+
# @param [Array<Document>] docs
|
172
|
+
def docs_to_yml(docs)
|
173
|
+
|
174
|
+
yaml_mode = false
|
175
|
+
md_table_mode = false
|
176
|
+
|
177
|
+
# 获取里面所有的 `template keys`
|
178
|
+
template_keys = docs.collect() { |doc| doc.data[TEMPLATE] }.uniq
|
179
|
+
align = yaml_mode ? "" : ":----:"
|
180
|
+
sep = yaml_mode ? " " : " | "
|
181
|
+
# 初始化
|
182
|
+
yaml_templates_hash = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = { VALUES => [], MD_URL => [] } } }
|
183
|
+
template_keys.each do |template_key|
|
184
|
+
# 找到当前遍历的 符合 `key` 的所有 `docs`
|
185
|
+
template_docs = docs.find_all() { |doc| doc.data[TEMPLATE] == template_key }
|
186
|
+
template_docs.each do |doc|
|
187
|
+
keys = doc.data[DATE_CONTENTS].map { |hash| hash[KEY] }
|
188
|
+
yaml_templates_hash[template_key][keys][VALUES] << doc.data[DATE_CONTENTS].map { |hash| hash[VALUE] }
|
189
|
+
yaml_templates_hash[template_key][keys][MD_URL] << doc.data[DATE_CONTENTS].map { |hash| hash[MD_URL] ? hash[MD_URL] : hash[VALUE] }
|
190
|
+
end
|
191
|
+
# 计算 对齐空格数
|
192
|
+
yaml_templates_hash[template_key].each do |keys, hash|
|
193
|
+
values_list = hash[VALUES]
|
194
|
+
lengths = []
|
195
|
+
keys.each_index do |index|
|
196
|
+
k_length = get_length(keys[index])
|
197
|
+
v_max = values_list.collect() { |values| values[index] }.max_by() { |value| get_length(value) }
|
198
|
+
lengths[index] = [k_length, get_length(v_max), get_length(align)].max()
|
199
|
+
end
|
200
|
+
yaml_templates_hash[template_key][keys][MAX_LENGTH] = lengths
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
content = ""
|
205
|
+
if yaml_mode
|
206
|
+
content += "```yml\n"
|
207
|
+
yaml_templates_hash.each_key do |template_key|
|
208
|
+
content += "#{template_key}:\n"
|
209
|
+
templates_hash = yaml_templates_hash[template_key]
|
210
|
+
templates_hash.each_key do |keys|
|
211
|
+
hash = templates_hash[keys]
|
212
|
+
lengths = hash[MAX_LENGTH]
|
213
|
+
content += " - #{center_values(keys, lengths, sep)}\n"
|
214
|
+
hash[VALUES].each_index do |index|
|
215
|
+
values = hash[VALUES][index]
|
216
|
+
content += " - #{center_values(values, lengths, sep)}\n"
|
217
|
+
end
|
218
|
+
content += "\n"
|
219
|
+
end
|
220
|
+
content += "\n"
|
221
|
+
end
|
222
|
+
content += "```"
|
223
|
+
else
|
224
|
+
content += "```markdown\n" if md_table_mode
|
225
|
+
yaml_templates_hash.each_key do |template_key|
|
226
|
+
templates_hash = yaml_templates_hash[template_key]
|
227
|
+
templates_hash.each_key do |keys|
|
228
|
+
hash = templates_hash[keys]
|
229
|
+
lengths = hash[MAX_LENGTH]
|
230
|
+
content += "#{sep}#{center_values(keys, lengths, sep)}#{sep}\n"
|
231
|
+
content += "#{sep}#{center_values(keys.map { align }, lengths, sep)}#{sep}\n"
|
232
|
+
hash[VALUES].each_index do |index|
|
233
|
+
values = md_table_mode ? hash[VALUES][index] : hash[MD_URL][index]
|
234
|
+
content += "#{sep}#{center_values(values, lengths, sep)}#{sep}\n"
|
235
|
+
end
|
236
|
+
content += "\n"
|
237
|
+
end
|
238
|
+
content += "\n"
|
239
|
+
end
|
240
|
+
content += "```" if md_table_mode
|
241
|
+
end
|
242
|
+
content
|
243
|
+
end
|
244
|
+
|
245
|
+
# @param [Array<String>] values
|
246
|
+
# @param [Array<Int>] lengths
|
247
|
+
# @param [String] sep
|
248
|
+
def center_values(values, lengths, sep)
|
249
|
+
temp = Array.new(values)
|
250
|
+
temp.each_index do |index|
|
251
|
+
# ljust center rjust
|
252
|
+
if temp[index]
|
253
|
+
temp[index] = temp[index].encode('gbk').b.center(lengths[index]).force_encoding("gbk").encode("utf-8")
|
254
|
+
else
|
255
|
+
temp[index] = "".center(lengths[index])
|
256
|
+
end
|
257
|
+
end
|
258
|
+
temp.join(sep)
|
259
|
+
end
|
260
|
+
|
261
|
+
# @param [Array<String>] values
|
262
|
+
# @param [Array<Int>] lengths
|
263
|
+
# @param [String] sep
|
264
|
+
# @param [Array<String>] permalinks
|
265
|
+
def center_values_url(values, lengths, sep, permalinks)
|
266
|
+
temp = Array.new(values)
|
267
|
+
temp.each_index do |index|
|
268
|
+
# ljust center rjust
|
269
|
+
temp[index] = temp[index].encode('gbk').b.center(lengths[index]).force_encoding("gbk").encode("utf-8")
|
270
|
+
end
|
271
|
+
temp.each_with_index do |temp, index|
|
272
|
+
url = "[#{values[index]}](#{permalinks[index]})"
|
273
|
+
temp[index] = temp.gsub(values[index], url)
|
274
|
+
end
|
275
|
+
temp.join(sep)
|
276
|
+
end
|
277
|
+
|
278
|
+
# @param [String] value
|
279
|
+
def get_length(value)
|
280
|
+
value ? value.encode('gbk').b.length : 0
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|