-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhomepage.py
120 lines (96 loc) · 3.65 KB
/
homepage.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
import os
import re
from utils import (
find_line_info_with_index,
find_lines_index_with_string,
get_dir_year_month,
)
project_path = os.path.realpath(os.path.dirname(__file__))
print("project_path:", project_path)
filename = f"{project_path}/index.md"
count_last_files = 5
def get_last(sorted_data):
index = 0
res = []
for year, months in sorted_data.items():
for month in months:
path = f"{year}/{month}"
days = os.listdir(path)
days = sorted(days, reverse=True)
for day in days:
res.append(f"{path}/{day}")
index += 1
if index >= count_last_files:
return res
return res
def modify_with_html(last_info) -> str:
res = "\n\n"
for v in last_info:
# -3: .md
title = v[:-3]
file_path = f"{project_path}/{v}"
details = get_recent_details(file_path)
gold_section = round(0.618 * len(details))
details_a = details[0:gold_section]
details_b = details[gold_section:]
one = "<p>\n"
link = f'<a href="./{title}">{details_b}...</a>'
one += f'<span class="date">{title}</span><br />\n'
one += f'<span class="details">{details_a}{link}</span>\n'
one += "</p>\n"
res += one
res += "\n\n"
return res
def get_recent_details(file_path):
with open(file_path, "r+", encoding="utf-8") as file_handle:
all_line = file_handle.readlines()
res = ""
for index, value in enumerate(all_line):
if value.startswith("#") or value == "\n":
continue
res += value
res = res.replace("\n", " ")
# 将多个空格替换为一个
res = re.sub(r"\s+", " ", res)
if len(res) > 30:
break
res = res[:30]
return res
def update_recent(file_path, rm_begin_line, rm_end_line, recent_info):
temp_filename = "temptemp"
# 打开原始文件以读取模式和一个临时文件以写入模式
with open(file_path, "r", encoding="utf-8") as original_file, open(
temp_filename, "w", encoding="utf-8"
) as temp_file:
ori_lines = original_file.readlines()
temp_file.write("".join(ori_lines[:rm_begin_line]))
temp_file.write("".join(recent_info))
temp_file.write("".join(ori_lines[rm_end_line:]))
# 替换原文件为临时文件的内容
os.replace(temp_filename, file_path)
if __name__ == "__main__":
get_ym_info = get_dir_year_month(project_path)
print(get_ym_info)
# sorted_data = dict(sorted(aa.items(), reverse=True))
sorted_data = {
year: sorted(months, reverse=True)
for year, months in sorted(get_ym_info.items(), reverse=True)
}
print("sorted_data", sorted_data)
last_info = get_last(sorted_data)
print("last_info", last_info)
# 找到要删除的部分
find_last_index = find_lines_index_with_string(filename, "最近更新")
print("find_last_index", find_last_index)
find_info = find_line_info_with_index(filename, find_last_index[0])
print("find_info", find_info)
count_hashes = find_info.count("#")
find_index_same_title_lv = find_lines_index_with_string(
filename, "#" * count_hashes
)
print("find_index_same_title_lv", find_index_same_title_lv)
index_in_same_title = find_index_same_title_lv.index(find_last_index[0])
next_index = find_index_same_title_lv[index_in_same_title + 1]
print("next_index", next_index)
rm_begin_line, rm_end_line = find_last_index[0], next_index - 1
update_recent(filename, rm_begin_line, rm_end_line, modify_with_html(last_info))