Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add link code extension #269

Merged
merged 40 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e518022
feat: initial attempt to add the link code
Revathyvenugopal162 Aug 4, 2023
b6d8c8c
feat: add edit this page option
Revathyvenugopal162 Aug 4, 2023
82b6c21
fix typo
Revathyvenugopal162 Aug 4, 2023
a687047
test : test with pypim
Revathyvenugopal162 Aug 4, 2023
89715df
Feat: add the etxtension with env configs
Revathyvenugopal162 Aug 7, 2023
04a0c19
Feat: add the config values
Revathyvenugopal162 Aug 7, 2023
2ba7bf8
fix: context name
Revathyvenugopal162 Aug 7, 2023
bd4066b
fix: edit this page
Revathyvenugopal162 Aug 7, 2023
7013501
fix: edit this page for autoapi
Revathyvenugopal162 Aug 7, 2023
4444f61
Update src/ansys_sphinx_theme/__init__.py
Revathyvenugopal162 Aug 7, 2023
e04a861
fix: credits
Revathyvenugopal162 Aug 7, 2023
92444e7
Merge branch 'feat/add-link-code-extension' of https://github.com/ans…
Revathyvenugopal162 Aug 7, 2023
920b18e
Apply suggestions from code review
Revathyvenugopal162 Aug 8, 2023
34029c7
test: improve doctsring and function
Revathyvenugopal162 Aug 8, 2023
e4bf0ba
test: take the source director from html_context
Revathyvenugopal162 Aug 8, 2023
7390708
fix: take the source directory from html_context
Revathyvenugopal162 Aug 8, 2023
60bc78f
fix: add errors
Revathyvenugopal162 Aug 8, 2023
ba6923f
fix: add github version
Revathyvenugopal162 Aug 8, 2023
825cd7d
docs: add the documentation
Revathyvenugopal162 Aug 8, 2023
01bce5a
docs: add the indentation
Revathyvenugopal162 Aug 8, 2023
0ccc410
docs: add the accept words
Revathyvenugopal162 Aug 8, 2023
0bf2029
fix: add contants
Revathyvenugopal162 Aug 8, 2023
480a41a
fix: doce build error
Revathyvenugopal162 Aug 8, 2023
16db738
Fix: Add the reference in not section.
Revathyvenugopal162 Aug 9, 2023
a4521e9
docs: improve the docstrings
Revathyvenugopal162 Aug 9, 2023
deeaa53
Fix: Add the docstrings for domain
Revathyvenugopal162 Aug 9, 2023
16182af
doc: add the extensions page
Revathyvenugopal162 Aug 9, 2023
3324b0c
docs: improve docstrings
Revathyvenugopal162 Aug 9, 2023
406f04e
Merge branch 'feat/add-link-code-extension' of https://github.com/ans…
Revathyvenugopal162 Aug 9, 2023
b8f53ac
doc: add the extensions page clarification
Revathyvenugopal162 Aug 9, 2023
9defc41
doc: fix vale warning
Revathyvenugopal162 Aug 9, 2023
3c755b8
doc: fix docs on html page
Revathyvenugopal162 Aug 9, 2023
155e19b
docs: updates the html link
Revathyvenugopal162 Aug 9, 2023
42289f4
docs: add the doctrings and precedence
Revathyvenugopal162 Aug 10, 2023
ad71c9e
Apply suggestions from code review
Revathyvenugopal162 Aug 10, 2023
67daf97
docs: add the vale resolves
Revathyvenugopal162 Aug 10, 2023
de250ac
Merge branch 'main' into feat/add-link-code-extension
Revathyvenugopal162 Aug 10, 2023
298d7bb
doc: resolve doc error
Revathyvenugopal162 Aug 10, 2023
38f22bc
Merge branch 'feat/add-link-code-extension' of https://github.com/ans…
Revathyvenugopal162 Aug 10, 2023
f0d0608
Apply suggestions from code review
Revathyvenugopal162 Aug 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/_templates/message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="sidebar-message">
See the new feature of linkcode extension in
<a href="https://sphinxdocs.ansys.com/version/dev/user_guide/link_code.html"
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
>dev docs.</a
>
Your contributions are welcome!
</div>
6 changes: 6 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
watermark,
)

ANNOUNCEMENT_URL = (
"https://raw.githubusercontent.com/ansys/ansys-sphinx-theme/main/doc/_templates/message.html"
)

# Project information
project = "ansys_sphinx_theme"
copyright = f"(c) {datetime.now().year} ANSYS, Inc. All rights reserved"
Expand All @@ -45,6 +49,7 @@
html_theme_options = {
"github_url": "https://github.com/ansys/ansys-sphinx-theme",
"contact_mail": "pyansys.support@ansys.com",
"use_edit_page_button": True,
"additional_breadcrumbs": [
("PyAnsys", "https://docs.pyansys.com/"),
],
Expand All @@ -64,6 +69,7 @@
f"ansys-sphinx-theme-v{convert_version_to_pymeilisearch(__version__)}": "ansys-sphinx-theme", # noqa: E501
},
},
"announcement": ANNOUNCEMENT_URL,
}

html_short_title = html_title = "Ansys Sphinx Theme"
Expand Down
4 changes: 3 additions & 1 deletion doc/source/user_guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ While the Ansys Sphinx theme is often used as is, you can customize the followin
- :ref:`ref_user_guide_html_theme`
- :ref:`ref_user_guide_pdf_cover`
- :ref:`ref_user_guide_404_page`

- :ref:`ref_user_guide_link_code`

.. toctree::
:hidden:
:maxdepth: 2
Expand All @@ -22,4 +23,5 @@ While the Ansys Sphinx theme is often used as is, you can customize the followin
options
pdf
404_page
link_code
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

