Repository navigation
Expand file tree
/
Copy pathcopy-and-fix-book-html.py
More file actions
executable file
·189 lines (150 loc) · 5.73 KB
/
Copy pathcopy-and-fix-book-html.py
File metadata and controls
executable file
·189 lines (150 loc) · 5.73 KB
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
#!/usr/bin/env python
from dataclasses import dataclass
from pathlib import Path
import json
from lxml import html
import subprocess
BOOK_SOURCE = Path('../book')
DEST = Path('_site/book')
CHAPTERS = [
c.replace('.asciidoc', '.html')
for c in json.loads((BOOK_SOURCE / 'atlas.json').read_text())['files']
if c.partition('.')[0] not in [
'cover',
'titlepage',
'copyright',
'toc',
'ix',
'author_bio',
'colo',
]
]
def parse_chapters():
for chapter in CHAPTERS:
path = BOOK_SOURCE / chapter
yield chapter, html.fromstring(path.read_text())
def get_anchor_targets(parsed_html):
ignores = {'header', 'content', 'footnotes', 'footer', 'footer-text'}
all_ids = [
a.get('id') for a in parsed_html.cssselect('*[id]')
]
return [i for i in all_ids if not i.startswith('_') and i not in ignores]
@dataclass
class ChapterInfo:
href_id: str
chapter_title: str
old_title: str
subheaders: list
xrefs: list
def get_chapter_info():
chapter_info = {}
appendix_numbers = list('ABCDEFGHIJKL')
chapter_numbers = list(range(1, 100))
part_numbers = list(range(1, 10))
for chapter, parsed_html in parse_chapters():
print('getting info from', chapter)
if not parsed_html.cssselect('h2'):
header = parsed_html.cssselect('h1')[0]
else:
header = parsed_html.cssselect('h2')[0]
href_id = header.get('id')
if href_id is None:
href_id = parsed_html.cssselect('body')[0].get('id')
subheaders = [h.get('id') for h in parsed_html.cssselect('h3')]
old_title = header.text_content()
chapter_title = old_title.replace('Appendix A: ', '')
if chapter.startswith('chapter_'):
chapter_no = chapter_numbers.pop(0)
chapter_title = f'{chapter_no}: {chapter_title}'
if chapter.startswith('appendix_'):
appendix_no = appendix_numbers.pop(0)
chapter_title = f'Appendix {appendix_no}: {chapter_title}'
if chapter.startswith('part'):
part_no = part_numbers.pop(0)
chapter_title = f'Part {part_no}: {chapter_title}'
if chapter.startswith('epilogue'):
chapter_title = f'Epilogue: {chapter_title}'
xrefs = get_anchor_targets(parsed_html)
chapter_info[chapter] = ChapterInfo(
href_id, chapter_title, old_title, subheaders, xrefs
)
return chapter_info
def fix_xrefs(contents, chapter, chapter_info):
parsed = html.fromstring(contents)
links = parsed.cssselect('a[href^=\#]')
for link in links:
for other_chap in CHAPTERS:
if other_chap == chapter:
continue
chapter_id = chapter_info[other_chap].href_id
href = link.get('href')
targets = ['#' + x for x in chapter_info[other_chap].xrefs]
if href == '#' + chapter_id:
link.set('href', f'/book/{other_chap}')
elif href in targets:
link.set('href', f'/book/{other_chap}{href}')
return html.tostring(parsed)
def fix_title(contents, chapter, chapter_info):
parsed = html.fromstring(contents)
titles = parsed.cssselect('h2')
if titles and titles[0].text.startswith('Appendix A'):
title = titles[0]
title.text = chapter_info[chapter].chapter_title
return html.tostring(parsed)
def copy_chapters_across_with_fixes(chapter_info, fixed_toc):
comments_html = Path('fragments/disqus_comments.html').read_text()
buy_book_div = html.fromstring(
Path('fragments/buy_the_book_banner.html').read_text()
)
analytics_div = html.fromstring(
Path('fragments/google_analytics.html').read_text()
)
for chapter in CHAPTERS:
old_contents = (BOOK_SOURCE / chapter).read_text()
new_contents = fix_xrefs(old_contents, chapter, chapter_info)
new_contents = fix_title(new_contents, chapter, chapter_info)
parsed = html.fromstring(new_contents)
body = parsed.cssselect('body')[0]
if header := parsed.cssselect('#header'):
body.set('class', 'article toc2 toc-left')
header[0].append(fixed_toc)
body.insert(0, buy_book_div)
body.append(html.fromstring(
comments_html.replace('PAGE_IDENTIFIER', chapter.split('.')[0])
))
body.append(analytics_div)
fixed_contents = html.tostring(parsed)
target = DEST / chapter
print('writing', target)
target.write_bytes(fixed_contents)
def extract_toc_from_book():
parsed = html.fromstring((BOOK_SOURCE / 'book.html').read_text())
return parsed.cssselect('#toc')[0]
def fix_toc(toc, chapter_info):
href_mappings = {}
title_mappings = {}
for chapter in CHAPTERS:
chapinfo = chapter_info[chapter]
if chapinfo.href_id:
href_mappings['#' + chapinfo.href_id] = f'/book/{chapter}'
for subheader in chapinfo.subheaders:
href_mappings['#' + subheader] = f'/book/{chapter}#{subheader}'
title_mappings[chapinfo.old_title] = chapinfo.chapter_title
for (el, attr, link, pos) in toc.iterlinks():
print(el.text, attr, link, pos)
el.set('href', href_mappings[link])
if 'Appendix' in el.text:
old_title = el.text.partition(':')[2].strip()
new_title = title_mappings.get(old_title, el.text)
print('old', old_title)
el.text = new_title
print(el.text)
toc.set('class', 'toc2')
return toc
def main():
toc = extract_toc_from_book()
chapter_info = get_chapter_info()
fixed_toc = fix_toc(toc, chapter_info)
copy_chapters_across_with_fixes(chapter_info, fixed_toc)
if __name__ == '__main__':
main()