Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/795.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Home link reference
40 changes: 25 additions & 15 deletions src/ansys_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<a([^>]*?)href="[^"]*index\.html"([^>]*?)>\s*Home\s*</a>')

PACKAGE_HOME_HTML_PATTERN = re.compile(
r'<a([^>]*)href="([^"]*index\.html)"([^>]*)>\s*Home\s*</a>', re.IGNORECASE
)

# make logo paths available
ansys_favicon = str((LOGOS_PATH / "ansys-favicon.png").absolute())
Expand Down Expand Up @@ -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
Expand All @@ -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 <self>" entry at the beginning
home_entry = ("Home", "self")
toc.attributes["entries"].insert(0, home_entry)


Expand Down Expand Up @@ -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'<a{attrs_before}href="index.html"{attrs_after} title="{project_name}">\n Home\n</a>' # noqa: E501
return match.group(0)

return (
f'<a{attrs_before}href="{href_link}"{attrs_after} '
f'title="{project_name}">Home</a>'
)

new_text = PACKAGE_HOME_HTML_PATTERN.sub(replacer, text)

Expand Down Expand Up @@ -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)
Expand Down
Loading