Skip to content

Commit 5b456d5

Browse files
Revathyvenugopal162jorgepilotoMaxJPReyRobPasMue
committed
feat: add link code extension (#269)
Co-authored-by: Jorge Martínez <[email protected]> Co-authored-by: Maxime Rey <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]>
1 parent 6e98090 commit 5b456d5

File tree

9 files changed

+488
-4
lines changed

9 files changed

+488
-4
lines changed

doc/source/_templates/message.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="sidebar-message">
2+
See the new feature of linkcode extension in
3+
<a href="https://sphinxdocs.ansys.com/version/dev/user_guide/extensions.html"
4+
>dev docs.</a
5+
>
6+
Your contributions are welcome!
7+
</div>

doc/source/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
watermark,
2121
)
2222

23+
ANNOUNCEMENT_URL = (
24+
"https://raw.githubusercontent.com/ansys/ansys-sphinx-theme/main/doc/_templates/message.html"
25+
)
26+
2327
# Project information
2428
project = "ansys_sphinx_theme"
2529
copyright = f"(c) {datetime.now().year} ANSYS, Inc. All rights reserved"
@@ -45,6 +49,7 @@
4549
html_theme_options = {
4650
"github_url": "https://github.com/ansys/ansys-sphinx-theme",
4751
"contact_mail": "[email protected]",
52+
"use_edit_page_button": True,
4853
"additional_breadcrumbs": [
4954
("PyAnsys", "https://docs.pyansys.com/"),
5055
],
@@ -64,6 +69,7 @@
6469
f"ansys-sphinx-theme-v{convert_version_to_pymeilisearch(__version__)}": "ansys-sphinx-theme", # noqa: E501
6570
},
6671
},
72+
"announcement": ANNOUNCEMENT_URL,
6773
}
6874

6975
html_short_title = html_title = "Ansys Sphinx Theme"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. _ref_user_guide_extension:
2+
3+
Extensions
4+
==========
5+
6+
This page contains the shipped extensions that seamlessly integrate with this theme to enhance
7+
documentation functionality.
8+
9+
Linkcode extension
10+
-------------------
11+
12+
The Linkcode extension automatically adds "Source" links to the documentation for Python, C, C++,
13+
and JavaScript objects. It allows you to link to the source code hosted on GitHub.
14+
15+
Installation
16+
~~~~~~~~~~~~
17+
18+
To use the ``ansys-sphinx-theme`` linkcode extension, you need to add it to the Sphinx configuration:
19+
20+
#. Add the following line in the ``conf.py`` file:
21+
22+
.. code-block:: python
23+
24+
extensions = ["ansys_sphinx_theme.extension.linkcode"]
25+
26+
Configuration options
27+
---------------------
28+
29+
The Linkcode extension provides a way to configure its behavior by using certain options within your ``conf.py`` file.
30+
Depending on your preferred approach, you can utilize the direct
31+
configuration options or the ``html_context`` dictionary to streamline your settings.
32+
33+
If both sets of configuration options are given, the direct configuration options (that is, ``link_code_library``,
34+
``link_code_source``, ``link_code_branch``) has
35+
precedence over the corresponding settings in the ``html_context`` dictionary.
36+
37+
Direct configuration options
38+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39+
40+
- ``link_code_library`` :
41+
The user/repository name where the source code is hosted. For example, ``ansys/ansys-sphinx-theme``.
42+
43+
- ``link_code_source`` (str, optional, default: ''):
44+
The relative path of the source code file within the repository. For example, ``src``.
45+
46+
- ``link_code_branch`` (str, optional, default: 'main'):
47+
The GitHub branch. It can be a specific version like ``main`` or ``dev``.
48+
49+
If the ``link_code_source`` and ``link_code_branch`` options are not provided in the configuration,
50+
the following default values are used:
51+
52+
- ``link_code_source``: An empty string (``''``). This links to the root of the repository.
53+
- ``link_code_branch``: ``main``. This is the default branch name used if no branch is specified.
54+
55+
.. code-block:: python
56+
57+
# Example of setting direct configuration in example
58+
link_code_library = "username/repo-name"
59+
link_code_source = "src"
60+
link_code_branch = "dev"
61+
62+
Using ``html_context`` dictionary
63+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
You also have the option to centralize your GitHub-related configuration by incorporating it
65+
directly into the ``html_context`` dictionary within your ``conf.py`` file. This approach allows you to
66+
minimize redundancy and manage the GitHub-related information more effectively for the extension:
67+
68+
.. code-block:: python
69+
70+
# Example of setting GitHub-related configuration in conf.py
71+
html_context = {
72+
"github_user": "<your-github-org>",
73+
"github_repo": "<your-github-repo>",
74+
"github_version": "<your-branch>",
75+
"source_path": "<path-from-root-to-your-source_file>",
76+
}
77+
78+
With this setup, you can fine-tune your configuration according to your preferences and requirements,
79+
enhancing the integration of the ``linkcode`` extension into your documentation.

