|
1 | 1 | """This is the ansys-sphinx-theme module.""" |
2 | | -import os |
3 | | -from pathlib import Path |
| 2 | +import pathlib |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import sphinx |
4 | 6 |
|
5 | 7 | from ansys_sphinx_theme.latex import generate_404 # noqa: F401 |
6 | 8 |
|
7 | 9 | __version__ = "0.9.dev0" |
8 | 10 |
|
9 | | -# get location of this directory |
10 | | -_this_path = os.path.dirname(os.path.realpath(__file__)) |
| 11 | + |
| 12 | +# Declare the fundamental paths of the theme |
| 13 | +THIS_PATH = pathlib.Path(__file__).parent.resolve() |
| 14 | +THEME_PATH = THIS_PATH / "theme" / "ansys_sphinx_theme" |
| 15 | +STATIC_PATH = THEME_PATH / "static" |
| 16 | +STYLE_PATH = STATIC_PATH / "css" |
| 17 | +CSS_PATH = STYLE_PATH / "ansys_sphinx_theme.css" |
| 18 | +TEMPLATES_PATH = THEME_PATH / "_templates" |
11 | 19 |
|
12 | 20 | # make logo paths available |
13 | | -pyansys_logo_black = os.path.join(_this_path, "static", "pyansys-logo-black-cropped.png") |
14 | | -pyansys_logo_white = os.path.join(_this_path, "static", "pyansys-logo-white-cropped.png") |
15 | | -ansys_favicon = os.path.join(_this_path, "static", "ansys-favicon.png") |
16 | | -ansys_logo_white = os.path.join(_this_path, "static", "ansys_logo_white.pdf") |
17 | | -ansys_logo_white_cropped = os.path.join(_this_path, "static", "ansys_logo_white_cropped.pdf") |
18 | | -watermark = os.path.join(_this_path, "static", "watermark.pdf") |
19 | | -ansys_logo_black = os.path.join(_this_path, "static", "ansys_logo_black_cropped.jpg") |
| 21 | +ansys_favicon = str((STATIC_PATH / "ansys-favicon.png").absolute()) |
| 22 | +ansys_logo_black = str((STATIC_PATH / "ansys_logo_black_cropped.jpg").absolute()) |
| 23 | +ansys_logo_white = str((STATIC_PATH / "ansys_logo_white.pdf").absolute()) |
| 24 | +ansys_logo_white_cropped = str((STATIC_PATH / "ansys_logo_white_cropped.pdf").absolute()) |
| 25 | +page_404 = str((STATIC_PATH / "404.rst").absolute()) |
| 26 | +pyansys_logo_black = str((STATIC_PATH / "pyansys-logo-black-cropped.png").absolute()) |
| 27 | +pyansys_logo_white = str((STATIC_PATH / "pyansys-logo-white-cropped.png").absolute()) |
| 28 | +watermark = str((STATIC_PATH / "watermark.pdf").absolute()) |
| 29 | + |
| 30 | + |
| 31 | +def get_html_theme_path() -> pathlib.Path: |
| 32 | + """Return list of HTML theme paths. |
20 | 33 |
|
21 | | -# Enable default 404 page |
22 | | -page_404 = os.path.join(_this_path, "static", "404.rst") |
| 34 | + Returns |
| 35 | + ------- |
| 36 | + pathlib.Path |
| 37 | + Path pointing to the installation directory of the theme. |
23 | 38 |
|
24 | | -html_logo = pyansys_logo_black |
25 | | -CSS_FILENAME = "ansys_sphinx_theme.css" |
| 39 | + """ |
| 40 | + return THEME_PATH.resolve() |
26 | 41 |
|
27 | 42 |
|
28 | | -def get_html_theme_path(): |
29 | | - """Return list of HTML theme paths.""" |
30 | | - return Path(__file__).parents[0].absolute() |
| 43 | +def get_version_match(semver: str) -> str: |
| 44 | + """Evaluate the version match for the multi-documentation. |
31 | 45 |
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + semver : str |
| 49 | + Semantic version number in the form of a string. |
32 | 50 |
|
33 | | -def get_version_match(semver): |
34 | | - """Evaluate the version match for the multi-documentation.""" |
| 51 | + Returns |
| 52 | + ------- |
| 53 | + str |
| 54 | + Matching version number in the form of a string. |
| 55 | +
|
| 56 | + """ |
35 | 57 | if semver.endswith("dev0"): |
36 | 58 | return "dev" |
37 | 59 | major, minor, _ = semver.split(".") |
38 | 60 | return ".".join([major, minor]) |
39 | 61 |
|
40 | 62 |
|
41 | | -def setup(app): |
42 | | - """Connect to the sphinx theme app.""" |
| 63 | +def setup_default_html_theme_options(app): |
| 64 | + """Set up the default configuration for the HTML options. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + app : sphinx.application.Sphinx |
| 69 | + Application instance for rendering the documentation. |
| 70 | +
|
| 71 | + Notes |
| 72 | + ----- |
| 73 | + This function is the only way to overwrite ``pydata-sphinx-theme`` |
| 74 | + configuration. Variables declared in the ``theme.conf`` do not include |
| 75 | + inherited ones. |
| 76 | +
|
| 77 | + """ |
| 78 | + # Place all switchers and icons at the end of the navigation bar |
| 79 | + app.config.html_theme_options.setdefault( |
| 80 | + "navbar_end", ["version-switcher", "theme-switcher", "navbar-icon-links"] |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def setup(app: sphinx.application.Sphinx) -> Dict: |
| 85 | + """Connect to the sphinx theme app. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + app : sphinx.application.Sphinx |
| 90 | + Application instance for rendering the documentation. |
| 91 | +
|
| 92 | + Returns |
| 93 | + ------- |
| 94 | + Dict |
| 95 | + Dictionary containing application status. |
| 96 | +
|
| 97 | + """ |
| 98 | + # Add the theme configuration |
43 | 99 | theme_path = get_html_theme_path() |
44 | 100 | app.add_html_theme("ansys_sphinx_theme", theme_path) |
45 | | - theme_css_path = theme_path / "static" / "css" / CSS_FILENAME |
46 | | - if not theme_css_path.exists(): |
47 | | - raise FileNotFoundError(f"Unable to locate ansys-sphinx theme at {theme_css_path}") |
48 | | - app.add_css_file(str(theme_css_path.relative_to(theme_path / "static"))) |
49 | | - |
50 | | - # add templates for autosummary |
51 | | - path_templates = os.path.join(_this_path, "_templates") |
52 | | - app.config.templates_path.append(path_templates) |
| 101 | + app.config.templates_path.append(str(THEME_PATH / "components")) |
| 102 | + |
| 103 | + # Add default HTML configuration |
| 104 | + setup_default_html_theme_options(app) |
| 105 | + |
| 106 | + # Verify that the main CSS file exists |
| 107 | + if not CSS_PATH.exists(): |
| 108 | + raise FileNotFoundError(f"Unable to locate ansys-sphinx theme at {CSS_PATH.absolute()}") |
| 109 | + app.add_css_file(str(CSS_PATH.relative_to(STATIC_PATH))) |
| 110 | + |
| 111 | + # Add templates for autosummary |
| 112 | + app.config.templates_path.append(str(TEMPLATES_PATH)) |
53 | 113 |
|
54 | 114 | return { |
55 | 115 | "version": __version__, |
|
0 commit comments