-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert.py
700 lines (600 loc) · 32.4 KB
/
convert.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
"""
This script was used for the initial conversion of the generated HTML to what is now inside pages/.
Most likely, you do not want to use this anymore, and if you want to do changes, just change the HTML files in pages/ directly.
"""
import os
import re
import json
import functools
import collections
def get_html_files():
files = [f for f in os.listdir(src_folder_path()) if re.match(r'^.*\.html$', f)]
return files
def folder_path():
return os.path.dirname(os.path.realpath(__file__))
def src_folder_path():
return os.path.join(folder_path(), "html_in/")
def out_folder_path():
return os.path.join(folder_path(), "html_out/")
def check_for_tag_mismatch(html):
m = []
if html.count("<table") != html.count("</table>"):
m.append("<table>")
if html.count("<td") != html.count("</td>"):
m.append("<td>")
if html.count("<div") != html.count("</div>"):
m.append("<div>")
if html.count("<span") != html.count("</span>"):
m.append("<span>")
if html.count("<code") != html.count("</code>"):
m.append("<code>")
if html.count("<li>") != html.count("</li>"):
m.append("<li>")
if html.count("<ul>") != html.count("</ul>"):
m.append("<ul>")
if html.count("<ol>") != html.count("</ol>"):
m.append("<ol>")
if html.count("<dd>") != html.count("</dd>"):
m.append("<dd>")
if html.count("<dt") != html.count("</dt>"):
m.append("<dt>")
return m
def cleanup(html):
html = re.sub(r"<br ?/?>\s*</td>", "</td>", html, flags=re.MULTILINE)
return html
def insert_table_links():
files = get_html_files()
file_names = [f[:-5] for f in files]
file_names_re = "|".join([re.escape(fn) for fn in file_names if len(fn.strip()) > 0 and fn != "list" and fn != "or" and fn != "and"])
for f in files:
fpath = os.path.join(src_folder_path(), f)
with open(fpath, 'r') as r:
content = r.read()
content = re.sub(f"( |<b>)(?:<span class=\"nolinebreak\">)?({file_names_re})(?:</span>)?( |</b>)", "\\1<a href=\"\\2.html\">\\2</a>\\3", content)
with open(fpath, "w", encoding="utf-8") as outf:
outf.write(content)
def insert_escaped_links():
files = get_html_files()
file_names = [f[:-5] for f in files if f.startswith("002") or "_002" in f or "_006" in f]
file_names = [(fn.replace("_002d", "-")
.replace("_002a", "*")
.replace("_002b", "+")
.replace("_002f", "/")
.replace("_0028", "(")
.replace("_0029", ")")
.replace("_0060", "`")
.replace("_0026", "&")
.replace("_0027", "'")
, fn) for fn in file_names if len(fn.strip()) > 0]
for f in files:
fpath = os.path.join(src_folder_path(), f)
with open(fpath, 'r') as r:
content = r.read()
for fprint, flink in file_names:
if fprint.startswith("002a"):
fprint = "*" +fprint[4:]
elif fprint.startswith("002b"):
fprint = "+" +fprint[4:]
elif fprint.startswith("002d"):
fprint = "-" +fprint[4:]
elif fprint.startswith("002f"):
fprint = "/" +fprint[4:]
elif fprint.startswith("0028"):
fprint = "(" +fprint[4:]
if "002" in fprint or "006" in fprint:
print(fprint)
assert(not "002" in fprint)
assert(not "006" in fprint)
content = content.replace(f" {fprint} ", f" <a href=\"{flink}.html\">{fprint}</a> ")
content = content.replace(f" <span class=\"nolinebreak\">{fprint}</span> ", f" <a href=\"{flink}.html\">{fprint}</a> ")
content = content.replace(f"<b><span class=\"nolinebreak\">{fprint}</span></b>", f"<b><a href=\"{flink}.html\">{fprint}</a></b>")
content = content.replace(f"<b>{fprint}</b>", f"<a href=\"{flink}.html\">{fprint}</a>")
with open(fpath, "w", encoding="utf-8") as outf:
outf.write(content)
def main():
files = get_html_files()
nodes = []
to_write = []
backlinks = {}
keys_to_label = {}
all_fnames = [f for f in files]
parents = {}
for f in files:
fpath = os.path.join(src_folder_path(), f)
outpath = os.path.join(out_folder_path(), f)
with open(fpath, 'r') as r:
all_text = r.read()
in_section = False
out = []
section_cnt = 0
in_style = False
in_header = False
in_table = False
in_arguments = False
in_syntax_table = False
n_up = None
n_prev = None
n_next = None
n_next_title = None
n_up_title = None
n_prev_title = None
title = None
numbering = None
skip = 0
lines = all_text.split("\n")
nextl = None
if f == "index.html":
out = lines
else:
for ix, l in enumerate(lines):
if skip > 0:
skip -= 1
continue
if ix < len(lines) - 1:
nextl = lines[ix+1]
else:
nextl = None
if l == "<hr>" or l == "<hr/>":
continue
if l == """<h1 class="settitle" align="center">ANSI and GNU Common Lisp Document</h1>""":
continue
if l.startswith("<meta "):
continue
if l.startswith("<a name=\"") and l.endswith("</a>"):
continue
if l.startswith("<style"):
in_style = True
continue
if l.startswith("</style"):
in_style = False
continue
if in_style:
continue
if l.startswith("<link href="):
if "rel=\"up\"" in l:
n_up = re.match(".+href=\"([^\"]+?)(#[^\"]+)?\".+", l).group(1)
assert(n_up.endswith(".html"))
n_up_title = re.match(".+title=\"([^\"]+)\".+", l).group(1).replace("``", "\"").replace("''", "\"")
if n_up_title == "(dir)":
n_up = None
n_up_title = "-"
nodes[-1] = (nodes[-1][0], nodes[-1][1], n_up, nodes[-1][3])
if "rel=\"next\"" in l:
n_next = re.match(".+href=\"([^\"]+?)(#[^\"]+)?\".+", l).group(1)
assert(n_next.endswith(".html"))
n_next_title = re.match(".+title=\"([^\"]+)\".+", l).group(1).replace("``", "\"").replace("''", "\"")
if "rel=\"prev\"" in l:
n_prev = re.match(".+href=\"([^\"]+?)(#[^\"]+)?\".+", l).group(1)
assert(n_prev.endswith(".html"))
n_prev_title = re.match(".+title=\"([^\"]+)\".+", l).group(1).replace("``", "\"").replace("''", "\"")
if n_prev_title == "(dir)":
n_prev = None
n_prev_title = "-"
if l.startswith("<div class=\"header\""):
in_header = True
continue
if in_header and l.startswith("</div>"):
in_header = False
continue
if in_header:
continue
if l.startswith("<title>"):
l = l.replace(" (ANSI and GNU Common Lisp Document)", "")
title = re.match("<title>(.+)</title>", l).group(1)
l = l.replace("</title>", " (CLCS)</title>")
keywords = [t.strip() for t in title.split(",")]
for k in keywords:
if len(k.strip()) > 0:
nodes.append((k, f.replace(".html", ""), n_up, None, None))
# take the very first header as page header
if (l.startswith("<h3 ") or l.startswith("<h4 ") or l.startswith("<h2") or l.startswith("<h1")) and section_cnt == 0:
mnum = re.match(r".+>(\d+(\.\d+){0,6}) .+", l, flags= re.MULTILINE)
if mnum is not None:
numbering = mnum.group(1)
nodes[-1] = (nodes[-1][0], nodes[-1][1], nodes[-1][2], numbering, None)
l = re.sub(r">\d+(\.\d+){0,6} ", ">", l)
out.append("<div class=\"section top-most\">")
m = re.match(r".+\[(.+)\].*", l)
ntype = None
if m is not None:
ntype = m.group(1)
out.append(f"<div class=\"node-type\">{ntype}</div>")
l = re.sub(r"\[(.+)\]", "", l)
nodes[-1] = (nodes[-1][0], nodes[-1][1], nodes[-1][2], nodes[-1][3], ntype)
if "," in l:
tline = re.sub("<h[23456] class=\"(sub)?(sub)?section\">", "", l)
tline = re.sub("</h[23456]>", "", tline)
tline = re.sub("&(gt|GT);", ">", tline)
tline = re.sub("&(lt|LT);", "<", tline)
keywords = [t.strip() for t in tline.split(",")]
for k in keywords:
if k != title and len(k.strip()) > 0:
nodes.append((k, f.replace(".html", ""), n_up, None, ntype))
out.append(l)
in_section = True
section_cnt += 1
continue
elif l.startswith("<h4 ") or l.startswith("<h3 "):
if in_section:
if in_arguments:
out.append("</table>")
in_arguments = False
if in_syntax_table:
for r in range(1, 5):
if "</tr>" in out[-r]:
break
if "<td>" in out[-r]:
out.append("</td></tr>")
break
out.append("</table>")
in_syntax_table = False
out.append("</div>")
out.append("<div class=\"section\">")
l = re.sub("::<", "<", l)
out.append(l)
if re.match(".+(>Arguments|Compound Type Specifier Arguments).+", l):
in_arguments = True
out.append("""<table class="arguments-table">""")
else:
in_arguments = False
in_section = True
section_cnt += 1
continue
if l.startswith("</pre>"):
out.append("</code>")
if l.startswith("<pre class=\"example\">") or l.startswith("<pre>"):
l = re.sub("<pre( class=\"example\")?>", "<pre\\1><code>", l)
out.append(l)
continue
if "<tr><td align=\"left\" valign=\"top\">• " in l:
l = l.replace("• ", "")
l = l.replace("</a>:</td>", "</a></td>")
l = l.replace("``", "\"")
if "::=" in l and not "<td" in l and section_cnt == 1 and not in_syntax_table:
in_syntax_table = True
out.append("""<table class="syntax-table">""")
if in_syntax_table:
if "::=" in l:
for ib in range(10):
if "<td>" in out[-ib]:
out.append("</td></tr>")
break
l = re.sub("<!-- /@w -->", "", l)
l = re.sub("^<p>", "", l)
c0 = l.split("::=")[0]
c1 = re.sub(r"\s+$", "", l.split("::=")[1])
out.append(f"<tr><td>{c0}</td><td>::=</td><td>{c1}")
continue
elif re.match(r"^\s*</p>", l):
continue
elif "<!--end-syntax-table-->" in l:
in_syntax_table = False
out.append("</td></tr></table>")
continue
# table start
if re.match(r"(</p>)?<p>.+<!-- /@w -->", l) and not " Figure" in l and not in_syntax_table:
in_table = True
out.append("<table>")
l = re.sub("<p>", "", l)
l = re.sub("<!-- /@w -->", "", l)
l = re.sub("( ){2,}", "</td><td>", l)
l = f"<tr><td>{l}</td></tr>"
l = re.sub("<td> *</td>", "", l)
l = re.sub(r"<p>\s*</p>", "", l)
# table caption
if re.match(r"(</p>)?<p>( )+Figure.+", l):
l = re.sub("</?p>", "", l)
l = re.sub("<!-- /@w -->", "", l)
l = re.sub("( )+", " ", l)
l = f"<div class=\"table-subcaption\">{l}</div>"
out.append(l)
skip = 1
continue
if re.match(r"( )+Figure.+", l):
l = re.sub("<!-- /@w -->", "", l)
l = re.sub("( )+", " ", l)
l = f"<div class=\"table-subcaption\">{l}</div>"
out.append(l)
continue
if in_table and not in_syntax_table and re.match(r".+<!-- /@w -->", l):
l = re.sub("<!-- /@w -->", "", l)
l = re.sub("( ){2,}", "</td><td>", l)
l = f"<tr><td>{l}</td></tr>"
l = re.sub("<td> *</td>", "", l)
l = re.sub(r"<p>\s*</p>", "", l)
if l.startswith("</p>") and in_table and not in_syntax_table:
in_table = False
out.append("</table>")
continue
if in_arguments:
if "—" in l or "–" in l:
l = re.sub(r"^\s*<p>", "", l)
l = re.sub("&[mn]dash;", "</td><td>", l)
l = f"<tr><td>{l}"
out.append(l)
continue
elif re.match(r"^\s*</p>", l):
if ("—" in nextl
or "–" in nextl
or nextl.startswith("<a")
or nextl.startswith("</body")):
out.append("</td></tr>")
continue
elif re.match(r"^\s*<a .*", l):
continue
m = re.match("^(?:<p>)?‘([^;]+); [^&]+’$", l)
if m:
possible_ref = m.group(1)
possible_ref_esc = possible_ref.replace("-", "_002d")
if f"{possible_ref_esc}.html" in all_fnames:
if l.startswith("<p>"):
l = re.sub("‘(.+)’", f"""<a href="{possible_ref_esc}.html">\\1</a>""", l)
if l == ", " or l == ",":
out[-1] += ", "
continue
if l.startswith("<!--"):
continue
if l.startswith("</body>"):
if in_section:
if in_arguments or in_syntax_table:
for r in range(5):
if "</tr>" in out[-r]:
break
if "<td>" in out[-r]:
out.append("</td></tr>")
break
out.append("</table>")
out.append("</div>")
out.append("<div class=\"bl-placeholder\"></div>")
# close body main
out.append("</div>")
# close body inner
out.append("</div>")
# close body main inner
out.append("</div>")
out.append(l)
if l.startswith("<head"):
out.append("<meta charset=\"UTF-8\">")
out.append("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">")
out.append("<link rel=\"icon\" type=\"image/x-icon\" href=\"/favicon.ico\">")
out.append("<link rel=\"stylesheet\" href=\"/styles.css\">")
out.append("<link rel=\"stylesheet\" href=\"/highlight-lisp/themes/github.css\">")
out.append("""<script>
(() => {
let savedTheme = localStorage.getItem('clcs-theme');
let isDarkTheme = false;
if (savedTheme) {
isDarkTheme = savedTheme === 'dark';
} else {
isDarkTheme = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
}
if (isDarkTheme) {
window._theme = 'dark';
document.documentElement.setAttribute("data-theme", "dark");
} else {
window._theme = 'light';
}
})()
</script>""")
out.append("<script defer src=\"/scripts.js\"></script>")
if l.startswith("<body "):
prev_disabled = "disabled" if n_prev is None else ""
up_disabled = "disabled" if n_up is None else ""
next_disabled = "disabled" if n_next is None else ""
out.append(f"""
<div class="body__inner">
__nav-placeholder__
<div class="body__main">
<div class="body__main__inner">
<div class="top-wrapper">
<div class="top">
<div class="search">
<svg height="20" width="20" viewBox="0 0 512 512"><path d="M221.09 64a157.09 157.09 0 10157.09 157.09A157.1 157.1 0 00221.09 64z" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="32"/><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="32" d="M338.29 338.29L448 448"/></svg>
<input type="text" oninput="search(event)" onkeydown="searchKeydown(event)" placeholder="Search for pages">
<div id="search__drop" onblur="hideSearch()"></div>
</div>
<a class="index-btn" href="index.html">
<svg height="20" width="20" viewBox="0 0 512 512"><title>Index</title><path d="M80 212v236a16 16 0 0016 16h96V328a24 24 0 0124-24h80a24 24 0 0124 24v136h96a16 16 0 0016-16V212" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"/><path d="M480 256L266.89 52c-5-5.28-16.69-5.34-21.78 0L32 256M400 179V64h-48v69" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32"/></svg>
</a>
<div id="theme-btn--dark" onclick="toggleTheme()" title="Switch to light mode" class="index-btn"><svg viewBox="0 0 24 24" width="20" height="20"><path fill="none" d="M0 0h24v24H0z"/><path fill="currentColor" d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></svg></div>
<div id="theme-btn--light" onclick="toggleTheme()" title="Switch to dark mode" class="index-btn"><svg viewBox="0 0 24 24" width="20" height="20"><path fill="none" d="M0 0h24v24H0z"/><path fill="currentColor" d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></svg></div>
</div>
</div>
<div class="nav">
<a href="{n_prev}" class="nav-btn nav__prev {prev_disabled}">
<svg height="14" width="14" viewBox="0 0 512 512"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="M328 112L184 256l144 144"/></svg>
{n_prev_title or "-"}
</a>
<a href="{n_up}" class="nav-btn nav__up {up_disabled}">
<svg height="14" width="14" viewBox="0 0 512 512"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="M112 328l144-144 144 144"/></svg>
{n_up_title or "-"}
</a>
<a href="{n_next}" class="nav-btn nav__next {next_disabled}">
{n_next_title or "-"}
<svg height="14" width="14" viewBox="0 0 512 512"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="M184 112l144 144-144 144"/></svg>
</a>
</div>
""")
if l.startswith("</body>"):
# remember sidebar scroll position
out.append("""
<script>
(() => {
let sidebar = document.querySelector(".sidenav__main");
let t = localStorage.getItem("sidebar-scroll");
if (t !== null) {
sidebar.scrollTop = parseInt(t, 10);
}
window.addEventListener("beforeunload", () => {
localStorage.setItem("sidebar-scroll", sidebar.scrollTop);
});
})();
</script>""")
out.append(""" <script type="text/javascript" src="/highlight-lisp/highlight-lisp.js"></script>""")
text = "\n".join(out)
text = cleanup(text)
all_refs = re.finditer(r"<a href=\"([^\"]+).html(?:#[^\"]+)?\">([^<]+)</a>", text)
key = f.replace(".html", "")
for r in all_refs:
ref = r.group(1)
label = r.group(2)
assert(label is not None)
assert(len(label) > 0)
keys_to_label[ref] = label
if not ref in backlinks:
backlinks[ref] = []
if not key in backlinks[ref]:
backlinks[ref].append(key)
to_write.append((outpath, key, text))
n = nodes[-1]
nodes[-1] = (n[0], n[1], n_up, n[3], n[4])
parents = {}
for title, file, parent, numbering, ntype in nodes:
if numbering is None:
continue
if parent is None or parent == "Top" or parent == "index.html":
parent = ""
parent = parent.replace(".html", "")
if parent not in parents:
parents[parent] = []
parents[parent].append((title, file, numbering))
def cmp_nav_item(i1, i2):
num1 = i1[2]
num2 = i2[2]
comps1 = num1.split(".")
comps2 = num2.split(".")
for i in range(0, max(len(comps1), len(comps2))):
if i >= len(comps1):
return -1
if i >= len(comps2):
return 1
n1 = int(comps1[i])
n2 = int(comps2[i])
if n1 > n2:
return 1
if n2 > n1:
return -1
return 0
def print_nav_item(parents, children):
html = ""
for ctitle, cfile, numbering in sorted(children, key=functools.cmp_to_key(cmp_nav_item)):
cnode = ""
if cfile in parents:
cnode = print_nav_item(parents, parents[cfile])
cnode = f"<ul>{cnode}</ul>"
ctitle = ctitle.replace("``", "\"")
html += f"""<li><div><span>{numbering}</span><a href="{cfile}.html">{ctitle}</a></div>{cnode}</li>"""
return html
nav = print_nav_item(parents, parents[""])
nav = f"""<div class="sidenav">
<div class="sidenav__header"> CLCS
<div title="Scroll to current page" onclick="scrollSidenavToCurrentPage()" style="display:flex;justify-content:center;align-items:center;">
<svg viewBox="0 0 24 24" width="22" height="22"><path fill="none" d="M0 0h24v24H0z"/><path fill="currentColor" d="M13 1l.001 3.062A8.004 8.004 0 0 1 19.938 11H23v2l-3.062.001a8.004 8.004 0 0 1-6.937 6.937L13 23h-2v-3.062a8.004 8.004 0 0 1-6.938-6.937L1 13v-2h3.062A8.004 8.004 0 0 1 11 4.062V1h2zm-1 5a6 6 0 1 0 0 12 6 6 0 0 0 0-12zm0 4a2 2 0 1 1 0 4 2 2 0 0 1 0-4z"/></svg>
</div>
</div>
<div class="sidenav__main">
<ul>
<li><div><span>0</span><a href="index.html">Home</a></div></li>
{nav}
</ul>
</div>
<div class="sidenav__footer">
<a href="https://github.com/fonol/cl-community-spec">
src
</a>
<a href="https://github.com/fonol/cl-community-spec/issues/new">
report error
</a>
</div>
</div>"""
mismatched_tags = check_for_tag_mismatch(nav)
for t in mismatched_tags:
print(f"WARN: nav has mismatched tag: {t}")
for outpath, key, text in to_write:
if key in backlinks:
bl_list = []
for bl in backlinks[key]:
if bl == "index":
continue
lbl = keys_to_label.get(bl, bl)
lbl = lbl.replace("_002d", "-")
lbl = lbl.replace("_003e", ">")
lbl = lbl.replace("_0028", "(")
lbl = lbl.replace("_0029", ")")
lbl = lbl.replace("_0060_0060", "\"")
lbl = lbl.replace("_0027_0027", "\"")
lbl = lbl.replace("_0026", "&")
if lbl == "v_dash":
lbl = "/, //, ///"
elif lbl == "v_times":
lbl = "*, **, **"
elif lbl == "v_plus":
lbl = "+, ++, ++"
elif lbl == "f_mod":
lbl = "mod (Function)"
elif lbl == "f_abort":
lbl = "abort (Function)"
elif lbl == "f_list":
lbl = "list, list* (Function)"
elif lbl == "f_member":
lbl = "member, member-if, member-if-not (Function)"
elif lbl == "bit_sbit":
lbl = "bit, sbit"
elif lbl == "m_time":
lbl = "time (Macro)"
elif lbl == "rational_rationalize":
lbl = "rational, rationalize"
assert "_" not in lbl, lbl
bl_list.append(f"<a href=\"{bl}.html\">{lbl}</a>, ")
bl_list = "".join(sorted(bl_list, key = lambda b: b[b.index(">"):].lower()))
bl_list = bl_list[:-2]
bl_sec = f"""
<div class="section">
<h3>Backlinks</h3>
{bl_list}
</div>
""" if len(bl_list) > 0 else ""
text = text.replace("<div class=\"bl-placeholder\"></div>", bl_sec)
text = text.replace("__nav-placeholder__", nav)
mismatched_tags = check_for_tag_mismatch(text)
for t in mismatched_tags:
fd = outpath[outpath.rindex("/"):]
print(f"WARN: File {fd} has mismatched tag: {t}")
with open(outpath, "w", encoding="utf-8") as outf:
outf.write(text)
with open(os.path.join(folder_path(), "scripts.js"), "r", encoding="utf-8") as fscripts:
scripts = fscripts.read()
def clean_name(name):
return name.replace("'", "\\'").replace("``", "\"").replace("''", "\"")
nodelist = []
dup_names = set([clean_name(t) for t, count in collections.Counter([t[0] for t in nodes]).items() if count > 1])
dup_ntypes = {}
for t in nodes:
name = clean_name(t[0])
if name in dup_names and t[4] is not None:
file = t[1].replace("'", "\\'")
dup_ntypes[file] = t[4]
for t in nodes:
name = clean_name(t[0])
file = t[1].replace("'", "\\'")
if name in dup_names:
ntype = dup_ntypes[file]
assert(ntype is not None)
name = f"{name} ({ntype})"
nodelist.append((name, file))
nodelist = ",".join(sorted(set(["['{}','{}']".format(t[0], t[1]) for t in nodelist])))
scripts = re.sub(r"const NODES = \[.+\]; //N", f"const NODES = [{nodelist}]; //N", scripts)
with open(os.path.join(folder_path(), "scripts.js"), "w", encoding="utf-8") as fscripts:
fscripts.write(scripts)
searchable_terms = {}
for term, file, _, _, _ in nodes:
term = term.replace(">", ">")
term = term.replace("<", "<")
searchable_terms[term] = f"{file}.html"
with open(os.path.join(folder_path(), "searchable_terms.json"), "w") as out:
out.write(json.dumps(searchable_terms, indent=4))
print(f"Total: {len(nodes)} pages")
print("Finished conversion.\nResults can be found in /html_out.")
main()
# insert_table_links()
# insert_escaped_links()