47 changes: 47 additions & 0 deletions doc/source/user_guide/link_code.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.. _ref_user_guide_link_code:

Linkcode extension
==================

The Linkcode extension automatically adds "View Source" links to the documentation for Python, C, C++,
and JavaScript objects. It allows you to link to the source code hosted on GitHub.

Installation
------------

To use the ansys-sphinx-theme linkcode extension, you need to add it to the Sphinx configuration:
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

1. Add the following line in the ``conf.py`` file:
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

extensions = ["ansys_sphinx_theme.extension.linkcode"]

Configuration options
---------------------

The Linkcode extension supports the following configuration options in your `conf.py` file:

- ``link_code_library`` (str, optional):
The user/repository name where the source code is hosted. For example, ``ansys/ansys-sphinx-theme``.

- ``link_code_source`` (str, optional):
The relative path of the source code file within the repository. For example, ``src``.

- ``link_code_branch`` (str, optional, default: 'main'):
The GitHub branch. It can be a specific version like ``main`` or ``dev``.

You can also set the `link_code_library`, `link_code_source`, `link_code_branch`,
and other GitHub-related configuration options directly in the `html_context` dictionary in your `conf.py` file.
By doing so, you can avoid redundancy and centralize the GitHub-related information for the extension:

.. code-block:: python

# Example of setting GitHub-related configuration in conf.py
html_context = {
# "github_url": "https://github.com", # or your GitHub Enterprise site
"github_user": "<your-github-org>",
"github_repo": "<your-github-repo>",
"github_version": "<your-branch>",
"source_path": "<path-from-root-to-your-source_file>",
}
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion doc/styles/Vocab/ANSYS/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ ansys
Ansys Sphinx Theme
boolean
datatable
MeiliSearch
MeiliSearch
Linkcode
linkcode
link_code_library
link_code_source
link_code_branch
html_context
105 changes: 103 additions & 2 deletions src/ansys_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from typing import Any, Dict

from docutils.nodes import document
from sphinx import addnodes
from sphinx.application import Sphinx

from ansys_sphinx_theme.extension.linkcode import DOMAIN_KEYS, sphinx_linkcode_resolve
from ansys_sphinx_theme.latex import generate_404 # noqa: F401

__version__ = "0.11.dev0"
Expand Down Expand Up @@ -107,6 +109,106 @@ def setup_default_html_theme_options(app):
app.config.html_theme_options.setdefault("collapse_navigation", True)


def fix_edit_html_page_context(
app: Sphinx, pagename: str, templatename: str, context: dict, doctree: document
) -> None:
"""Add a function that Jinja can access for returning an "edit this page" link .

This function creates an "edit this page" link for any library.
The link points to the corresponding file on the main branch.

Parameters
----------
app : Sphinx
The Sphinx application instance for rendering the documentation.
pagename : str
The name of the current page.
templatename : str
The name of the template being used.
context : dict
The context dictionary for the page.
doctree : document
The document tree for the page.
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

Notes
-----
.. [1] Originally implemented by `Alex Kaszynski <https://github.com/akaszynski>`_ in
`PyVista <https://github.com/pyvista/pyvista>`_,
see https://github.com/pyvista/pyvista/pull/4113
"""

def fix_edit_link_button(link: str) -> str:
"""Transform "edit on GitHub" links to the correct URL.

This function fixes the URL for the "edit this page" link.

Parameters
----------
link : str
The link to the GitHub edit interface.

Returns
-------
str
The link to the corresponding file on the main branch.
"""
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
github_user = context.get("github_user", "")
github_repo = context.get("github_repo", "")
github_source = context.get("source_path", "")
kind = context.get("github_version", "")

if pagename.startswith("examples") and "index" not in pagename:
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{pagename}.py"

Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
elif "_autosummary" in pagename:
for obj_node in list(doctree.findall(addnodes.desc)):
domain = obj_node.get("domain")
for signode in obj_node:
if not isinstance(signode, addnodes.desc_signature):
continue
# Convert signode to a specified format
info = {}
for key in DOMAIN_KEYS.get(domain, []):
value = signode.get(key)
if not value:
value = ""
info[key] = value
if not info:
continue
# This is an API example
return sphinx_linkcode_resolve(
domain=domain,
info=info,
library=f"{github_user}/{github_repo}",
source_path=github_source,
github_version=kind,
edit=True,
)

elif "autoapi" in pagename:
for obj_node in list(doctree.findall(addnodes.desc)):
domain = obj_node.get("domain")
if domain != "py":
return link

for signode in obj_node:
if not isinstance(signode, addnodes.desc_signature):
continue

fullname = signode["module"]
modname = fullname.replace(".", "/")

if github_source:
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{github_source}/{modname}.py" # noqa: E501
else:
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{modname}.py" # noqa: E501

Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
else:
return link

context["fix_edit_link_button"] = fix_edit_link_button


def update_footer_theme(
app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: document
) -> None:
Expand Down Expand Up @@ -162,9 +264,8 @@ def setup(app: Sphinx) -> Dict:
app.add_js_file("https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js")
app.add_css_file("https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css")
app.connect("html-page-context", update_footer_theme)
# Add templates for autosummary
app.connect("html-page-context", fix_edit_html_page_context)
app.config.templates_path.append(str(TEMPLATES_PATH))

return {
"version": __version__,
"parallel_read_safe": True,
Expand Down
1 change: 1 addition & 0 deletions src/ansys_sphinx_theme/extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""A sub-package containing various extensions for the Ansys Sphinx Theme."""
Loading
Loading