-
Notifications
You must be signed in to change notification settings - Fork 513
/
make_doc.py
128 lines (109 loc) · 4.82 KB
/
make_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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import shutil
import sys
from version import implementation
impl_name = '.'.join(str(x) for x in implementation[:3])
major = '.'.join(str(x) for x in implementation[:2])
# hack sys.path to be able to import markdown
sys.path.insert(0, os.path.join(os.path.dirname(os.getcwd()),
'www','src','Lib','browser'))
import markdown
# restore original sys.path
del sys.path[0]
# path of markdown files
md_doc_path = os.path.join(os.path.dirname(os.getcwd()), 'www', 'doc')
# static doc is in static_doc/en/... and in static_doc/3.x/en/...
static_doc_paths = [os.path.join(os.path.dirname(os.getcwd()), 'www',
'static_doc'),
os.path.join(os.path.dirname(os.getcwd()), 'www',
'static_doc', major)]
md_tutorial_path = os.path.join(os.path.dirname(os.getcwd()), 'www',
'tutorial')
static_tutorial_path = os.path.join(os.path.dirname(os.getcwd()), 'www',
'static_tutorial')
src_paths = (static_doc_paths +
[os.path.join(p,'cookbook') for p in static_doc_paths] +
[static_tutorial_path])
for path in src_paths:
if not os.path.exists(path):
os.mkdir(path)
# copy css
for static_doc_path in static_doc_paths:
shutil.copy(os.path.join(md_doc_path,'doc_brython.css'),
os.path.join(static_doc_path,'doc_brython.css'))
# copy images
images_dir_src = os.path.join(md_doc_path,'images')
for static_doc_path in static_doc_paths:
images_dir_dest = os.path.join(static_doc_path,'images')
if not os.path.exists(images_dir_dest):
os.mkdir(images_dir_dest)
for img in os.listdir(images_dir_src):
shutil.copy(os.path.join(images_dir_src, img), images_dir_dest)
with open(os.path.join(md_tutorial_path, "index.html"),
encoding="utf-8") as f:
index_tutorial = f.read()
# documentation
def make_static_doc(lang, static_doc_path):
dest_path = os.path.join(static_doc_path, lang)
dest_paths = [dest_path, os.path.join(dest_path,'cookbook')]
index = open(os.path.join(md_doc_path, lang,
'index_static.html'), 'rb').read()
index = index.decode('utf-8')
index = index.replace('[[version]]', major)
for path in dest_paths:
if not os.path.exists(path):
os.mkdir(path)
print('static doc %s' %lang)
for i, (src_path, dest_path) in enumerate(zip([os.path.join(md_doc_path, lang),
os.path.join(md_doc_path,lang,'cookbook')], dest_paths)):
for filename in os.listdir(src_path):
ext = os.path.splitext(filename)[1]
if ext == '.md':
src = open(os.path.join(src_path, filename), 'rb').read()
src = src.decode('utf-8')
html, scripts = markdown.mark(src)
out = open(os.path.join(dest_path, filename[:-3]+'.html'), 'wb')
html = index.replace('<content>',html)
html = html.replace('<prefix>','/'.join(['..'] * (i + 1)))
if i == 1:
html = html.replace('class="navig" href="',
'class="navig" href="../')
if scripts:
script_content = "\n".join(scripts)
html = html.replace('<scripts>', script_content)
out.write(html.encode('utf-8'))
out.close()
elif ext=='.txt':
shutil.copy(os.path.join(src_path, filename),
os.path.join(dest_path, filename))
elif os.path.isdir(os.path.join(src_path, filename)) \
and filename != 'cookbook':
dest_dir = os.path.join(dest_path, filename)
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
shutil.copytree(os.path.join(src_path, filename), dest_dir)
for lang in ['fr', 'en']:
for static_doc_path in static_doc_paths:
make_static_doc(lang, static_doc_path)
# tutorial
for lang in ['br', 'fr', 'en', 'es', 'it', 'pt-br']:
print(f"tutorial {lang}")
md_path = os.path.join(md_tutorial_path, lang)
static_path = os.path.join(static_tutorial_path, lang)
if os.path.exists(md_path):
if not os.path.exists(static_path):
os.mkdir(static_path)
for filename in os.listdir(md_path):
print(md_path, filename)
basename, ext = os.path.splitext(filename)
if ext == '.md':
src_path = os.path.join(md_path, filename)
with open(src_path, encoding="utf-8") as f:
src = f.read()
src = src.replace('{implementation}', impl_name)
html, scripts = markdown.mark(src)
dest_path = os.path.join(static_path, basename + '.html')
print("save in", dest_path)
content = index_tutorial.replace("{{content}}", html)
with open(dest_path, 'w', encoding="utf-8") as out:
out.write(content)