Skip to content

Commit e60f652

Browse files
committed
feat: auto homepage, and code format
1 parent d19d3c6 commit e60f652

File tree

7 files changed

+314
-53
lines changed

7 files changed

+314
-53
lines changed

.github/workflows/auto-homepage.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: auto regenerate homepage
2+
3+
on: push
4+
5+
jobs:
6+
regenerate:
7+
runs-on: ubuntu-latest
8+
9+
permissions:
10+
# Give the default GITHUB_TOKEN write permission to commit and push the
11+
# added or changed files to the repository.
12+
contents: write
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
# Other steps that change files in the repository
18+
- name: Execute Python script
19+
run: |
20+
python homepage.py
21+
22+
# Commit all changed files back to the repository
23+
- uses: stefanzweifel/git-auto-commit-action@v5
24+

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
temp
44
Makefile
55
.output
6+
__pycache__

homepage.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import os
2+
import re
3+
4+
from utils import find_line_info_with_index, find_lines_index_with_string, get_dir_year_month
5+
6+
project_path = os.path.realpath(os.path.dirname(__file__))
7+
print("project_path:", project_path)
8+
9+
filename = f'{project_path}/index.md'
10+
11+
12+
13+
count_last_files = 5
14+
15+
16+
17+
18+
def get_last(sorted_data):
19+
index = 0
20+
res = []
21+
for year, months in sorted_data.items():
22+
for month in months:
23+
path = f'{year}/{month}'
24+
days = os.listdir(path)
25+
days = sorted(days, reverse=True)
26+
for day in days:
27+
res.append(f'{path}/{day}')
28+
index += 1
29+
if index >= count_last_files:
30+
return res
31+
32+
return res
33+
34+
35+
def modify_with_html(last_info) -> str:
36+
res = '\n\n'
37+
for v in last_info:
38+
# -3: .md
39+
title = v[:-3]
40+
file_path = f'{project_path}/{v}'
41+
details = get_recent_details(file_path)
42+
43+
one = '<p>\n'
44+
title
45+
one += f'<span class="date">{title}</span><br />\n'
46+
one += f'<span class="details">{details}</span>\n'
47+
one += '</p>\n'
48+
res += one
49+
50+
res += '\n\n'
51+
return res
52+
53+
54+
55+
def get_recent_details(file_path):
56+
with open(file_path, "r+", encoding='utf-8') as file_handle:
57+
all_line = file_handle.readlines()
58+
res = ''
59+
for index, value in enumerate(all_line):
60+
if value.startswith('#') or value == '\n':
61+
continue
62+
res += value
63+
res = res.replace('\n', ' ')
64+
# 将多个空格替换为一个
65+
res = re.sub(r'\s+', ' ', res)
66+
if len(res) > 30:
67+
break
68+
69+
res = res[:30]
70+
res += '...'
71+
72+
return res
73+
74+
75+
76+
def update_recent(file_path, rm_begin_line, rm_end_line, recent_info):
77+
temp_filename = 'temptemp'
78+
# 打开原始文件以读取模式和一个临时文件以写入模式
79+
with open(file_path, 'r', encoding='utf-8') as original_file, open(temp_filename, 'w', encoding='utf-8') as temp_file:
80+
ori_lines = original_file.readlines()
81+
temp_file.write(''.join(ori_lines[:rm_begin_line]))
82+
temp_file.write(''.join(recent_info))
83+
temp_file.write(''.join(ori_lines[rm_end_line:]))
84+
85+
# 替换原文件为临时文件的内容
86+
os.replace(temp_filename, file_path)
87+
88+
89+
if __name__ == '__main__':
90+
get_ym_info = get_dir_year_month(project_path)
91+
print(get_ym_info)
92+
# sorted_data = dict(sorted(aa.items(), reverse=True))
93+
sorted_data = {
94+
year: sorted(months, reverse=True)
95+
for year, months in sorted(get_ym_info.items(), reverse=True)
96+
}
97+
print("sorted_data", sorted_data)
98+
last_info = get_last(sorted_data)
99+
print("last_info", last_info)
100+
101+
# 找到要删除的部分
102+
find_last_index = find_lines_index_with_string(filename, '最近更新')
103+
print("find_last_index", find_last_index)
104+
find_info = find_line_info_with_index(filename, find_last_index[0])
105+
print("find_info", find_info)
106+
count_hashes = find_info.count('#')
107+
find_index_same_title_lv = find_lines_index_with_string(filename, '#'*count_hashes)
108+
print("find_index_same_title_lv",find_index_same_title_lv)
109+
index_in_same_title = find_index_same_title_lv.index(find_last_index[0])
110+
next_index = find_index_same_title_lv[index_in_same_title + 1]
111+
print("next_index", next_index)
112+
rm_begin_line, rm_end_line = find_last_index[0], next_index-1
113+
114+
115+
116+
update_recent(filename, rm_begin_line, rm_end_line, modify_with_html(last_info))
117+

