|
24 | 24 |
|
25 | 25 | import os
|
26 | 26 | import pathlib
|
27 |
| -from typing import Any, Dict |
28 |
| -import warnings |
| 27 | +import re |
| 28 | +from typing import Any |
29 | 29 |
|
30 | 30 | from docutils import nodes
|
| 31 | +from pydata_sphinx_theme.toctree import traverse_or_findall |
31 | 32 | from sphinx import addnodes
|
| 33 | +from sphinx.addnodes import toctree |
32 | 34 | from sphinx.application import Sphinx
|
33 | 35 | from sphinx.util import logging
|
34 | 36 |
|
|
70 | 72 | ANSYS_LOGO_LINK = "https://www.ansys.com/"
|
71 | 73 | PYANSYS_LOGO_LINK = "https://docs.pyansys.com/"
|
72 | 74 |
|
| 75 | +PACKAGE_HOME_HTML_PATTERN = re.compile(r'<a([^>]*?)href="[^"]*index\.html"([^>]*?)>\s*Home\s*</a>') |
| 76 | + |
| 77 | + |
73 | 78 | # make logo paths available
|
74 | 79 | ansys_favicon = str((LOGOS_PATH / "ansys-favicon.png").absolute())
|
75 | 80 | ansys_logo_black = str((LOGOS_PATH / "ansys_logo_black_cropped.jpg").absolute())
|
@@ -290,7 +295,7 @@ def fix_edit_link_page(link: str) -> str:
|
290 | 295 |
|
291 | 296 |
|
292 | 297 | 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 |
294 | 299 | ) -> None:
|
295 | 300 | """Update the version number of the Ansys Sphinx theme in the footer.
|
296 | 301 |
|
@@ -380,10 +385,12 @@ def configure_theme_logo(app: Sphinx):
|
380 | 385 |
|
381 | 386 | if logo_option == "ansys":
|
382 | 387 | 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 |
384 | 390 | elif logo_option == "pyansys":
|
385 | 391 | 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 |
387 | 394 | elif logo_option == "no_logo":
|
388 | 395 | theme_options["logo"] = None
|
389 | 396 |
|
@@ -474,7 +481,85 @@ def update_search_sidebar_context(
|
474 | 481 | context["sidebars"] = sidebar
|
475 | 482 |
|
476 | 483 |
|
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: |
478 | 563 | """Connect to the Sphinx theme app.
|
479 | 564 |
|
480 | 565 | Parameters
|
@@ -516,14 +601,15 @@ def setup(app: Sphinx) -> Dict:
|
516 | 601 | if whatsnew_file and changelog_file:
|
517 | 602 | app.connect("doctree-read", add_whatsnew_changelog)
|
518 | 603 | app.connect("doctree-resolved", extract_whatsnew)
|
519 |
| - |
520 | 604 | app.connect("html-page-context", add_sidebar_context)
|
521 | 605 | app.connect("html-page-context", update_footer_theme)
|
522 | 606 | app.connect("html-page-context", fix_edit_html_page_context)
|
523 | 607 | app.connect("html-page-context", update_search_sidebar_context)
|
524 | 608 | app.connect("html-page-context", update_template_context)
|
| 609 | + app.connect("doctree-resolved", on_doctree_resolved) |
525 | 610 |
|
526 | 611 | app.connect("build-finished", replace_html_tag)
|
| 612 | + app.connect("build-finished", add_tooltip_after_build) |
527 | 613 | if use_ansys_search:
|
528 | 614 | app.connect("build-finished", create_search_index)
|
529 | 615 |
|
|
0 commit comments