diff --git a/doc/changelog.d/795.fixed.md b/doc/changelog.d/795.fixed.md new file mode 100644 index 000000000..abdb6ed7f --- /dev/null +++ b/doc/changelog.d/795.fixed.md @@ -0,0 +1 @@ +Home link reference diff --git a/src/ansys_sphinx_theme/__init__.py b/src/ansys_sphinx_theme/__init__.py index be02f4eac..f317040eb 100644 --- a/src/ansys_sphinx_theme/__init__.py +++ b/src/ansys_sphinx_theme/__init__.py @@ -72,8 +72,9 @@ ANSYS_LOGO_LINK = "https://www.ansys.com/" PYANSYS_LOGO_LINK = "https://docs.pyansys.com/" -PACKAGE_HOME_HTML_PATTERN = re.compile(r']*?)href="[^"]*index\.html"([^>]*?)>\s*Home\s*') - +PACKAGE_HOME_HTML_PATTERN = re.compile( + r']*)href="([^"]*index\.html)"([^>]*)>\s*Home\s*', re.IGNORECASE +) # make logo paths available ansys_favicon = str((LOGOS_PATH / "ansys-favicon.png").absolute()) @@ -481,7 +482,7 @@ def update_search_sidebar_context( context["sidebars"] = sidebar -def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> None: +def resolve_home_entry(app: Sphinx, doctree: nodes.document, docname: str) -> None: """Add a 'Home' entry to the root TOC. Parameters @@ -500,20 +501,23 @@ def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> N The 'Home' entry links to the index page of the documentation. """ index_page = app.config.root_doc or app.config.master_doc or "index" + + # Get the root TOC root_toc = app.env.tocs[app.config.root_doc] - for toc in traverse_or_findall(root_toc, toctree): + if not root_toc: + return + + for toc in root_toc.findall(addnodes.toctree): if not toc.attributes.get("entries"): - return + continue + # Skip if "Home" already exists for title, page in toc.attributes["entries"]: - if title == "Home": + if title == "Home" and page in ("self", index_page): return - home_entry = ( - nodes.Text("Home"), - index_page if index_page != docname else None, - ) - # Insert 'Home' entry at the beginning of the TOC + # Insert "Home " entry at the beginning + home_entry = ("Home", "self") toc.attributes["entries"].insert(0, home_entry) @@ -547,11 +551,17 @@ def add_tooltip_after_build(app: Sphinx, exception): text = html_file.read_text(encoding="utf-8") def replacer(match): - attrs_before, attrs_after = match.groups() + attrs_before, href_link, attrs_after = match.groups() full_attrs = f"{attrs_before}{attrs_after}" + + # don’t duplicate if title already exists if "title=" in full_attrs: - return match.group(0) # don't duplicate title - return f'\n Home\n' # noqa: E501 + return match.group(0) + + return ( + f'Home' + ) new_text = PACKAGE_HOME_HTML_PATTERN.sub(replacer, text) @@ -606,7 +616,7 @@ def setup(app: Sphinx) -> dict: app.connect("html-page-context", fix_edit_html_page_context) app.connect("html-page-context", update_search_sidebar_context) app.connect("html-page-context", update_template_context) - app.connect("doctree-resolved", on_doctree_resolved) + app.connect("doctree-resolved", resolve_home_entry) app.connect("build-finished", replace_html_tag) app.connect("build-finished", add_tooltip_after_build)