-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert.py
78 lines (60 loc) · 1.77 KB
/
convert.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
77
78
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description:
import os
import shutil
from common import read_cmd
YML = '''
site_name: 常用命令 # 站点名称
nav: # 文档目录
- 首页: index.md
{}
theme: readthedocs # 主题
'''
SOURCE_DIR = 'commands'
OUTPUT_DIR = 'docs'
def cmd2md(filename):
filepath = f'{SOURCE_DIR}/{filename}'
outfile = f'{OUTPUT_DIR}/{fmt_name(filename)}.md'
input_lines = read_cmd(filepath)
outlines = []
input_lines.sort(key = lambda x: x['title'])
title_key = set()
for line in input_lines:
title = line['subtitle']
cmd = line['title']
cmd_begin = ' '.join(cmd.split(" ")[:2])
if cmd_begin not in title_key:
title_key.add(cmd_begin)
outlines.append(f"## {cmd_begin}\n")
title = f'**{title[2:]}**\n'
cmd = f'```\n{cmd}\n```\n'
outlines.append(title)
outlines.append(cmd)
with open(outfile, 'w') as out:
out.writelines(outlines)
def fmt_name(name):
return name[4:]
def first_upper(name):
return name[0].upper() + name[1:]
def cmd2mdname(name):
return fmt_name(name) + '.md'
def main(dirname):
'''转换'''
files = os.listdir(dirname)
files.sort()
navs = []
for f in files:
if not f.startswith('cmd_'):
continue
cmd2md(f)
navs.append(f'- {first_upper(fmt_name(f))}: {fmt_name(f)}.md')
# shutil.move(f'{OUTPUT_DIR}/{cmd2mdname(files[0])}', f'{OUTPUT_DIR}/index.md')
nav = '\n '.join(navs)
# nav = nav.replace(cmd2mdname(files[0]), 'index.md')
yml = YML.format(nav)
with open('mkdocs.yml', 'w') as f:
f.write(yml)
if __name__ == "__main__":
main(SOURCE_DIR)