|
@@ -0,0 +1,319 @@
|
|
1
|
+#! /usr/bin/env python
|
|
2
|
+
|
|
3
|
+import os
|
|
4
|
+from lxml.html import parse, etree, tostring
|
|
5
|
+
|
|
6
|
+copyright_string = """
|
|
7
|
+<div class="license">
|
|
8
|
+<a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/"
|
|
9
|
+target="_blank" class="m-2">
|
|
10
|
+<img alt="Creative Commons License" style="border-width:0"
|
|
11
|
+src="https://licensebuttons.net/l/by/4.0/80x15.png"/>
|
|
12
|
+</a>
|
|
13
|
+Except where otherwise noted, all content is licensed under a
|
|
14
|
+<a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/"
|
|
15
|
+target="_blank">Creative Commons Attribution 4.0 International License</a>.
|
|
16
|
+</div>
|
|
17
|
+"""
|
|
18
|
+
|
|
19
|
+BASEDIR = "./"
|
|
20
|
+SRCDIR = f"{BASEDIR}src/org/"
|
|
21
|
+
|
|
22
|
+tree = parse(f"{SRCDIR}index.html")
|
|
23
|
+
|
|
24
|
+el_head = None
|
|
25
|
+el_header = None
|
|
26
|
+el_title = None
|
|
27
|
+el_nav = None
|
|
28
|
+el_ttoc = None
|
|
29
|
+el_content = None
|
|
30
|
+el_postamble = None
|
|
31
|
+
|
|
32
|
+sections_2 = []
|
|
33
|
+
|
|
34
|
+for el in tree.iter():
|
|
35
|
+ if el.tag == 'head':
|
|
36
|
+ el_head = el
|
|
37
|
+ if el.tag == 'header':
|
|
38
|
+ el_header = el
|
|
39
|
+ if 'class' in el.attrib and el.attrib['class'] == 'title':
|
|
40
|
+ el_title = el
|
|
41
|
+ if el.tag == 'nav':
|
|
42
|
+ el_nav = el
|
|
43
|
+ if 'id' in el.attrib and el.attrib['id'] == 'text-table-of-contents':
|
|
44
|
+ el_ttoc = el
|
|
45
|
+ if 'id' in el.attrib and el.attrib['id'] == 'content':
|
|
46
|
+ el_content = el
|
|
47
|
+ if 'id' in el.attrib and el.attrib['id'] == 'postamble':
|
|
48
|
+ el_postamble = el
|
|
49
|
+ if 'class' in el.attrib and el.attrib['class'].startswith('outline-2'):
|
|
50
|
+ sections_2.append(el)
|
|
51
|
+
|
|
52
|
+export_dir = f"{BASEDIR}export/html"
|
|
53
|
+if not os.path.isdir(export_dir):
|
|
54
|
+ os.makedirs(export_dir)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+def ordered_pages(el):
|
|
58
|
+ """
|
|
59
|
+ Creates a list of all pages, able to return previous, next and up links.
|
|
60
|
+
|
|
61
|
+ `el` should be the text-table-of-contents element.
|
|
62
|
+ """
|
|
63
|
+ page_ids = []
|
|
64
|
+ page_texts = []
|
|
65
|
+ for child in el.iter():
|
|
66
|
+ if child.tag == 'a':
|
|
67
|
+ page_ids.append(child.attrib['href'].lstrip('#'))
|
|
68
|
+ page_texts.append(child.text)
|
|
69
|
+ return page_ids, page_texts
|
|
70
|
+
|
|
71
|
+page_ids, page_texts = ordered_pages(el_ttoc)
|
|
72
|
+
|
|
73
|
+def head_at_location(el_head, page_id):
|
|
74
|
+ output = tostring(el_head, pretty_print=True, encoding='unicode')
|
|
75
|
+ try:
|
|
76
|
+ index = page_ids.index(page_id)
|
|
77
|
+ except ValueError:
|
|
78
|
+ return output
|
|
79
|
+ if index == 0:
|
|
80
|
+ return output
|
|
81
|
+ return output.replace(
|
|
82
|
+ '</title>',
|
|
83
|
+ f': {page_texts[index]}</title>'
|
|
84
|
+ )
|
|
85
|
+
|
|
86
|
+def breadcrumbs(page_id):
|
|
87
|
+ try:
|
|
88
|
+ index = page_ids.index(page_id)
|
|
89
|
+ except ValueError:
|
|
90
|
+ return ''
|
|
91
|
+ if index == 0: # mains page, no need for breadcrumbs
|
|
92
|
+ return ''
|
|
93
|
+ breadcrumb_lis = f'<li>{page_texts[index]}</li>'
|
|
94
|
+ # If book or article, take shortcut
|
|
95
|
+ if page_id.startswith("b-"): # this is a book
|
|
96
|
+ page_id_stripped = "l_b"
|
|
97
|
+ elif page_id[:4].isdigit(): # this is an article
|
|
98
|
+ page_id_stripped = f"l_a_{page_id[:4]}"
|
|
99
|
+ else:
|
|
100
|
+ page_id_stripped = page_ids[index].rpartition('_')[0]
|
|
101
|
+ while page_id_stripped:
|
|
102
|
+ index_stripped = page_ids.index(page_id_stripped)
|
|
103
|
+ breadcrumb_lis = (
|
|
104
|
+ # give class to link to prevent auto addition of ids from cleanup_links script
|
|
105
|
+ '<li><a class="breadcrumb-link"' +
|
|
106
|
+ f'href="{page_ids[index_stripped]}.html">' +
|
|
107
|
+ f'{page_texts[index_stripped]}</a></li>'
|
|
108
|
+ + breadcrumb_lis)
|
|
109
|
+ page_id_stripped = page_id_stripped.rpartition('_')[0]
|
|
110
|
+ return f'<ul class="breadcrumbs">{breadcrumb_lis}</ul>'
|
|
111
|
+
|
|
112
|
+def link_previous(page_id):
|
|
113
|
+ try:
|
|
114
|
+ index = page_ids.index(page_id)
|
|
115
|
+ except ValueError:
|
|
116
|
+ return None
|
|
117
|
+ if index == 0: # first, no previous
|
|
118
|
+ return None
|
|
119
|
+ # If depth is 1, there is no previous
|
|
120
|
+ if page_id.count('_') == 0:
|
|
121
|
+ return None
|
|
122
|
+ else:
|
|
123
|
+ return f'<a href="{page_ids[index-1]}.html">{page_texts[index-1]} <small>[{page_ids[index-1].replace("_",".")}]</small></a>'
|
|
124
|
+
|
|
125
|
+def link_next(page_id):
|
|
126
|
+ try:
|
|
127
|
+ index = page_ids.index(page_id)
|
|
128
|
+ except ValueError:
|
|
129
|
+ return None
|
|
130
|
+ if index == len(page_ids) - 1: # last, no next
|
|
131
|
+ return None
|
|
132
|
+ return f'<a href="{page_ids[index+1]}.html">{page_texts[index+1]} <small>[{page_ids[index+1].replace("_",".")}]</small></a>'
|
|
133
|
+
|
|
134
|
+def link_up(page_id):
|
|
135
|
+ try:
|
|
136
|
+ index = page_ids.index(page_id)
|
|
137
|
+ except ValueError:
|
|
138
|
+ return None
|
|
139
|
+ # Strip one '_' from id and point there
|
|
140
|
+ if page_id.count('_') > 0:
|
|
141
|
+ index_up = page_ids.index(page_id.rpartition('_')[0])
|
|
142
|
+ return f'<a href="{page_ids[index_up]}.html">{page_texts[index_up]} <small>[{page_ids[index_up].replace("_",".")}]</small></a>'
|
|
143
|
+ return None
|
|
144
|
+
|
|
145
|
+def navigation_links(location):
|
|
146
|
+ links = '<ul class="navigation-links">'
|
|
147
|
+ previous = link_previous(location)
|
|
148
|
+ if previous:
|
|
149
|
+ links += f'<li>Prev: {previous}</li>'
|
|
150
|
+ nxt = link_next(location)
|
|
151
|
+ if nxt:
|
|
152
|
+ links += f'<li>Next: {nxt}</li>'
|
|
153
|
+ up = link_up(location)
|
|
154
|
+ if up:
|
|
155
|
+ links += f'<li>Up: {up}</li>'
|
|
156
|
+ links += '</ul>'
|
|
157
|
+ if previous or nxt or up:
|
|
158
|
+ return links
|
|
159
|
+ return ''
|
|
160
|
+
|
|
161
|
+def search_box():
|
|
162
|
+ return """
|
|
163
|
+ <form style="float: right; padding-right: 0;" method="get" id="search" action="https://duckduckgo.com/" target="_blank">
|
|
164
|
+ <input type="hidden" name="sites" value="integrability.org"/>
|
|
165
|
+ <input class="search" type="text" name="q" maxlength="300" placeholder="Search"/>
|
|
166
|
+ <input type="submit" value="Search" style="visibility: hidden; width: 0;" /></form>
|
|
167
|
+ """
|
|
168
|
+
|
|
169
|
+def list_to_details_recursive(el):
|
|
170
|
+ """
|
|
171
|
+ `el` contains either a single `a` child, or an `a` followed by `ul`.
|
|
172
|
+ In the first case, output as is.
|
|
173
|
+ In the second case, replace by details/summary.
|
|
174
|
+ """
|
|
175
|
+ # checks
|
|
176
|
+ if not (len(el) == 1 or len(el) == 2):
|
|
177
|
+ raise ValueError(f'el must have either 1 or 2 children, found {len(el)}')
|
|
178
|
+ if not el[0].tag in ['a', 'ul']:
|
|
179
|
+ raise ValueError(f'el[0] must be an a or ul tag, but found a {el[0].tag}')
|
|
180
|
+ if len(el) == 2 and not (el[0].tag == 'a' and el[1].tag == 'ul'):
|
|
181
|
+ raise ValueError(f'for len(el) == 2, el[1] must be ul, but found {el[1].tag}')
|
|
182
|
+ # single child, output as is
|
|
183
|
+ if len(el) == 1 and el[0].tag == 'a':
|
|
184
|
+ output = tostring(el[0], pretty_print=True, encoding='unicode')
|
|
185
|
+ else: # build a details/summary
|
|
186
|
+ summary_text = (
|
|
187
|
+ 'Table of contents' + search_box() if len(el) == 1 else
|
|
188
|
+ tostring(el[0], pretty_print=True, encoding='unicode')
|
|
189
|
+ )
|
|
190
|
+ ul = el[0] if len(el) == 1 else el[1]
|
|
191
|
+ # print(f'summary_text: {summary_text}')
|
|
192
|
+ # print(f'len(ul): {len(ul)}')
|
|
193
|
+ # print([li.tag for li in ul])
|
|
194
|
+ # print([tostring(li, pretty_print=True, encoding='unicode') for li in ul])
|
|
195
|
+ output = ('\n<details>'
|
|
196
|
+ f'\n<summary>\n{summary_text}'
|
|
197
|
+ '\n</summary>\n<ul>\n')
|
|
198
|
+ for li in ul:
|
|
199
|
+ if not li.tag == 'li':
|
|
200
|
+ raise ValueError('child of ul should be li')
|
|
201
|
+ output += '<li>\n'
|
|
202
|
+ output += list_to_details_recursive(li)
|
|
203
|
+ output += '\n</li>\n'
|
|
204
|
+ output += '\n</ul>\n</details>'
|
|
205
|
+ return output
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+collapsed_toc = '\n<nav id="collapsed-table-of-contents">'
|
|
210
|
+collapsed_toc += list_to_details_recursive(el_ttoc)
|
|
211
|
+collapsed_toc += '\n</nav>\n'
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+def toc_at_location(collapsed_toc, location):
|
|
215
|
+ """
|
|
216
|
+ Given a collapsed toc and location (filename),
|
|
217
|
+ mark the details hierarchy as open.
|
|
218
|
+ """
|
|
219
|
+ output = collapsed_toc
|
|
220
|
+ if location != 'index':
|
|
221
|
+ # open all ancestors
|
|
222
|
+ prefix = location.rpartition('_')[0]
|
|
223
|
+ while prefix:
|
|
224
|
+ output = output.replace(
|
|
225
|
+ f'<details>\n<summary>\n<a href="#{prefix}"',
|
|
226
|
+ f'<details open="">\n<summary class="toc-open">\n<a href="#{prefix}"')
|
|
227
|
+ prefix = prefix.rpartition('_')[0]
|
|
228
|
+ # highlight the current location, whether it's a summary or a
|
|
229
|
+ output = output.replace(
|
|
230
|
+ f'<details>\n<summary>\n<a href="#{location}"',
|
|
231
|
+ f'<details open="">\n<summary class="toc-currentpage">\n<a href="#{location}"'
|
|
232
|
+ )
|
|
233
|
+ output = output.replace(
|
|
234
|
+ f'<li>\n<a href="#{location}"',
|
|
235
|
+ f'<li class="toc-currentpage">\n<a href="#{location}"'
|
|
236
|
+ )
|
|
237
|
+ # but close all details which contain deeper levels than location
|
|
238
|
+ output = output.replace(
|
|
239
|
+ f'<details open="">\n<summary>\n<a href="#{location}_',
|
|
240
|
+ f'<details>\n<summary>\n<a href="#{location}_')
|
|
241
|
+ return output.replace('<a href', '<a class="toc-a" href')
|
|
242
|
+
|
|
243
|
+def write_file_start(_file, location):
|
|
244
|
+ _file.write('<!DOCTYPE html>\n<html lang="en">\n')
|
|
245
|
+ _file.write(head_at_location(el_head, location))
|
|
246
|
+ _file.write('<div id="content">\n')
|
|
247
|
+ #_file.write(tostring(el_header, pretty_print=True, encoding='unicode'))
|
|
248
|
+ _file.write('<header>\n<h1 class="title">\n'
|
|
249
|
+ '<a href="./index.html" class="homepage-link">')
|
|
250
|
+ _file.write(f'{el_title.text}</a>\n</h1>\n</header>')
|
|
251
|
+ _file.write(toc_at_location(collapsed_toc, location))
|
|
252
|
+ _file.write(breadcrumbs(location))
|
|
253
|
+ _file.write(navigation_links(location))
|
|
254
|
+
|
|
255
|
+def write_file_end(_file, location):
|
|
256
|
+ _file.write('\n<br>')
|
|
257
|
+ _file.write(navigation_links(location)) # repeat, for convenience
|
|
258
|
+ _file.write('\n<br>')
|
|
259
|
+ _file.write('\n<hr>')
|
|
260
|
+ _file.write(copyright_string)
|
|
261
|
+ _file.write(tostring(el_postamble, pretty_print=True, encoding='unicode'))
|
|
262
|
+ _file.write('\n</div>\n</html>')
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+def write_files_recursive(name, el, levelmax=5):
|
|
266
|
+ """
|
|
267
|
+ Recursively extract outlines.
|
|
268
|
+ """
|
|
269
|
+ if 'id' in el.attrib and el.attrib['id'] == 'content':
|
|
270
|
+ level = 1
|
|
271
|
+ elif not (el is not None and
|
|
272
|
+ 'class' in el.attrib and
|
|
273
|
+ el.attrib['class'].startswith('outline-')):
|
|
274
|
+ print(f'Element name {name} has no outline class, no files written.')
|
|
275
|
+ return
|
|
276
|
+ else:
|
|
277
|
+ level = int(el.attrib['class'].partition('outline-')[2][0])
|
|
278
|
+
|
|
279
|
+ _file = open(f'{BASEDIR}export/html/{name}.html', 'w')
|
|
280
|
+ write_file_start(_file, name)
|
|
281
|
+
|
|
282
|
+ # Count outline children for establishing output format
|
|
283
|
+ children = []
|
|
284
|
+ for child in el.iter():
|
|
285
|
+ if ('class' in child.attrib and
|
|
286
|
+ child.attrib['class'].startswith('outline-%s' % str(level + 1))):
|
|
287
|
+ children.append(child)
|
|
288
|
+
|
|
289
|
+ # if no children or if we're on levelmax, write everything
|
|
290
|
+ if (len(children) == 0 or level == levelmax):
|
|
291
|
+ _file.write(tostring(el, pretty_print=True, encoding='unicode'))
|
|
292
|
+ # otherwise write anything above first next-level headling,
|
|
293
|
+ # and then replace the next-level headlines by links to their files
|
|
294
|
+ else:
|
|
295
|
+ if (el.text):
|
|
296
|
+ _file.write(el.text)
|
|
297
|
+ for child in el:
|
|
298
|
+ if ('class' in child.attrib and
|
|
299
|
+ child.attrib['class'].startswith('outline-%s' % str(level + 1))):
|
|
300
|
+ break # break out once we hit the first next-level headline
|
|
301
|
+ if child.tag != "nav": # don't write the table-of-contents
|
|
302
|
+ _file.write(tostring(child, pretty_print=True, encoding='unicode'))
|
|
303
|
+ # now print the list of children
|
|
304
|
+ _file.write(f'<h{level+1}>In this section:</h{level+1}>')
|
|
305
|
+ _file.write('\n<ul class="child-links-list">')
|
|
306
|
+ for child in children:
|
|
307
|
+ child_h = next(child.iter('h%s' % int(level + 1)))
|
|
308
|
+ child_label = child_h.attrib['id']
|
|
309
|
+ child_text = next(child_h.iter('a')).text
|
|
310
|
+ _file.write(f'\n<li><a href="{child_label}.html">{child_text}</a></li>')
|
|
311
|
+ write_files_recursive(name=child_label, el=child)
|
|
312
|
+ _file.write('\n</ul>\n')
|
|
313
|
+
|
|
314
|
+ write_file_end(_file, name)
|
|
315
|
+ _file.close()
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+# Rewrite the index.html file:
|
|
319
|
+write_files_recursive('index', el_content) # the only call needed
|