|
10 | 10 | # add these directories to sys.path here. If the directory is relative to the |
11 | 11 | # documentation root, use os.path.abspath to make it absolute, like shown here. |
12 | 12 | # |
13 | | -import pathlib |
14 | | -import sys |
| 13 | +import sys.path |
| 14 | +import skillsnetwork |
15 | 15 |
|
16 | | -sys.path.insert(0, pathlib.Path(__file__).parents[2].resolve().as_posix()) |
| 16 | +from operator import attrgetter |
| 17 | +from os.path import relpath |
| 18 | +from inspect import getsourcefile, getsourcelines |
| 19 | +from pathlib import Path |
| 20 | +from subprocess import Popen |
| 21 | + |
| 22 | +sys.path.insert(0, Path(__file__).parents[2].resolve().as_posix()) |
17 | 23 |
|
18 | 24 |
|
19 | 25 | # -- Project information ----------------------------------------------------- |
|
37 | 43 | "sphinx.ext.autodoc", |
38 | 44 | "sphinx.ext.autosummary", |
39 | 45 | "sphinx.ext.intersphinx", |
40 | | - "sphinx.ext.viewcode", |
| 46 | + "sphinx.ext.linkcode", |
41 | 47 | "sphinx_autodoc_typehints", |
42 | 48 | ] |
43 | 49 |
|
|
72 | 78 | "image_dark": "SN_web_darkmode.png", |
73 | 79 | } |
74 | 80 | } |
| 81 | + |
| 82 | +# -- Options for the linkcode extension -------------------------------------- |
| 83 | +# Resolve function |
| 84 | +# This function is used to populate the (source) links in the API |
| 85 | +def linkcode_resolve(domain, info): |
| 86 | + if domain != "py" or not info["module"] == "skillsnetwork": |
| 87 | + return None |
| 88 | + try: |
| 89 | + obj = attrgetter(info["fullname"])(skillsnetwork) |
| 90 | + fn = relpath( |
| 91 | + getsourcefile(obj), |
| 92 | + start=Path(skillsnetwork.__file__).parent, |
| 93 | + ) |
| 94 | + source, lineno = getsourcelines(obj) |
| 95 | + filename = f"skillsnetwork/{fn}#L{lineno}-L{lineno + len(source) - 1}" |
| 96 | + except Exception as e: |
| 97 | + return None |
| 98 | + |
| 99 | + commit_hash = Popen( |
| 100 | + ["git", "rev-parse", "HEAD"], |
| 101 | + stdout=subprocess.PIPE, |
| 102 | + universal_newlines=True, |
| 103 | + ).communicate()[0][:-1] |
| 104 | + return ( |
| 105 | + "https://github.com/ibm-skills-network/skillsnetwork-python-library/blob/%s/%s" |
| 106 | + % (commit_hash, filename) |
| 107 | + ) |
0 commit comments