-
Notifications
You must be signed in to change notification settings - Fork 438
/
change_code_reference.py
52 lines (44 loc) · 1.7 KB
/
change_code_reference.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
# coding = utf8
import os
import re
def replace_func(match):
full_text = match.group(0)
if "int " in full_text or "char " in full_text or "void " in full_text or "PyObject" in full_text or "#define " in full_text or "typedef " in full_text or "usedpools" in full_text or "for (" in full_text:
lang = "c"
elif "redis-cli" in full_text or "cd " in full_text or "git " in full_text or "mkdir " in full_text or "Timer " in full_text:
lang = "shell script"
else:
lang = "python3"
if full_text[-2] != "\n":
full_text += "\n"
middle = full_text[1:-2]
final_middle = ""
for line in middle.split("\n"):
if line:
if line[0] == "\t":
skip_index = 1
else:
skip_index = 4
final_middle += line[skip_index:] + "\n"
else:
final_middle += "\n"
full_text = full_text[0] + "\n```" + lang + final_middle + "\n```" + full_text[-2:]
return full_text
def replace(text):
r = re.sub("\n\n((((?:\t| ).+?\n)|\n)+)(?:\n(?!\t| )|$)", replace_func, text, flags=re.DOTALL)
return r
def replace_all():
for each in os.walk("./"):
if each[2]:
for each_file in each[2]:
suffix = each_file.split(".")[-1]
if suffix == "md":
full_file = each[0] + os.path.sep + each_file
with open(full_file, "r") as f:
text = f.read()
replaced_text = replace(text)
with open(full_file, "w") as f:
f.write(replaced_text)
print("replaced: %s" % (full_file, ))
if __name__ == "__main__":
replace_all()