doc/source/user_guide/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ While the Ansys Sphinx theme is often used as is, you can customize the followin
1313
- :ref:`ref_user_guide_html_theme`
1414
- :ref:`ref_user_guide_pdf_cover`
1515
- :ref:`ref_user_guide_404_page`
16-
16+
- :ref:`ref_user_guide_extension`
17+
1718
.. toctree::
1819
:hidden:
1920
:maxdepth: 2
@@ -22,4 +23,5 @@ While the Ansys Sphinx theme is often used as is, you can customize the followin
2223
options
2324
pdf
2425
404_page
26+
extensions
2527

doc/styles/Vocab/ANSYS/accept.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ ansys
44
Ansys Sphinx Theme
55
boolean
66
datatable
7-
MeiliSearch
7+
MeiliSearch
8+
Linkcode
9+
linkcode
10+
link_code_library
11+
link_code_source
12+
link_code_branch
13+
html_context

src/ansys_sphinx_theme/__init__.py

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from typing import Any, Dict
44

55
from docutils.nodes import document
6+
from sphinx import addnodes
67
from sphinx.application import Sphinx
78

9+
from ansys_sphinx_theme.extension.linkcode import DOMAIN_KEYS, sphinx_linkcode_resolve
810
from ansys_sphinx_theme.latex import generate_404 # noqa: F401
911

1012
__version__ = "0.10.2"
@@ -107,6 +109,103 @@ def setup_default_html_theme_options(app):
107109
app.config.html_theme_options.setdefault("collapse_navigation", True)
108110

109111

112+
def fix_edit_html_page_context(
113+
app: Sphinx, pagename: str, templatename: str, context: dict, doctree: document
114+
) -> None:
115+
"""Add a function that Jinja can access for returning an "edit this page" link .
116+
117+
This function creates an "edit this page" link for any library.
118+
The link points to the corresponding file on the main branch.
119+
120+
Parameters
121+
----------
122+
app : Sphinx
123+
Sphinx application instance for rendering the documentation.
124+
pagename : str
125+
Name of the current page.
126+
templatename : str
127+
Name of the template being used.
128+
context : dict
129+
Context dictionary for the page.
130+
doctree : document
131+
Document tree for the page.
132+
133+
Notes
134+
-----
135+
.. [1] Originally implemented by `Alex Kaszynski <https://github.com/akaszynski>`_ in
136+
`PyVista <https://github.com/pyvista/pyvista>`_,
137+
see https://github.com/pyvista/pyvista/pull/4113
138+
"""
139+
140+
def fix_edit_link_button(link: str) -> str:
141+
"""Transform "edit on GitHub" links to the correct URL.
142+
143+
This function fixes the URL for the "edit this page" link.
144+
145+
Parameters
146+
----------
147+
link : str
148+
Link to the GitHub edit interface.
149+
150+
Returns
151+
-------
152+
str
153+
Link to the corresponding file on the main branch.
154+
"""
155+
github_user = context.get("github_user", "")
156+
github_repo = context.get("github_repo", "")
157+
github_source = context.get("source_path", "")
158+
kind = context.get("github_version", "")
159+
160+
if "_autosummary" in pagename:
161+
for obj_node in list(doctree.findall(addnodes.desc)):
162+
domain = obj_node.get("domain")
163+
for signode in obj_node:
164+
if not isinstance(signode, addnodes.desc_signature):
165+
continue
166+
# Convert signode to a specified format
167+
info = {}
168+
for key in DOMAIN_KEYS.get(domain, []):
169+
value = signode.get(key)
170+
if not value:
171+
value = ""
172+
info[key] = value
173+
if not info:
174+
continue
175+
# This is an API example
176+
return sphinx_linkcode_resolve(
177+
domain=domain,
178+
info=info,
179+
library=f"{github_user}/{github_repo}",
180+
source_path=github_source,
181+
github_version=kind,
182+
edit=True,
183+
)
184+
185+
elif "autoapi" in pagename:
186+
for obj_node in list(doctree.findall(addnodes.desc)):
187+
domain = obj_node.get("domain")
188+
if domain != "py":
189+
return link
190+
191+
for signode in obj_node:
192+
if not isinstance(signode, addnodes.desc_signature):
193+
continue
194+
195+
fullname = signode["module"]
196+
modname = fullname.replace(".", "/")
197+
198+
if github_source:
199+
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{github_source}/{modname}.{domain}" # noqa: E501
200+
else:
201+
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{modname}.{domain}" # noqa: E501
202+
203+
else:
204+
return link
205+
206+
context["fix_edit_link_button"] = fix_edit_link_button
207+
208+
110209
def update_footer_theme(
111210
app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: document
112211
) -> None:
@@ -162,9 +261,8 @@ def setup(app: Sphinx) -> Dict:
162261
app.add_js_file("https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js")
163262
app.add_css_file("https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css")
164263
app.connect("html-page-context", update_footer_theme)
165-
# Add templates for autosummary
264+
app.connect("html-page-context", fix_edit_html_page_context)
166265
app.config.templates_path.append(str(TEMPLATES_PATH))
167-
168266
return {
169267
"version": __version__,
170268
"parallel_read_safe": True,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""A sub-package containing various extensions for the Ansys Sphinx Theme."""

0 commit comments

Comments
 (0)