-
Notifications
You must be signed in to change notification settings - Fork 1
/
autogen.py
53 lines (44 loc) · 1.32 KB
/
autogen.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
import os
f = open('template.yml', 'r')
content = f.read()
f.close()
lines = ['nav:', ' - Home: index.md']
ignore = [
'notes/index.md',
'notes/img',
'notes/js',
]
def generate_line(path, depth):
print(path)
f = open(path, 'r')
name = f.readline().rstrip()[2:]
f.close()
return ' ' * (depth + 1) + '- ' + name + ': ' + path[6:]
def to_name(name):
return ' '.join(map(lambda s: s.capitalize(), name.split('_')))
def add_dir(path, depth):
files = []
dirs = []
sub_dirs = {}
for name in os.listdir(path):
tmp_path = os.path.join(path, name)
if tmp_path in ignore:
continue
if os.path.isdir(tmp_path):
name_line = ' ' * (depth + 1) + '- ' + to_name(name) + ':'
dirs.append(name_line)
sub_dirs[name_line] = add_dir(tmp_path, depth + 1)
elif tmp_path.endswith('md'):
files.append(generate_line(tmp_path, depth))
ret = []
for dir_name in sorted(dirs):
ret.append(dir_name)
ret += sub_dirs[dir_name]
files = sorted(files)
if path.endswith('leetcode'):
files = sorted(files, key=lambda x: int(x.strip().split(' ')[1].split('.')[0]))
return ret + files
lines += add_dir('notes', 0)
f = open('mkdocs.yml', 'w')
f.write(content + '\n'.join(lines) + '\n')
f.close()