index.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
# diary
1+
# Diary
22

33
to keep a diary, to make life fun.
44

55
如果当日没有总结,次日没有期待,那……
66

7-
all diary
7+
## 最近更新
8+
9+
10+
<p>
11+
<span class="date">2024/02/06</span><br />
12+
<span class="details">周二 多云转阴 昨天是听错了吗?楼下早餐店竟然已经关门回家过...</span>
13+
</p>
14+
<p>
15+
<span class="date">2024/02/02</span><br />
16+
<span class="details">周五 完了要被裁了。被老板说太菜了。 虽然口头上让我过个好年...</span>
17+
</p>
18+
<p>
19+
<span class="date">2024/02/01</span><br />
20+
<span class="details">早起窜稀,浪费了许久时间, 赶公交,到地铁站,正好地铁到了就...</span>
21+
</p>
22+
<p>
23+
<span class="date">2024/01/31</span><br />
24+
<span class="details">周三 小雨 老天总是玩弄世人 早上打开窗帘,阴雨的一天很昏暗...</span>
25+
</p>
26+
27+
28+
29+
## All Diary
830

931
- [2022]
1032
- [2023]
@@ -23,3 +45,13 @@ choose one day (not commend)
2345
[2022]: ./2022/
2446
[2023]: ./2023/
2547
[2024]: ./2024/
48+
49+
<style>
50+
.date {
51+
font-style: italic;
52+
font-weight: 600;
53+
}
54+
.details {
55+
color: #878787;
56+
}
57+
</style>

run.py

+21-49
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from datetime import date
33
import os
44

5+
from utils import get_dir_year_month
6+
57
big_month = [1, 3, 5, 7, 8, 10, 12]
68

