Skip to content

Commit 0b5b419

Browse files
authored
ci(docs): Add source code links (#322)
* Add sphinx.ext.linkcode extension and linkcode_resolve function in conf.py * Change build-deploy-docs.yml to do the Build Documentation step after the Determine deployment folder step, and make it set the environment variable TORCHJD_VERSION This adds a new [source] link for each public class and function in the documentation, linking to the corresponding lines of code of either the main branch or the vX.Y.Z tag, on GitHub.
1 parent c88bdfe commit 0b5b419

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

.github/workflows/build-deploy-docs.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ jobs:
2525
- name: Install dependencies (default & doc)
2626
run: pdm install --group doc --frozen-lockfile
2727

28-
- name: Build Documentation
29-
working-directory: docs
30-
run: pdm run make dirhtml
31-
3228
- name: Determine deployment folder
3329
id: deploy_folder
3430
run: |
3531
echo "Determining deployment folder..."
3632
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
3733
echo "Deploying to target ${{ github.ref_name }}"
3834
echo "DEPLOY_DIR=${{ github.ref_name }}" >> $GITHUB_OUTPUT
35+
echo "TORCHJD_VERSION=${{ github.ref_name }}" >> $GITHUB_OUTPUT
3936
else
4037
echo "Deploying to target latest"
4138
echo "DEPLOY_DIR=latest" >> $GITHUB_OUTPUT
39+
echo "TORCHJD_VERSION=main" >> $GITHUB_OUTPUT
4240
fi
4341
42+
- name: Build Documentation
43+
working-directory: docs
44+
run: pdm run make dirhtml
45+
env:
46+
TORCHJD_VERSION: ${{ steps.deploy_folder.outputs.TORCHJD_VERSION }}
47+
4448
- name: Deploy to DEPLOY_DIR of TorchJD/documentation
4549
uses: peaceiris/actions-gh-pages@v4
4650
with:

docs/source/conf.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# For the full list of built-in configuration values, see the documentation:
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
55

6+
import inspect
67
import os
8+
import sys
79

810
import tomli
911

@@ -33,6 +35,7 @@
3335

3436
extensions = [
3537
"sphinx.ext.autodoc",
38+
"sphinx.ext.linkcode",
3639
"sphinx_autodoc_typehints",
3740
"sphinx.ext.intersphinx",
3841
"myst_parser", # Enables markdown support
@@ -70,3 +73,63 @@
7073
]
7174

7275
html_title = "TorchJD"
76+
77+
78+
def linkcode_resolve(domain: str, info: dict[str, str]) -> str | None:
79+
"""
80+
Returns an optional link to the source code of an object defined by its domain and info.
81+
82+
See https://www.sphinx-doc.org/en/master/usage/extensions/linkcode.html#confval-linkcode_resolve
83+
for more information.
84+
"""
85+
86+
if domain != "py" or not info["module"]:
87+
return None
88+
89+
obj = _get_obj(info)
90+
file_name = _get_file_name(obj)
91+
92+
if not file_name:
93+
return None
94+
95+
line_str = _get_line_str(obj)
96+
version_str = _get_version_str()
97+
98+
link = f"https://github.com/TorchJD/torchjd/blob/{version_str}/{file_name}{line_str}"
99+
return link
100+
101+
102+
def _get_obj(_info: dict[str, str]):
103+
module_name = _info["module"]
104+
full_name = _info["fullname"]
105+
sub_module = sys.modules.get(module_name)
106+
obj = sub_module
107+
for part in full_name.split("."):
108+
obj = getattr(obj, part)
109+
# strip decorators, which would resolve to the source of the decorator
110+
obj = inspect.unwrap(obj)
111+
return obj
112+
113+
114+
def _get_file_name(obj) -> str | None:
115+
try:
116+
file_name = inspect.getsourcefile(obj)
117+
file_name = os.path.relpath(file_name, start=_PATH_ROOT)
118+
except TypeError: # This seems to happen when obj is a property
119+
file_name = None
120+
return file_name
121+
122+
123+
def _get_line_str(obj) -> str:
124+
source, start = inspect.getsourcelines(obj)
125+
end = start + len(source) - 1
126+
line_str = f"#L{start}-L{end}"
127+
return line_str
128+
129+
130+
def _get_version_str() -> str:
131+
try:
132+
version_str = os.environ["TORCHJD_VERSION"]
133+
except KeyError:
134+
version_str = "main"
135+
return version_str

0 commit comments

Comments
 (0)