@@ -41,6 +41,7 @@ def get_anchor_targets(parsed_html):
4141class 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
151151def 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
178184if __name__ == '__main__' :
0 commit comments