forked from lfortran/lfortran
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
202 lines (164 loc) · 4.78 KB
/
build.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
Script for conveniently building the documentation in all supported languages.
Run this script with:
.. code::
python3 build.py
You can also pass the language as an argument:
.. code::
python3 build.py en de
The first language will be handled as the default language.
"""
import json
import sys
import subprocess
from pathlib import Path
from typing import List, Dict
root = Path(__file__).parent
"""
Make sure to run this script from the root of the documentation repository.
"""
template = """
<!DOCTYPE HTML>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1; url={0}">
<script>
window.location.href = "{0}"
</script>
<title>Page Redirection</title>
If you are not redirected automatically, follow the <a href='{0}'>link</a>.
"""
all_redirects = {
"index.html": "{0}/",
**{
f"{name}/index.html": f"../{{0}}/{name}/"
for name in (
"installation",
"language",
"progress",
"design",
"developer_tutorial",
"ast_and_asr",
"contributing",
)
},
**{
f"intrinsics/{name}/index.html": f"../../../{{0}}/intrinsics/{name}/"
for name in (
"array/allocated",
"array/cshift",
"array/size",
"bit/bge",
"bit/bgt",
"bit/bit_size",
"bit/ble",
"bit/blt",
"bit/btest",
"bit/shiftl",
"bit/shiftr",
"bit/rshift",
"character/achar",
"character/adjustl",
"character/adjustr",
"character/char",
"character/lge",
"character/len_trim",
"kind-type/kind",
"mathematical/acos",
"mathematical/acosh",
"mathematical/asin",
"mathematical/asinh",
"mathematical/atan",
"mathematical/atan2",
"mathematical/atanh",
"mathematical/fraction",
"misc/command_argument_count",
"misc/cpu_time",
"misc/date_and_time",
"misc/new_line",
"numeric/abs",
"numeric/aimag",
"numeric/aint",
"numeric/anint",
"numeric/ceiling",
"numeric/cmplx",
"numeric/conjg",
"numeric/digits",
"numeric/dim",
"numeric/epsilon",
"numeric/erf",
"numeric/erfc",
"numeric/exp",
"numeric/floor",
"numeric/gamma",
"numeric/mod",
)
},
}
"""
All redirects from the original site without language component.
"""
all_languages: List[str]
"""
List of currently supported languages, taken from ``doc/src/_static/languages.json``.
To support a new language add its `language code`_ to the list.
.. _language code: https://www.sphinx-doc.org/en/master/usage/configuration.html#intl-options
"""
with open(root / "src" / "_static" / "languages.json") as fd:
all_languages = list(json.load(fd).values())
def build_docs(language: str) -> None:
"""
Build the documentation for a single language.
Parameters
----------
language : str
The language to build the documentation for.
"""
subprocess.run(
[
"sphinx-build",
"-b",
"dirhtml",
str(root / "src"),
str(root / "site" / language),
f"-Dlanguage={language}",
],
cwd=root,
)
def build_redirects(redirects: Dict[str, str], language: str) -> None:
"""
Build the redirects for a single language.
Parameters
----------
redirects : Dict[str, str]
Page redirects to build.
language : str
The language to build the redirects for.
"""
for source, target in redirects.items():
source_path = root / "site" / source
redirect = template.format(target.format(language))
if not source_path.parent.exists():
source_path.parent.mkdir(parents=True)
with open(source_path, "w", encoding="utf-8") as fd:
fd.write(redirect)
def build_all(redirects: Dict[str, str], languages: List[str]) -> None:
"""
Build the documentation for all languages.
Parameters
----------
redirects : Dict[str, str]
Page redirects to build.
languages : List[str]
List of languages to build the documentation for.
"""
for language in languages:
build_docs(language)
build_redirects(redirects, languages[0])
if __name__ == "__main__":
languages = sys.argv[1:] if len(sys.argv) > 1 else all_languages
build_all(all_redirects, languages)
print()
print("Preview the documentation using")
print()
print(f" python3 -m http.server -d \"{root / 'site'}\"")
print()