79
month_char = [
@@ -133,35 +135,6 @@ def adjust_begin_index(y, m):
133135
print("new begin: ", begin_index)
134136

135137

136-
list_file_depth = 2
137-
current_depth = 0
138-
all_file_name = []
139-
140-
141-
def list_all_files(root_dir):
142-
_files = []
143-
exclude_path = ['.github', '.vscode', '.gitignore', 'LICENSE', '.git']
144-
# 列出文件夹下的所有目录和文件
145-
l1 = os.listdir(root_dir)
146-
l1 = list(filter(lambda e: e not in exclude_path, l1))
147-
global current_depth
148-
current_depth += 1
149-
# print(l1)
150-
all_file_name.append(l1)
151-
152-
if current_depth >= list_file_depth:
153-
return _files
154-
155-
for i in range(0, len(l1)):
156-
path = os.path.join(root_dir, l1[i])
157-
if os.path.isdir(path):
158-
_files.extend(list_all_files(path))
159-
if os.path.isfile(path):
160-
_files.append(path)
161-
162-
return _files
163-
164-
165138
def create_today_file(cp=False):
166139
today = date.today()
167140
today = str(today).split('-')
@@ -204,27 +177,23 @@ def create_today_file(cp=False):
204177

205178

206179
def create_someday_file(year, month, day):
207-
list_all_files(project_path)
208-
print("all_file_name", all_file_name)
209-
# [['2022', '2031'], ['11', '10'], ['12']]
210-
p1 = all_file_name[0]
211-
p1 = list(filter(lambda e: e.isdigit(), p1))
212-
print("path depth1, year:", p1)
213-
if year not in p1:
180+
obj = get_dir_year_month(project_path)
181+
print("get_dir_year_month", obj)
182+
years = obj.keys()
183+
print("path depth1, years:", years)
184+
if year not in years:
214185
os.mkdir(project_path + '/' + year)
215186
os.mkdir(project_path + '/' + year + '/' + month)
216187
print("mkdir year and month, y m is ", year, month)
217-
print("cp file: meanwhile add readme and all links about this month")
188+
print("create_someday_file: meanwhile add readme and all links about this month")
218189
everyday(int(year), int(month))
219190
else:
220-
year_index = p1.index(year)
221-
print("year index", year_index)
222-
p2 = all_file_name[year_index + 1]
223-
print("path depth2, month:", p2)
224-
if month not in p2:
191+
months = obj[year]
192+
print("path depth2, month:", months)
193+
if month not in months:
225194
os.mkdir(project_path + '/' + year + '/' + month)
226195
print("mkdir month, m is ", month)
227-
print("cp file: meanwhile add readme and all links about this month")
196+
print("create_someday_file: meanwhile add readme and all links about this month")
228197
everyday(int(year), int(month))
229198

230199

@@ -362,7 +331,7 @@ def insert_line(file_path, insert_info):
362331
'What do you want to do? \n' \
363332
' 1: create today file or add something in today file.\n' \
364333
' use command, not commended\n' \
365-
' 2: list all files but only depth 2\n' \
334+
' 2: list all year/month dirs but only depth 2\n' \
366335
' 3: copy file to today file \n' \
367336
'>> 4: commend -> copy file to appoint file \n' \
368337
' 5: quit and create a file named temp to write \n' \
@@ -379,11 +348,8 @@ def insert_line(file_path, insert_info):
379348
create_today_file()
380349
break
381350
elif judge == '2':
382-
list_all_files(project_path)
383-
# merge
384-
all_file_name = sum(all_file_name, [])
385-
all_file_name = list(filter(lambda e: e.isdigit(), all_file_name))
386-
print(all_file_name)
351+
obj = get_dir_year_month(project_path)
352+
print(obj)
387353
break
388354
elif judge == '3':
389355
create_today_file(True)
@@ -392,7 +358,13 @@ def insert_line(file_path, insert_info):
392358
print("please input year month day, as like '2022 01 01' ")
393359
new_in = input()
394360
new_in = new_in.split(' ')
361+
if len(new_in) != 3:
362+
print('error input, exit')
363+
break
395364
year, month, day = new_in[0], new_in[1], new_in[2]
365+
if len(year) != 4 and len(month) != 2 and len(day) != 2:
366+
print('error input, exit')
367+
break
396368
create_someday_file(year, month, day)
397369
cp_file(year, month, day)
398370
break

test.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
14
def modify_line_find(filename, find_str):
25
all_line = ""
36
with open(filename, "r+", encoding='utf-8') as file_handle:
@@ -16,5 +19,11 @@ def modify_line_find(filename, find_str):
1619
file_handle.writelines(all_line)
1720

1821

19-
if __name__ == '__main__':
20-
modify_line_find("temp", '04')
22+
project_path = os.path.realpath(os.path.dirname(__file__))
23+
print("project_path:", project_path)
24+
filename = f'{project_path}/temp'
25+
26+
27+
28+
# if __name__ == '__main__':
29+
# modify_line_find("temp", '04')

0 commit comments

Comments
 (0)