Skip to content

Commit 7837400

Browse files
Revathyvenugopal162pyansys-ci-botjorgepilotoRobPasMueMaxJPRey
authored
feat: add autoapi extension (#372)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Jorge Martínez <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]> Co-authored-by: Maxime Rey <[email protected]>
1 parent 5963f20 commit 7837400

File tree

7 files changed

+226
-3
lines changed

7 files changed

+226
-3
lines changed

doc/changelog.d/372.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: add autoapi extension

doc/source/getting_started/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ you need to install the following dependencies:
2929

3030
- `sphinx-autoapi <Sphinx_AutoAPI_PyPI_>`_
3131
- `sphinx-design <Sphinx_Design_PyPI_>`_
32+
- `sphinx-jinja <Sphinx_Jinja_PyPI_>`_
3233

3334
An example page demonstrating autoapi rendering with the Ansys sphinx theme template can
3435
be found on the ``API Reference`` page of the
@@ -73,4 +74,5 @@ Consider using the ``conf.py`` for this repository:
7374
.. _Jinja2_PyPI: https://pypi.org/project/Jinja2/
7475
.. _Sphinx_AutoAPI_PyPI: https://pypi.org/project/sphinx-autoapi/
7576
.. _Sphinx_Design_PyPI: https://pypi.org/project/sphinx-design/
77+
.. _Sphinx_Jinja_PyPI: https://pypi.org/project/sphinx-jinja/
7678
.. _PyAnsys_Geometry_Docs: https://geometry.docs.pyansys.com/

doc/source/user_guide/autoapi.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,90 @@ do so by modifying the configuration above. The line of code declaring the desir
7373
autoapi_template_dir = get_autoapi_templates_dir_relative_path(Path(__file__))
7474
7575
76+
``ansys_sphinx_theme_autoapi`` theme options and extension
77+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78+
79+
To use the ``autoapi`` along with the ``ansys-sphinx-theme``, you need to
80+
add ``ansys_sphinx_theme.extension.autoapi`` to the ``extensions`` list in your ``conf.py`` file
81+
and set the ``ansys_sphinx_theme_autoapi`` theme options in the ``html_theme_options`` dictionary.
82+
83+
- ``project``: The name of the project.
84+
- ``output``: The path to the directory where the generated files are placed.
85+
By default, this is set to the ``api`` directory.
86+
- ``templates``: The path to the directory containing the custom templates for ``sphinx-autoapi``.
87+
By default, this is set to the ``autoapi_templates`` directory in the theme package.
88+
- ``directory``: The path to the directory containing the source code with respect to the ``conf.py`` file.
89+
By default, this is set to the ``src/ansys`` directory.
90+
- ``use_implicit_namespaces``: If set to ``True``, the autoapi extension use `implicit namespaces`.
91+
By default, this is set to ``True``.
92+
- ``keep_files``: If set to ``True``, the autoapi extension keeps the generated files.
93+
By default, this is set to ``True``.
94+
- ``own_page_level``: The level of the page where the autoapi extension places the content of the class.
95+
By default, this is set to ``class``.
96+
- ``type``: The type of the autoapi extension. By default, this is set to ``python``.
97+
- ``options``: The options to pass to the autoapi extension. By default,
98+
this is set to ``["members", "undoc-members", "show-inheritance", "show-module-summary", "special-members"]``.
99+
- ``class_content``: The content of the class. By default this is set to ``class``.
100+
101+
All these options can be set in the ``conf.py`` file of your Sphinx project.
102+
103+
.. code:: python
104+
105+
html_theme_options = {
106+
"ansys-sphinx-theme-autoapi": {
107+
"project": "My Project",
108+
"output": "api",
109+
"directory": "src/ansys",
110+
"use_implicit_namespaces": True,
111+
"keep_files": True,
112+
"own_page_level": "class",
113+
"type": "python",
114+
"options": [
115+
"members",
116+
"undoc-members",
117+
"show-inheritance",
118+
"show-module-summary",
119+
"special-members",
120+
],
121+
"class_content": "class",
122+
}
123+
}
124+
125+
You need to add ``ansys_sphinx_theme.extension.autoapi`` to the ``extensions`` list in your ``conf.py`` file:
126+
127+
.. code:: python
128+
129+
extensions = [
130+
"ansys_sphinx_theme.extension.autoapi",
131+
]
132+
133+
The complete configuration for ``sphinx-autoapi`` in your ``conf.py`` file should look like this:
134+
135+
.. code:: python
136+
137+
138+
html_theme_options = {
139+
"ansys_sphinx_theme_autoapi": {
140+
"project": "My Project",
141+
"output": "api",
142+
"use_implicit_namespaces": True,
143+
"directory": "src/ansys",
144+
"keep_files": True,
145+
"own_page_level": "class",
146+
"type": "python",
147+
"options": [
148+
"members",
149+
"undoc-members",
150+
"show-inheritance",
151+
"show-module-summary",
152+
"special-members",
153+
],
154+
"class_content": "class",
155+
}
156+
}
157+
158+
extensions = [
159+
"ansys_sphinx_theme.extension.autoapi",
160+
]
161+
162+

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies = [
3535
autoapi = [
3636
"sphinx-autoapi==3.0.0a4",
3737
"sphinx-design==0.5.0",
38+
"sphinx-jinja==2.0.2",
3839
]
3940
doc = [
4041
"numpydoc==1.7.0",

src/ansys_sphinx_theme/__init__.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ def get_html_theme_path() -> pathlib.Path:
7676
return THEME_PATH.resolve()
7777

7878

79+
def get_autoapi_templates_dir_relative_path(path: pathlib.Path) -> str:
80+
"""Return a string representing the relative path for autoapi templates.
81+
82+
Parameters
83+
----------
84+
path : pathlib.Path
85+
Path to the desired file.
86+
87+
Returns
88+
-------
89+
str
90+
A string rerpesenting the relative path to the autoapi templates.
91+
92+
"""
93+
return os.path.relpath(str(AUTOAPI_TEMPLATES_PATH.absolute()), start=str(path.absolute()))
94+
95+
7996
def get_version_match(semver: str) -> str:
8097
"""Evaluate the version match for the multi-documentation.
8198
@@ -384,7 +401,11 @@ def replace_html_tag(app, exception):
384401
return
385402

386403
build_dir = pathlib.Path(app.builder.outdir).resolve()
387-
if app.config["extensions"] and "autoapi.extension" not in app.config["extensions"]:
404+
defined_extensions = app.config["extensions"]
405+
if not any(
406+
extension in defined_extensions
407+
for extension in ["autoapi.extension", "ansys_sphinx_theme.extension.autoapi"]
408+
):
388409
return
389410
api_dir = app.config["autoapi_root"]
390411
api_path = build_dir / api_dir
@@ -430,10 +451,10 @@ def setup(app: Sphinx) -> Dict:
430451
app.config.templates_path.append(str(TEMPLATES_PATH))
431452
app.add_js_file("https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js")
432453
app.add_css_file("https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css")
454+
app.add_css_file("https://www.nerdfonts.com/assets/css/webfont.css")
433455
app.connect("html-page-context", update_footer_theme)
434456
app.connect("html-page-context", fix_edit_html_page_context)
435457
app.connect("html-page-context", add_cheat_sheet)
436-
app.add_css_file("https://www.nerdfonts.com/assets/css/webfont.css")
437458
app.connect("build-finished", replace_html_tag)
438459
return {
439460
"version": __version__,
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

src/ansys_sphinx_theme/theme/ansys_sphinx_theme/theme.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ article_header_start = breadcrumbs.html
1616
footer_end = theme-version.html
1717
pygment_light_style = friendly
1818
pygment_dark_style = monokai
19-
cheatsheet =
19+
cheatsheet =
20+
ansys_sphinx_theme_autoapi =

0 commit comments

Comments
 (0)