-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (63 loc) · 2.08 KB
/
main.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
import sys
try:
filename = sys.argv[1]
except IndexError:
print("[!] Use: python main.py [filename]")
exit()
file = open(filename, "r", encoding="utf-8")
output = open("output1.md", "w", encoding="utf-8")
structure = {"number": 0}
pre = []
len_prefix = 999
code = False
for line in file.readlines():
if line.startswith("```"):
code = True if not code else False
if line.startswith("#") and not code:
sline = line.strip().split(" ", 2)
if len(sline) == 3:
is_line_numbers = True
if sline[1].endswith("."):
line_numbers = sline[1][:-1].split(".")
for i in line_numbers:
try:
int(i)
except ValueError:
is_line_numbers = False
else:
is_line_numbers = False
if is_line_numbers:
sline = [sline[0], sline[2]]
else:
sline = [sline[0], sline[1] + " " + sline[2]]
prefix = sline[0]
content = sline[1]
if len_prefix > len(prefix):
if len(prefix) == 1:
pre = []
else:
nlp = len_prefix - len(prefix)
s = []
for i in range(len(pre) - 2):
s.append(pre[i])
pre = s
if len_prefix == len(prefix):
pre[len(prefix) - 1] = content
else:
pre.append(content)
if len(pre) == 1:
structure["number"] += 1
line = prefix + " " + str(structure["number"]) + ". " + content
else:
if structure.get("-".join(pre[:-1])) is None:
structure["-".join(pre[:-1])] = 0
structure["-".join(pre[:-1])] += 1
f = []
line = str(structure["number"]) + "."
for i in pre[:-1]:
f.append(i)
line += str(structure["-".join(f)]) + "."
line = prefix + " " + line + " " + content
len_prefix = len(prefix)
line += "\n"
output.write(line)