Skip to content

Commit 1bcf97f

Browse files
authored
Add direct links to github source code to docs (#7738)
### Description This adds direct links to the respective source code of classes, methods, etc. to the docs. In my opinion these nicer and more useful than viewing the source code in copy in the docs. ![image](https://github.com/Project-MONAI/MONAI/assets/20091488/4a6a2650-9fd2-4cd9-a2a1-084cc3a5fb36) I currently added it in addition to the links to the source files copied into the docs. If desired this could also be done instead of. I also used a github logo instead of another `[source]` link. I am not sure whether there is any easier way to change that into the logo than the way I have done, but this one seems to work. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [x] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: John Zielke <j.l.zielke@gmail.com>
1 parent daf2e71 commit 1bcf97f

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

docs/source/conf.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import os
1414
import subprocess
1515
import sys
16+
import importlib
17+
import inspect
1618

1719
sys.path.insert(0, os.path.abspath(".."))
1820
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
@@ -95,7 +97,7 @@ def generate_apidocs(*args):
9597
"sphinx.ext.mathjax",
9698
"sphinx.ext.napoleon",
9799
"sphinx.ext.autodoc",
98-
"sphinx.ext.viewcode",
100+
"sphinx.ext.linkcode",
99101
"sphinx.ext.autosectionlabel",
100102
"sphinx.ext.autosummary",
101103
"sphinx_autodoc_typehints",
@@ -162,3 +164,54 @@ def setup(app):
162164
# Hook to allow for automatic generation of API docs
163165
# before doc deployment begins.
164166
app.connect("builder-inited", generate_apidocs)
167+
168+
169+
# -- Linkcode configuration --------------------------------------------------
170+
DEFAULT_REF = "dev"
171+
if os.environ.get("GITHUB_REF_TYPE", "branch") == "tag":
172+
# When building a tag, link to the tag itself
173+
git_ref = os.environ.get("GITHUB_REF", DEFAULT_REF)
174+
else:
175+
git_ref = os.environ.get("GITHUB_SHA", DEFAULT_REF)
176+
177+
DEFAULT_REPOSITORY = "Project-MONAI/MONAI"
178+
repository = os.environ.get("GITHUB_REPOSITORY", DEFAULT_REPOSITORY)
179+
180+
base_code_url = f"https://github.com/{repository}/blob/{git_ref}"
181+
MODULE_ROOT_FOLDER = "monai"
182+
183+
184+
# Adjusted from https://github.com/python-websockets/websockets/blob/main/docs/conf.py
185+
def linkcode_resolve(domain, info):
186+
if domain != "py":
187+
raise ValueError(
188+
f"expected domain to be 'py', got {domain}."
189+
"Please adjust linkcode_resolve to either handle this domain or ignore it."
190+
)
191+
192+
mod = importlib.import_module(info["module"])
193+
if "." in info["fullname"]:
194+
objname, attrname = info["fullname"].split(".")
195+
obj = getattr(mod, objname)
196+
try:
197+
# object is a method of a class
198+
obj = getattr(obj, attrname)
199+
except AttributeError:
200+
# object is an attribute of a class
201+
return None
202+
else:
203+
obj = getattr(mod, info["fullname"])
204+
205+
try:
206+
file = inspect.getsourcefile(obj)
207+
source, lineno = inspect.getsourcelines(obj)
208+
except TypeError:
209+
# e.g. object is a typing.Union
210+
return None
211+
file = os.path.relpath(file, os.path.abspath(".."))
212+
if not file.startswith(MODULE_ROOT_FOLDER):
213+
# e.g. object is a typing.NewType
214+
return None
215+
start, end = lineno, lineno + len(source) - 1
216+
url = f"{base_code_url}/{file}#L{start}-L{end}"
217+
return url

0 commit comments

Comments
 (0)