forked from emqx/emqx-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
executable file
·94 lines (79 loc) · 2.49 KB
/
gen.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
import yaml
import json
import sys
import shutil
if len(sys.argv) != 2:
print('expecting ce or ee as arg1')
exit(1)
if sys.argv[1] != r'ce' and sys.argv[1] != r'ee':
print('expecting ce or ee as arg1')
exit(2)
## check if the 'lang' field matches expected input
## when no 'lang' is defined, it matches both 'en' and 'cn'
def is_lang_match(i, en_or_cn):
if 'lang' in i:
return i['lang'] == en_or_cn
else:
return True
EDITION = sys.argv[1]
## check if the 'edition' field matches expected input
## when no 'edition' is defined, it matches both 'ce' and 'ee'
def is_edition_match(i, ce_or_ee):
if 'edition' in i:
return i['edition'] == ce_or_ee
else:
return True
def copy_cfg_md(lang):
if lang == 'en':
dir = 'en_US'
else:
dir = 'zh_CN'
src = dir + '/admin/cfg-' + EDITION + '.md'
dst = dir + '/configuration/configuration-manual.md'
shutil.copyfile(src, dst)
def read_title_from_md(lang, path):
if lang == 'en':
dir = 'en_US'
else:
dir = 'zh_CN'
path = dir + '/' + path + '.md'
with open(path) as f:
for line in f:
if line.strip():
return line.strip('\n').strip('#').strip()
def parse(children, lang, edition):
acc=[]
for i in range(len(children)):
child = children[i]
if not is_lang_match(child, lang):
continue
if not is_edition_match(child, edition):
continue
if 'title_en' in child:
title = child['title_en']
if lang == 'cn' and 'title_cn' in child:
title = child['title_cn']
else:
title = read_title_from_md(lang, child)
_child = {'title': title}
if isinstance(child, str):
_child['path'] = child
else:
if 'path' in child:
_child['path'] = child['path']
if 'children' in child:
godeep = parse(child['children'], lang, edition)
_child['children'] = godeep
acc.append(_child)
return acc
copy_cfg_md('en')
copy_cfg_md('cn')
with open(r'dir.yaml', encoding='utf-8') as file:
# The FullLoader parameter handles the conversion from YAML
# scalar values to Python the dictionary format
all = yaml.load(file, Loader=yaml.FullLoader)
en = parse(all, 'en', EDITION)
cn = parse(all, 'cn', EDITION)
res ={'en': en, 'cn': cn}
json.dump(res, sys.stdout, indent=2, ensure_ascii=False)