72
72
ANSYS_LOGO_LINK = "https://www.ansys.com/"
73
73
PYANSYS_LOGO_LINK = "https://docs.pyansys.com/"
74
74
75
- PACKAGE_HOME_HTML_PATTERN = re .compile (r'<a([^>]*?)href="[^"]*index\.html"([^>]*?)>\s*Home\s*</a>' )
76
-
75
+ PACKAGE_HOME_HTML_PATTERN = re .compile (
76
+ r'<a([^>]*)href="([^"]*index\.html)"([^>]*)>\s*Home\s*</a>' , re .IGNORECASE
77
+ )
77
78
78
79
# make logo paths available
79
80
ansys_favicon = str ((LOGOS_PATH / "ansys-favicon.png" ).absolute ())
@@ -481,7 +482,7 @@ def update_search_sidebar_context(
481
482
context ["sidebars" ] = sidebar
482
483
483
484
484
- def on_doctree_resolved (app : Sphinx , doctree : nodes .document , docname : str ) -> None :
485
+ def resolve_home_entry (app : Sphinx , doctree : nodes .document , docname : str ) -> None :
485
486
"""Add a 'Home' entry to the root TOC.
486
487
487
488
Parameters
@@ -500,20 +501,23 @@ def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> N
500
501
The 'Home' entry links to the index page of the documentation.
501
502
"""
502
503
index_page = app .config .root_doc or app .config .master_doc or "index"
504
+
505
+ # Get the root TOC
503
506
root_toc = app .env .tocs [app .config .root_doc ]
504
- for toc in traverse_or_findall (root_toc , toctree ):
507
+ if not root_toc :
508
+ return
509
+
510
+ for toc in root_toc .findall (addnodes .toctree ):
505
511
if not toc .attributes .get ("entries" ):
506
- return
512
+ continue
507
513
514
+ # Skip if "Home" already exists
508
515
for title , page in toc .attributes ["entries" ]:
509
- if title == "Home" :
516
+ if title == "Home" and page in ( "self" , index_page ) :
510
517
return
511
518
512
- home_entry = (
513
- nodes .Text ("Home" ),
514
- index_page if index_page != docname else None ,
515
- )
516
- # Insert 'Home' entry at the beginning of the TOC
519
+ # Insert "Home <self>" entry at the beginning
520
+ home_entry = ("Home" , "self" )
517
521
toc .attributes ["entries" ].insert (0 , home_entry )
518
522
519
523
@@ -547,11 +551,17 @@ def add_tooltip_after_build(app: Sphinx, exception):
547
551
text = html_file .read_text (encoding = "utf-8" )
548
552
549
553
def replacer (match ):
550
- attrs_before , attrs_after = match .groups ()
554
+ attrs_before , href_link , attrs_after = match .groups ()
551
555
full_attrs = f"{ attrs_before } { attrs_after } "
556
+
557
+ # don’t duplicate if title already exists
552
558
if "title=" in full_attrs :
553
- return match .group (0 ) # don't duplicate title
554
- return f'<a{ attrs_before } href="index.html"{ attrs_after } title="{ project_name } ">\n Home\n </a>' # noqa: E501
559
+ return match .group (0 )
560
+
561
+ return (
562
+ f'<a{ attrs_before } href="{ href_link } "{ attrs_after } '
563
+ f'title="{ project_name } ">Home</a>'
564
+ )
555
565
556
566
new_text = PACKAGE_HOME_HTML_PATTERN .sub (replacer , text )
557
567
@@ -606,7 +616,7 @@ def setup(app: Sphinx) -> dict:
606
616
app .connect ("html-page-context" , fix_edit_html_page_context )
607
617
app .connect ("html-page-context" , update_search_sidebar_context )
608
618
app .connect ("html-page-context" , update_template_context )
609
- app .connect ("doctree-resolved" , on_doctree_resolved )
619
+ app .connect ("doctree-resolved" , resolve_home_entry )
610
620
611
621
app .connect ("build-finished" , replace_html_tag )
612
622
app .connect ("build-finished" , add_tooltip_after_build )
0 commit comments