-
Notifications
You must be signed in to change notification settings - Fork 28
/
doc.py
49 lines (41 loc) · 1.32 KB
/
doc.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
class Doc:
def __init__(self):
self.__fields = {}
def __parse_title(self, line):
if line.startswith('# '):
return (1, line[2:].strip('\r\n '))
elif line.startswith('## '):
return (2, line[3:].strip())
return (0,line)
def parse(self,path):
try:
f = open(path, 'r')
except FileNotFoundError as e:
print(e)
return
prefix = []
level = 0
content = []
line = f.readline()
while line:
cur_level,title = self.__parse_title(line)
if cur_level > 0:
if level > 0:
self.__fields['.'.join(prefix)] = ''.join(content)
content.clear()
if cur_level == 1:
prefix = [title]
elif cur_level == 2:
prefix = [prefix[0], title]
level = cur_level
content.append(line)
line = f.readline()
if level > 0:
self.__fields['.'.join(prefix)] = ''.join(content)
content.clear()
f.close()
def get(self, name):
if self.__fields.__contains__(name):
return self.__fields[name]
else:
return '**无文档**'