forked from harukamm/ocaml-blockly
-
Notifications
You must be signed in to change notification settings - Fork 21
/
update_docs.py
67 lines (56 loc) · 1.82 KB
/
update_docs.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
#!/usr/bin/python2.7
# Update the demo page in docs/ directory.
# - The source path must be relative to the current directory.
# - The base name of the source path is same with the destination filename by
# default.
sourceMap = {
"block_of_ocaml_utils.js": "block_of_ocaml/utils.js",
"converter.js": "block_of_ocaml/",
"blockly_compressed.js": "./",
"blocks_compressed.js": "./",
"typedlang_compressed.js": "./",
"en.js": "msg/js/",
"ja.js": "msg/js/",
"index.html": "demos/typed/dev.html",
"style.css": "demos/typed/",
"typed.js": "demos/typed/",
"eval.js": "demos/universe/",
"canvas.html": "demos/typed/",
}
import os, shutil
def verify_sourcepath():
for filename, source_path in sourceMap.items():
basename = os.path.basename(source_path)
if not basename:
sourceMap[filename] += filename
def copy_files():
for filename, src in sourceMap.items():
shutil.copy(src, 'docs/' + filename)
def edit_copied_files():
disable_devmode()
# TODO(harukam): Edit dev.html to load compressed JS files in docs/ instead
# of uncompressed ones.
pass
import re
def disable_devmode():
path = 'docs/typed.js'
if not os.path.isfile(path):
raise Exception('Not found script: ' + path)
pattern = r'^(Typed\.DEVMODE = )(true|false);'
replacement = r'\1false;'
with open(path, 'r+') as f:
# Find the statement `Typed.DEVMODE = (true|false);` in the script.
body = f.read()
m = re.search(pattern, body, flags=re.MULTILINE)
if not m:
raise Exception('Not found DEVMODE statement')
# Disable the debugging mode if it's enabled.
if m.group(2) == 'true':
replaced_body = re.sub(pattern, replacement, body, 1, flags=re.MULTILINE)
f.seek(0)
f.truncate()
f.write(replaced_body)
if __name__ == "__main__":
verify_sourcepath()
copy_files()
edit_copied_files()