-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
__USE_PREPARE_index.py
executable file
·324 lines (295 loc) · 10 KB
/
__USE_PREPARE_index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
# Custom index for the SageMath Documentation
# Purpose: It generates the index files needed for the CDN.
#
# Usage:
# Run it only **once** (!) with the argument "fix" to repair all those
# relative static links. That's necessary to work well with the CDN
# network at GitHub. (later, run it without "fix")
#
# After indexing, check the index.html file and maybe blacklist more PDFs (many are just pictures)
# by appending to the `filter_ref` list.
#
# Note: the script assumes the html and pdf files are copied after
# running doc-html and doc-pdf targets. e.g. like this:
# $ nice ionice -c 3 sh -c 'make -j2 && make doc-html && make doc-pdf'
# then copy
# $ cp -alR $SAGE_ROOT/local/share/doc/sage/pdf .
# $ cp -alR $SAGE_ROOT/local/share/doc/sage/html .
#
# License: Apache 2.0
# Author: Harald Schilly <[email protected]>
from glob import glob
from collections import defaultdict
from old_versions import OLD_VERSIONS
LANG = {
"en": "English",
"es": "Spanish",
"hu": "Hungarian",
"pt": "Portuguese",
"de": "German",
"ru": "Russian",
"fr": "French",
"it": "Italian",
"ca": "Catalan",
"tr": "Turkish",
"ja": "Japanese",
}
intro = """\
<!DOCTYPE html>
<!--
DO NOT EDIT THIS PAGE -- AUTOGENERATED VIA index.py
-->
<head>
<title>SageMath Documentation %(path)s</title>
<link rel="stylesheet" href="/html/en/website/_static/sage.css" type="text/css" />
<style>
html {margin: 20px; font-family: sans-serif; font-size: 90%%; }
body { background-color: white; max-width: 1200px; }
h2 { margin: 0;}
a {text-decoration: none;}
a:hover {text-decoration: underline; }
div.table { display: table; }
div.table.ov { margin-bottom: 1rem; }
div.row { display: table-row; }
div.row:hover {background: #eef; }
div.cell { display: table-cell; padding: 0 2em 0.5em 0; line-height: 1.5em;}
div.lang {font-weight: bold; }
div.row.en {border: 1px solid #ccf;}
div.row.last {height: 3em;}
div.entry {margin: 1em 1em 1em 0; display: inline; white-space: nowrap;}
div.entry.lang-en {font-weight: bold;}
</style>
<link rel="shortcut icon" href="/html/en/website/_static/favicon.ico"/>
</head>
<body>
<a href="https://www.sagemath.org/">
<img width="350" alt="SageMath Logo"
style="border-radius: 10px;"
src="https://www.sagemath.org/pix/sage_logo_new_l_hc_edgy-nq8.png"
title="SageMath Mathematical Software">
</a>
"""
filter_ref = [
"graph-",
"graph_plot-",
"shapes-",
"plot3d-",
"platonic-",
"knot-",
"graph_generators-",
"dyck_word-",
"hyperbolic_regular_polygon-",
"plot-",
"generic_graph-",
"fully_packed_loop",
"link-",
"complex_plot-",
"hyperbolic_geodesic-",
"contour_plot-",
"density_plot-",
"plot_field-",
"chart-",
"point_configuration-",
"text-",
"point-",
"parametric_plot3d-",
"revolution_plot3d-",
"ordered_tree-",
"implicit_plot3d-",
"polygon-",
"streamline_plot-",
"vectorfield-",
"tangent_vector-",
"curve-",
'hyperbolic_polygon-',
"arrow-",
"circle-",
"bezier_path-",
"mv_polytopes-",
"set_partition-",
"finite_coxeter_groups-",
"integrated_curve-",
"list_plot3d-",
"calculus-",
"index_face_set-",
"multigraphics-",
"vector_calc_cartesian-",
"vector_calc_change-",
"vector_calc_plane-",
"graphics-",
"topological_submanifold-",
"elementary-",
"glca-",
"frieze-",
"families-",
"random-",
"fully_commutative_elements-",
"matrix_plot-",
"subset-",
"ellipse-",
"packaging_sage_library-",
"parametric_surface-",
"shapes2-",
"plot_field3d-",
"hyperbolic_arc-",
"line-",
"arc-",
"disk-",
"bar_chart-",
"step-",
"scatter_plot-",
"histogram-",
"operation_table-",
"tableau-",
"library-",
]
filter_html = [("en", "website")]
pages = {
"html": defaultdict(lambda: defaultdict(list)),
"pdf": defaultdict(lambda: defaultdict(list))
}
for path in glob("*/*"):
what, lang = path.split("/")
for path2 in glob(path + "/*"):
if what == "html":
EXT = "/index.html"
for fn in glob(path2 + EXT):
pages[what][lang][""].append(fn)
elif what == "pdf":
for EXT in ["/*.pdf", "/*/*.pdf"]:
for fn in glob(path2 + EXT):
dirs = fn.split("/")
# this is only for english reference
if len(dirs) >= 5:
subcat = dirs[2].title()
else:
subcat = ""
pages[what][lang][subcat].append(fn)
ROBOTS_TXT = [
"User-agent: *",
"Disallow: /html/*.txt$",
]
output = [intro % {"path": ""}]
output.append("<h1>Documentation</h1>")
# TODO fix how this is deployed, then set it to true
SHOW_OLDER_VERSIONS = False
if SHOW_OLDER_VERSIONS:
output.append(
"<div class='table ov'><div class='row first'><div class='cell'>Older versions:</div>"
)
for ov in OLD_VERSIONS:
if SHOW_OLDER_VERSIONS:
output.append(
f"<div class='cell'><a href='./{ov}/index.html'>{ov}</a></div>")
ROBOTS_TXT.append(f"Disallow: /{ov}/")
with open("robots.txt", "w") as index:
index.write("\n".join(ROBOTS_TXT))
output.append("</div></div></div>")
sortlang = lambda k_v: LANG.get(k_v[0], k_v[0].title())
output.append('<div class="table">')
for what in sorted(pages.keys()):
output.append('<div class="row first">')
output.append("<div class='cell'><h2>%s</h2></div>" % what.upper())
first_row = True
for lang, page_entries in sorted(pages[what].items(), key=sortlang):
for subcat, entries in sorted(page_entries.items()):
if not first_row:
output.append(
f'<div class="row {lang}"><div class="cell"></div>')
else:
first_row = False
subtitle = LANG.get(lang, lang)
if subcat != "":
subtitle += f"/{subcat}"
output.append(
f'<div class="cell lang"><a href="{what}/{lang}">{subtitle}</a></div>'
)
output.append('<div class="cell doc">')
for entry in sorted(entries):
entries = entry.split("/")
if what == "html":
if tuple(entries[-3:-1]) in filter_html:
continue
fn = entries[-2].replace("_", " ").title()
elif what == "pdf":
if any(entries[-1].startswith(_) for _ in filter_ref):
continue
fn = entries[-1][:-4].replace("_", " ").title()
output.append(
f"<div class='entry lang-{lang}'><a href='{entry}'>{fn}</a></div>"
)
output.append("</div></div>")
output.append('<div class="row last"></div>')
output.append("</div>")
output.append("</body></html>")
with open("index.html", "w") as index:
index.write("\n".join(output))
# create minimal index.html files in subdirectories
IDX_TOKEN = "<!-- generated by index.py -->" # to recognize it later on!
import os
from os import walk, sep
from os.path import join
for subdir in ["html", "pdf"]:
for root, path, filenames in walk(subdir + sep):
# ignore files in _static, ...
root_dirs = root.split(sep)
if any(_.startswith("_") for _ in root_dirs):
continue
# if there is no index.html file
idxfn = join(root, "index.html")
if "index.html" not in filenames or IDX_TOKEN in open(idxfn,
"r").read():
# now we have to write our index.html file
print(f"{root_dirs} >>> {idxfn}")
index = [intro % {"path": root}]
index.append(IDX_TOKEN)
if len(root_dirs) > 1:
index.append('<p><a href="%s">Home</a></p>' %
'/'.join([".."] * (len(root_dirs) - 1)))
index.append('<p><a href="../index.html">Parent Directory</a></p>')
path = list(filter(lambda _: not _.startswith("_"), path))
if len(path) > 0:
index.append('<h2>Sub-Directories</h2>')
for p in sorted(path):
is_lang_list = len(root_dirs) == 1 or (
len(root_dirs) == 2 and root_dirs[1] == '')
name = LANG.get(p, p) if is_lang_list else p
index.append('<p><a href="%(p)s/">%(name)s</a></p>' % {
"p": p,
"name": name
})
def f_op(fn):
return not (fn == "index.html" or fn.startswith("genindex-")
or fn.startswith("."))
filenames = list(filter(f_op, filenames))
if len(filenames) > 0:
index.append('<h2>Documents</h2>')
for fn in sorted(filenames):
index.append('<p><a href="%(fn)s">%(fn)s</a></p>' %
{"fn": fn})
# print " ", path, filenames
index.append("</body></html>")
with open(idxfn, "w") as outidx:
outidx.write("\n".join(index))
# fix for https://github.com/sagemath/documentation/issues/23
if os.path.exists('pdf/en/reference/_static'):
print("removing pdf/en/reference/_static symlink")
os.system('rm pdf/en/reference/_static')
import sys
if len(sys.argv) > 1 and sys.argv[1] == "fix":
print(' fix the static links in sphinx _static files '.center(100, "#"))
print(' ONLY RUN THIS ONCE '.center(100, "!"))
import os
import subprocess as sp
os.chdir(os.path.join(os.path.dirname(__file__), "html"))
ROOT = os.path.abspath(os.curdir)
files_cmd = sp.Popen("find -type l -name _static".split(), stdout=sp.PIPE)
for link in files_cmd.stdout:
link = link.decode('utf8')
d = os.path.normpath(os.path.join(ROOT, link.rsplit("/", 1)[0]))
print(d)
os.chdir(d)
os.system(
r'find -type f -name "*.html" -exec sed -i "s/_static/..\/_static/" {} \;'
)