|
| 1 | +"""Module for adding the autoapi extension with the theme.""" |
| 2 | + |
| 3 | +# Copyright (C) 2021 - 2024 ANSYS, Inc. and/or its affiliates. |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +# |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +import os |
| 26 | +from typing import Any, Dict |
| 27 | + |
| 28 | +from sphinx.application import Sphinx |
| 29 | + |
| 30 | +from ansys_sphinx_theme import __version__, get_autoapi_templates_dir_relative_path |
| 31 | + |
| 32 | + |
| 33 | +def add_autoapi_theme_option(app: Sphinx) -> None: |
| 34 | + """Add the autoapi template path to the theme options. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + app : ~sphinx.application.Sphinx |
| 39 | + Application instance for rendering the documentation. |
| 40 | + """ |
| 41 | + autoapi = app.config.html_theme_options.get("ansys_sphinx_theme_autoapi", {}) |
| 42 | + if not autoapi: |
| 43 | + return |
| 44 | + |
| 45 | + # HACK: The ``sphinx_jinja`` and ``sphinx_design`` should be added to the extensions. |
| 46 | + required_extensions = ["sphinx_jinja", "sphinx_design"] |
| 47 | + |
| 48 | + for extension in required_extensions: |
| 49 | + if extension not in app.config["extensions"]: |
| 50 | + app.config["extensions"].append(extension) |
| 51 | + AUTOAPI_OPTIONS = [ |
| 52 | + "members", |
| 53 | + "undoc-members", |
| 54 | + "show-inheritance", |
| 55 | + "show-module-summary", |
| 56 | + "special-members", |
| 57 | + ] |
| 58 | + autoapi_template_dir = autoapi.get("templates", "") |
| 59 | + autoapi_project_name = autoapi.get("project", "") |
| 60 | + |
| 61 | + if not autoapi_template_dir: |
| 62 | + autoapi_template_dir = get_autoapi_templates_dir_relative_path(app.confdir) |
| 63 | + |
| 64 | + app.config["autoapi_template_dir"] = autoapi_template_dir |
| 65 | + |
| 66 | + def prepare_jinja_env(jinja_env) -> None: |
| 67 | + """Prepare the Jinja environment for the theme.""" |
| 68 | + jinja_env.globals["project_name"] = autoapi_project_name |
| 69 | + |
| 70 | + # Set the autoapi options |
| 71 | + |
| 72 | + app.config["autoapi_prepare_jinja_env"] = prepare_jinja_env |
| 73 | + app.config["autoapi_type"] = autoapi.get("type", "python") |
| 74 | + app.config["autoapi_root"] = autoapi.get("output", "api") |
| 75 | + app.config["autoapi_own_page_level"] = autoapi.get("own_page_level", "class") |
| 76 | + app.config["autoapi_python_use_implicit_namespaces"] = autoapi.get( |
| 77 | + "use_implicit_namespaces", True |
| 78 | + ) |
| 79 | + app.config["autoapi_keep_files"] = autoapi.get("keep_files", True) |
| 80 | + app.config["autoapi_python_class_content"] = autoapi.get("class_content", "class") |
| 81 | + app.config["autoapi_options"] = autoapi.get("options", AUTOAPI_OPTIONS) |
| 82 | + |
| 83 | + # HACK: The ``autoapi_dirs`` should be given as a relative path to the conf.py. |
| 84 | + relative_autoapi_dir = os.path.relpath( |
| 85 | + autoapi.get("directory", "src/ansys"), start=str(app.confdir / "conf.py") |
| 86 | + ) |
| 87 | + app.config["autoapi_dirs"] = [relative_autoapi_dir] |
| 88 | + |
| 89 | + |
| 90 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 91 | + """Add the autoapi extension to the Sphinx application. |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + app : ~sphinx.application.Sphinx |
| 96 | + Application instance for rendering the documentation. |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + Dict[str, Any] |
| 101 | + A dictionary containing the version and parallel read/write safety flags. |
| 102 | + """ |
| 103 | + # HACK: The ``autoapi.extension`` should add here to initialize the extension. |
| 104 | + app.setup_extension("autoapi.extension") |
| 105 | + app.connect("builder-inited", add_autoapi_theme_option, priority=400) |
| 106 | + return { |
| 107 | + "version": __version__, |
| 108 | + "parallel_read_safe": True, |
| 109 | + "parallel_write_safe": True, |
| 110 | + } |
0 commit comments