-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_post.py
218 lines (180 loc) · 5.93 KB
/
new_post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2020-11-06 17:34:32
# @Author : Lewis Tian ([email protected])
# @Link : github.com/taseikyo
# @Version : python3.8
"""
根据模板生成一个 weekly 基本内容
"""
import os
import time
import random
TEMPLATE = """
> @Author : Lewis Tian ([email protected])
>
> @Link : github.com/taseikyo
>
> @Range : {begin_date} - {begin_date}
# Weekly #{current_id}
[readme](../README.md) | [previous]({previous_post}) | [next]({next_post})
![](../images/2024/00/1.jpg "Weekly #{current_id}")
\\**Photo by Lewis Tian on Unsplash*
## Table of Contents
- [algorithm](#algorithm-)
- [review](#review-)
- [tip](#tip-)
- [share](#share-)
## algorithm [🔝](#weekly-{current_id})
## review [🔝](#weekly-{current_id})
## tip [🔝](#weekly-{current_id})
## share [🔝](#weekly-{current_id})
[readme](../README.md) | [previous]({previous_post}) | [next]({next_post})
"""
def new_post():
"""
根据当前已有的 weekly 生成新的
"""
files = os.listdir("weekly")
files.sort()
current_id = len(files) + 1
current_date = time.strftime("%Y-%m-%d", time.localtime())
current_month = time.strftime("%Y%m", time.localtime())
# 202011W1.md
if current_month == files[-1][:6]:
current_name = f"{current_month}W{int(files[-1][-4])+1}.md"
next_name = f"{current_month}W{int(files[-1][-4])+2}.md"
else:
current_name = f"{files[-1][:6]}W{int(files[-1][-4])+1}.md"
next_name = f"{files[-1][:6]}W{int(files[-1][-4])+2}.md"
print(f"{current_name=} {next_name=}")
with open(f"weekly/{current_name}", "w", encoding="utf-8") as f:
f.write(
TEMPLATE.format(
begin_date=current_date,
current_id=current_id,
previous_post=files[-1],
next_post=next_name,
)[1:]
)
def has_read_this_url(url: str = ""):
"""
判断是否已经添加了 @url 对应的博客
"""
assert url.strip() != ""
files = [f for f in os.listdir("weekly") if f.endswith("md")]
for file in files:
with open(f"weekly/{file}", encoding="utf-8") as f:
for idx, line in enumerate(f):
if line.find(url) >= 0:
print(f"{url} has been found: {file}:{idx}!")
return
print(f"{url} has not been read!")
def update_readme():
"""
尝试使用 GitHub Action 自动更新 readme
"""
with open("README.md", encoding="utf-8") as f:
lines = f.readlines()
for i, x in enumerate(lines):
if x.find("Calendar") > 0:
start = i + 2
break
# GitHub Action 会出现乱序的情况,很奇怪
files = os.listdir("weekly")
files.sort()
print(files)
word = random.choice(
[
"OK",
"Wow",
"Nice",
"Amazing",
"Excellent",
"Well done",
"Outstanding",
]
)
lines[start] = f"{word}! {len(files)} posts in total. Keep going!\n"
# 表格每行 5 个,不足 5 个补空
cnt = 0
one_line = []
all_line = []
for file in files:
one_line.append(f" [{file.split('.')[0]}](weekly/{file}) ")
cnt += 1
if cnt == 5:
all_line.append(one_line)
one_line = []
cnt = 0
if 0 < cnt < 5:
one_line += [" " for _ in range(cnt, 6)]
all_line.append(one_line)
table = "\n".join([f"|{'|'.join(x)}|" for x in all_line])
license = "\n\n## License\n\nCopyright (c) 2020 Lewis Tian. Licensed under the MIT license.\n"
# 日历样式+列表样式;前者显示时间,后者显示主题
with open("SUMMARY.md", encoding="utf-8") as f:
lists = f.readlines()[2:]
lists[0] = "\n\n## List\n"
for i, x in enumerate(lists):
if x.startswith("## "):
lists[i] = x.replace("## ", "### ")
elif x.startswith("### "):
lists[i] = x.replace("### ", "#### ")
with open("README.md", "w", encoding="utf-8") as f:
f.write("".join(lines[: start + 4]))
f.write(table)
f.write("".join(lists))
f.write(license)
def update_summary():
"""
由于 gitbook 展示列表乱序,听说是根据 SUMMARY 来的
于是在更新 README 的时候顺便更新
"""
files = os.listdir("weekly")
files.sort(reverse=True)
nums = len(files)
# 按 【年 - 月 - 第 x 期:share 的主题】排列
month_table = {
"01": "一",
"02": "二",
"03": "三",
"04": "四",
"05": "五",
"06": "六",
"07": "七",
"08": "八",
"09": "九",
"10": "十",
"11": "十一",
"12": "十二",
}
current_year = ""
current_month = ""
summary_list = ["# SUMMARY\n", "- [关于本书](README.md)\n"]
for idx, file in enumerate(files):
with open(f"weekly/{file}", encoding="utf-8") as f:
lines = f.readlines()
for x, line in enumerate(lines):
if line.find("## share") == 0:
topic = lines[x + 2]
# 去掉链接,前面的序号,以及中英的标记
topic = topic.split("](")[0].split("1. ")[-1].split("(")[0].strip()
if topic[0] == "[":
topic = topic[1:]
if current_year != file[:4]:
current_year = file[:4]
tmp = f"\n## {current_year}\n"
summary_list.append(tmp)
if current_month != file[4:6]:
current_month = file[4:6]
tmp = f"\n### {month_table[current_month]}月\n"
summary_list.append(tmp)
tmp = f"- [第 {nums-idx} 期:{topic}](weekly/{file})"
summary_list.append(tmp)
summary_list.append("")
with open("SUMMARY.md", "w", encoding="utf-8") as f:
f.write("\n".join(summary_list).replace("\n\n\n", "\n\n"))
if __name__ == "__main__":
update_summary()
update_readme()