/
auto.py
75 lines (63 loc) · 2.33 KB
/
auto.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
import os
import re
import json
ab_path = os.path.dirname(os.path.abspath(__file__))
pages = {}
print(f'当前工作目录:{ab_path}')
def input_dir():
recv = input('请输入你要生成文档的目录(首尾不要斜杠):')
recv_path = ab_path+'/'+recv
return (recv, recv_path) if os.path.isdir(recv_path) else input_dir()
def input_file(rela_path):
recv = input('请输入你要用作主页index的文档文件(基于上述目录):')
recv_path = ab_path+'/'+rela_path+'/'+recv
return (recv, recv_path.strip().lower()) if os.path.isfile(recv_path) else input_file(rela_path)
def operate_dict(path):
pointer = pages
if path:
sep = [x for x in path.split('/') if x != '']
for i in sep:
if i not in pointer.keys():
pointer[i] = {}
pointer = pointer[i]
return pointer
def get_title(file_path):
title = ''
try:
with open(file_path, encoding='utf-8') as file:
lines = file.readlines()
for o in lines:
line = o.strip()
if line.startswith('#'):
title = re.sub(r'#+\s*', '', line)
break
return title
except Exception:
print('Failed to get title:', file_path)
return ''
if __name__ == "__main__":
rela_path, root_dir = input_dir()
(index_file, index_path) = input_file(rela_path)
for root, dirs, files in os.walk(root_dir):
root = root.replace('\\', '/')
clean_path = root.split('/'+rela_path)[1]
path = rela_path+clean_path
operating = operate_dict(clean_path)
for i in files:
file_path = path+'/'+i
print('Walking:', i)
file_title = get_title(ab_path+'/'+file_path)
operating[i] = [file_path, file_title]
config = {
"doc_title": "Document",
"auto_url": True,
"jump_hist": True,
"index": {},
"pages": pages,
"not_found": ["404.md", "404"]
}
index_title = get_title(index_path)
config["index"] = [rela_path+'/'+index_file, index_title]
with open(ab_path+'/config.json', 'w+', encoding='utf-8') as file:
file.write(json.dumps(config, indent=2, ensure_ascii=False))
print('Config file generated successfully! ')