7272ANSYS_LOGO_LINK = "https://www.ansys.com/"
7373PYANSYS_LOGO_LINK = "https://docs.pyansys.com/"
7474
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+ )
7778
7879# make logo paths available
7980ansys_favicon = str ((LOGOS_PATH / "ansys-favicon.png" ).absolute ())
@@ -481,7 +482,7 @@ def update_search_sidebar_context(
481482 context ["sidebars" ] = sidebar
482483
483484
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 :
485486 """Add a 'Home' entry to the root TOC.
486487
487488 Parameters
@@ -500,20 +501,23 @@ def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> N
500501 The 'Home' entry links to the index page of the documentation.
501502 """
502503 index_page = app .config .root_doc or app .config .master_doc or "index"
504+
505+ # Get the root TOC
503506 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 ):
505511 if not toc .attributes .get ("entries" ):
506- return
512+ continue
507513
514+ # Skip if "Home" already exists
508515 for title , page in toc .attributes ["entries" ]:
509- if title == "Home" :
516+ if title == "Home" and page in ( "self" , index_page ) :
510517 return
511518
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" )
517521 toc .attributes ["entries" ].insert (0 , home_entry )
518522
519523
@@ -547,11 +551,17 @@ def add_tooltip_after_build(app: Sphinx, exception):
547551 text = html_file .read_text (encoding = "utf-8" )
548552
549553 def replacer (match ):
550- attrs_before , attrs_after = match .groups ()
554+ attrs_before , href_link , attrs_after = match .groups ()
551555 full_attrs = f"{ attrs_before } { attrs_after } "
556+
557+ # don’t duplicate if title already exists
552558 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+ )
555565
556566 new_text = PACKAGE_HOME_HTML_PATTERN .sub (replacer , text )
557567
@@ -606,7 +616,7 @@ def setup(app: Sphinx) -> dict:
606616 app .connect ("html-page-context" , fix_edit_html_page_context )
607617 app .connect ("html-page-context" , update_search_sidebar_context )
608618 app .connect ("html-page-context" , update_template_context )
609- app .connect ("doctree-resolved" , on_doctree_resolved )
619+ app .connect ("doctree-resolved" , resolve_home_entry )
610620
611621 app .connect ("build-finished" , replace_html_tag )
612622 app .connect ("build-finished" , add_tooltip_after_build )
0 commit comments