Skip to content

Commit 0c497bd

Browse files
Revathyvenugopal162pyansys-ci-botjorgepilotoSMoraisAnsys
authored
fix: add home section (#769)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Jorge Martínez <[email protected]> Co-authored-by: Sébastien Morais <[email protected]>
1 parent 4cf2799 commit 0c497bd

File tree

3 files changed

+100
-13
lines changed

3 files changed

+100
-13
lines changed

doc/changelog.d/769.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add home section

pyproject.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ classifiers = [
3030
"Programming Language :: Python :: 3.13",
3131
]
3232
dependencies = [
33-
"Sphinx>=6.1.0",
34-
"pydata-sphinx-theme>=0.15.4,<0.17",
35-
"Jinja2>=3.1.2",
3633
"importlib-metadata>=4.0",
34+
"Jinja2>=3.1.2",
3735
"pdf2image>=1.17.0",
36+
"pydata-sphinx-theme>=0.15.4,<0.17",
3837
"PyYAML==6.0.2",
38+
"Sphinx>=6.1.0",
3939
]
4040

4141
[project.optional-dependencies]
4242
autoapi = [
43+
"astroid>=3.0,<4.0",
4344
"sphinx-autoapi==3.6.0",
4445
"sphinx-design==0.6.1",
4546
"sphinx-jinja==2.0.2",
46-
"astroid>=3.0,<4.0",
4747
]
4848
doc = [
4949
"jupytext==1.17.2",
50-
"notebook==7.4.5",
5150
"nbsphinx==0.9.7",
51+
"notebook==7.4.5",
5252
"numpydoc==1.9.0",
5353
"pandas==2.3.2",
5454
"Pillow>=9.0",
@@ -63,8 +63,8 @@ doc = [
6363
"sphinx-gallery==0.19.0",
6464
"sphinx-jinja==2.0.2",
6565
"sphinx-notfound-page==1.1.0",
66-
"tox==4.28.4",
6766
"sphinx-theme-builder[cli]==0.2.0b2",
67+
"tox==4.28.4",
6868
]
6969
changelog = [
7070
"PyYAML==6.0.2",

src/ansys_sphinx_theme/__init__.py

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
import os
2626
import pathlib
27-
from typing import Any, Dict
28-
import warnings
27+
import re
28+
from typing import Any
2929

3030
from docutils import nodes
31+
from pydata_sphinx_theme.toctree import traverse_or_findall
3132
from sphinx import addnodes
33+
from sphinx.addnodes import toctree
3234
from sphinx.application import Sphinx
3335
from sphinx.util import logging
3436

@@ -70,6 +72,9 @@
7072
ANSYS_LOGO_LINK = "https://www.ansys.com/"
7173
PYANSYS_LOGO_LINK = "https://docs.pyansys.com/"
7274

75+
PACKAGE_HOME_HTML_PATTERN = re.compile(r'<a([^>]*?)href="[^"]*index\.html"([^>]*?)>\s*Home\s*</a>')
76+
77+
7378
# make logo paths available
7479
ansys_favicon = str((LOGOS_PATH / "ansys-favicon.png").absolute())
7580
ansys_logo_black = str((LOGOS_PATH / "ansys_logo_black_cropped.jpg").absolute())
@@ -290,7 +295,7 @@ def fix_edit_link_page(link: str) -> str:
290295

291296

292297
def update_footer_theme(
293-
app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: nodes.document
298+
app: Sphinx, pagename: str, templatename: str, context: dict[str, Any], doctree: nodes.document
294299
) -> None:
295300
"""Update the version number of the Ansys Sphinx theme in the footer.
296301
@@ -380,10 +385,12 @@ def configure_theme_logo(app: Sphinx):
380385

381386
if logo_option == "ansys":
382387
theme_options["logo"] = ansys_logo
383-
theme_options["logo_link"] = theme_options.get("logo_link", ANSYS_LOGO_LINK)
388+
# Ansys logo should link to the ANSYS homepage
389+
theme_options["logo_link"] = ANSYS_LOGO_LINK
384390
elif logo_option == "pyansys":
385391
theme_options["logo"] = pyansys_logo
386-
theme_options["logo_link"] = theme_options.get("logo_link", PYANSYS_LOGO_LINK)
392+
# PyAnsys logo should link to the PyAnsys Meta documentation
393+
theme_options["logo_link"] = PYANSYS_LOGO_LINK
387394
elif logo_option == "no_logo":
388395
theme_options["logo"] = None
389396

@@ -474,7 +481,85 @@ def update_search_sidebar_context(
474481
context["sidebars"] = sidebar
475482

476483

477-
def setup(app: Sphinx) -> Dict:
484+
def on_doctree_resolved(app: Sphinx, doctree: nodes.document, docname: str) -> None:
485+
"""Add a 'Home' entry to the root TOC.
486+
487+
Parameters
488+
----------
489+
app : Sphinx
490+
Sphinx application instance for rendering the documentation.
491+
doctree : nodes.document
492+
Document tree for the page.
493+
docname : str
494+
Name of the current document.
495+
496+
Notes
497+
-----
498+
This function checks if the 'Home' entry already exists in the root TOC.
499+
If it does not exist, it adds the 'Home' entry at the beginning of the TOC.
500+
The 'Home' entry links to the index page of the documentation.
501+
"""
502+
index_page = app.config.root_doc or app.config.master_doc or "index"
503+
root_toc = app.env.tocs[app.config.root_doc]
504+
for toc in traverse_or_findall(root_toc, toctree):
505+
if not toc.attributes.get("entries"):
506+
return
507+
508+
for title, page in toc.attributes["entries"]:
509+
if title == "Home":
510+
return
511+
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
517+
toc.attributes["entries"].insert(0, home_entry)
518+
519+
520+
def add_tooltip_after_build(app: Sphinx, exception):
521+
"""Add tooltips to 'Home' links after the build process.
522+
523+
Parameters
524+
----------
525+
app : Sphinx
526+
Sphinx application instance for rendering the documentation.
527+
exception : Exception
528+
Exception raised during the build process.
529+
530+
Returns
531+
-------
532+
None
533+
"""
534+
if exception:
535+
return
536+
537+
outdir = pathlib.Path(app.outdir)
538+
539+
project_name = "Package Home"
540+
541+
if app.config.html_short_title:
542+
project_name = f"{app.config.html_short_title} home"
543+
elif app.config.project:
544+
project_name = f"{app.config.project} home"
545+
546+
for html_file in outdir.rglob("*.html"):
547+
text = html_file.read_text(encoding="utf-8")
548+
549+
def replacer(match):
550+
attrs_before, attrs_after = match.groups()
551+
full_attrs = f"{attrs_before}{attrs_after}"
552+
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
555+
556+
new_text = PACKAGE_HOME_HTML_PATTERN.sub(replacer, text)
557+
558+
if new_text != text:
559+
html_file.write_text(new_text, encoding="utf-8")
560+
561+
562+
def setup(app: Sphinx) -> dict:
478563
"""Connect to the Sphinx theme app.
479564
480565
Parameters
@@ -516,14 +601,15 @@ def setup(app: Sphinx) -> Dict:
516601
if whatsnew_file and changelog_file:
517602
app.connect("doctree-read", add_whatsnew_changelog)
518603
app.connect("doctree-resolved", extract_whatsnew)
519-
520604
app.connect("html-page-context", add_sidebar_context)
521605
app.connect("html-page-context", update_footer_theme)
522606
app.connect("html-page-context", fix_edit_html_page_context)
523607
app.connect("html-page-context", update_search_sidebar_context)
524608
app.connect("html-page-context", update_template_context)
609+
app.connect("doctree-resolved", on_doctree_resolved)
525610

526611
app.connect("build-finished", replace_html_tag)
612+
app.connect("build-finished", add_tooltip_after_build)
527613
if use_ansys_search:
528614
app.connect("build-finished", create_search_index)
529615

0 commit comments

Comments
 (0)