Skip to content

Commit e879b41

Browse files
committed
book prepper mostly working, toc fixed
1 parent b59bb68 commit e879b41

4 files changed

Lines changed: 32 additions & 26 deletions

File tree

_site/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ <h2>Simple patterns for building complex applications</h2>
4343
<h1>The Book</h1>
4444
<ul>
4545
<li>
46-
Read for free from the sources using Github previews:
47-
<a href="https://github.com/cosmicpython/book#table-of-contents">github.com/cosmicpython/book</a>
46+
Read for free
47+
<a href="book/preface.html">on this site</a> (license: CC-By-NC-ND).
4848
</li>
4949

5050
<li>
51-
Read in online on O'Reilly Learning (aka Safari)
51+
Read in online on O'Reilly Learning (aka Safari)
5252
<a href="https://learning.oreilly.com/library/view/architecture-patterns-with/9781492052197/">learning.oreilly.com</a>
5353
</li>
5454

_site/rss.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Simple patterns for building complex apps
88
</description>
99
<link>https://cosmicpython.com</link>
10-
<lastBuildDate>Mon, 16 Mar 2020 10:44:20 -0000</lastBuildDate>
10+
<lastBuildDate>Mon, 16 Mar 2020 13:15:11 -0000</lastBuildDate>
1111
<pubDate>Sat, 4 Jan 2020 19:15:54 -0500</pubDate>
1212
<atom:link href="https://cosmicpython.com/rss.xml" rel="self" type="application/rss+xml" />
1313

pages/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ <h2>Simple patterns for building complex applications</h2>
2020
<h1>The Book</h1>
2121
<ul>
2222
<li>
23-
Read for free from the sources using Github previews:
24-
<a href="https://github.com/cosmicpython/book#table-of-contents">github.com/cosmicpython/book</a>
23+
Read for free
24+
<a href="book/preface.html">on this site</a> (license: CC-By-NC-ND).
2525
</li>
2626

2727
<li>
28-
Read in online on O'Reilly Learning (aka Safari)
28+
Read in online on O'Reilly Learning (aka Safari)
2929
<a href="https://learning.oreilly.com/library/view/architecture-patterns-with/9781492052197/">learning.oreilly.com</a>
3030
</li>
3131

prep-book-html.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def get_anchor_targets(parsed_html):
4141
class ChapterInfo:
4242
href_id: str
4343
chapter_title: str
44+
old_title: str
4445
subheaders: list
4546
xrefs: list
4647

@@ -63,12 +64,12 @@ def get_chapter_info():
6364
href_id = parsed_html.cssselect('body')[0].get('id')
6465
subheaders = [h.get('id') for h in parsed_html.cssselect('h3')]
6566

66-
chapter_title = header.text_content()
67-
chapter_title = chapter_title.replace('Appendix A: ', '')
67+
old_title = header.text_content()
68+
chapter_title = old_title.replace('Appendix A: ', '')
6869

6970
if chapter.startswith('chapter_'):
7071
chapter_no = chapter_numbers.pop(0)
71-
chapter_title = f'Chapter {chapter_no}: {chapter_title}'
72+
chapter_title = f'{chapter_no}: {chapter_title}'
7273

7374
if chapter.startswith('appendix_'):
7475
appendix_no = appendix_numbers.pop(0)
@@ -82,7 +83,9 @@ def get_chapter_info():
8283
chapter_title = f'Epilogue: {chapter_title}'
8384

8485
xrefs = get_anchor_targets(parsed_html)
85-
chapter_info[chapter] = ChapterInfo(href_id, chapter_title, subheaders, xrefs)
86+
chapter_info[chapter] = ChapterInfo(
87+
href_id, chapter_title, old_title, subheaders, xrefs
88+
)
8689

8790
return chapter_info
8891

@@ -117,7 +120,6 @@ def copy_chapters_across_with_fixes(chapter_info, fixed_toc):
117120
# comments_html = open('disqus_comments.html').read()
118121
# buy_book_div = html.fromstring(open('buy_the_book_banner.html').read())
119122
# analytics_div = html.fromstring(open('analytics.html').read())
120-
# load_toc_script = open('load_toc.js').read()
121123

122124
for chapter in CHAPTERS:
123125
old_contents = Path(f'book/{chapter}').read_text()
@@ -126,8 +128,6 @@ def copy_chapters_across_with_fixes(chapter_info, fixed_toc):
126128
parsed = html.fromstring(new_contents)
127129
body = parsed.cssselect('body')[0]
128130
if header := parsed.cssselect('#header'):
129-
# head = parsed.cssselect('head')[0]
130-
# head.append(html.fragment_fromstring('<script>' + load_toc_script + '</script>'))
131131
body.set('class', 'article toc2 toc-left')
132132
header[0].append(fixed_toc)
133133
# body.insert(0, buy_book_div)
@@ -150,19 +150,26 @@ def extract_toc_from_book():
150150

151151
def fix_toc(toc, chapter_info):
152152
href_mappings = {}
153+
title_mappings = {}
153154
for chapter in CHAPTERS:
154-
chap = chapter_info[chapter]
155-
if chap.href_id:
156-
href_mappings['#' + chap.href_id] = f'/book/{chapter}'
157-
for subheader in chap.subheaders:
155+
chapinfo = chapter_info[chapter]
156+
if chapinfo.href_id:
157+
href_mappings['#' + chapinfo.href_id] = f'/book/{chapter}'
158+
for subheader in chapinfo.subheaders:
158159
href_mappings['#' + subheader] = f'/book/{chapter}#{subheader}'
159-
160-
def fix_link(href):
161-
if href in href_mappings:
162-
return href_mappings[href]
163-
return href
164-
165-
toc.rewrite_links(fix_link)
160+
title_mappings[chapinfo.old_title] = chapinfo.chapter_title
161+
162+
for (el, attr, link, pos) in toc.iterlinks():
163+
print(el.text, attr, link, pos)
164+
el.set('href', href_mappings[link])
165+
if 'Appendix' in el.text:
166+
old_title = el.text.partition(':')[2].strip()
167+
new_title = title_mappings.get(old_title, el.text)
168+
print('old', old_title)
169+
el.text = new_title
170+
print(el.text)
171+
172+
# toc.rewrite_links(lambda href: href_mappings.get(href, href))
166173
toc.set('class', 'toc2')
167174
return toc
168175

@@ -172,7 +179,6 @@ def main():
172179
chapter_info = get_chapter_info()
173180
fixed_toc = fix_toc(toc, chapter_info)
174181
copy_chapters_across_with_fixes(chapter_info, fixed_toc)
175-
rsync_images()
176182

177183

178184
if __name__ == '__main__':

0 commit comments

Comments
 (0)