-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf.py
executable file
·98 lines (76 loc) · 2.64 KB
/
f.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
#!/usr/bin/env python3
from os import path, walk
import subprocess
from typing import List
root_dir = path.dirname(path.realpath(__file__))
index_contents = []
def fmt_md(file: str):
file = path.join(root_dir, file)
with open(file, "r+") as f:
text = f.read()
text = (
text.replace("\t", " ", -1)
.replace("‘", "'", -1)
.replace("’", "'", -1)
.replace("“", '"', -1)
.replace("”", '"', -1)
.replace("(", " (", -1)
.replace(")", ") ", -1)
.replace("《", " <", -1)
.replace("》", "> ", -1)
.replace("【", " [", -1)
.replace("】", "] ", -1)
.replace(",", ", ", -1)
.replace("、", ", ", -1)
.replace("。", ". ", -1)
.replace(";", "; ", -1)
.replace(":", ": ", -1)
.replace("!", "! ", -1)
.replace("?", "? ", -1)
.replace("–", "-", -1)
.replace("′", "'")
.replace(" ", "", -1)
.replace(" ", " ", -1)
)
f.seek(0)
f.write(text)
f.truncate()
def get_files_from_git() -> List[str]:
subprocess.run(["git", "add", "--all"])
output = subprocess.run(["git", "status", "-s"], capture_output=True)
files = output.stdout.decode("utf-8").split("\n")
files = filter(lambda s: s.endswith(".md") and (not s.startswith("D ")), files)
files = map(
lambda s: s.split("->").pop().strip() if s.startswith("R") else s[1:].strip(),
files,
)
return files
# Format files
for file in get_files_from_git():
fmt_md(file)
# Build index
def gen_index_contents(sub_path):
file_infos = []
article_dir = path.join(root_dir, sub_path)
for _, _, filenames in walk(article_dir):
for filename in filenames:
if filename.startswith("quantum-0"):
continue
p = path.join(article_dir, filename)
file_infos.append({"path": p, "name": filename})
file_infos.sort(key=lambda info: info["name"])
for i in file_infos:
with open(i["path"], "r") as f:
lines = f.readlines()
title = lines[1].split(":").pop().strip()
content = f'- [{title}]({sub_path}/{i["name"]})'
index_contents.append(content)
years = ["2025"]
def write_index_file():
for year in years:
index_contents.append(f"### {year}\n")
gen_index_contents(year)
index_contents.append("")
with open(path.join(root_dir, "index.md"), "a") as f:
f.write("\n".join(index_contents))
# write_index_file()