Skip to content

Commit 55665c0

Browse files
Sam ProkopchukSam Prokopchuk
authored andcommitted
Use sphinx.ext.linkcode instead of sphinx.ext.viewcode
1 parent 7bc6152 commit 55665c0

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

doc/source/conf.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
# add these directories to sys.path here. If the directory is relative to the
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
13-
import pathlib
14-
import sys
13+
import sys.path
14+
import skillsnetwork
1515

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())
1723

1824

1925
# -- Project information -----------------------------------------------------
@@ -37,7 +43,7 @@
3743
"sphinx.ext.autodoc",
3844
"sphinx.ext.autosummary",
3945
"sphinx.ext.intersphinx",
40-
"sphinx.ext.viewcode",
46+
"sphinx.ext.linkcode",
4147
"sphinx_autodoc_typehints",
4248
]
4349

@@ -72,3 +78,30 @@
7278
"image_dark": "SN_web_darkmode.png",
7379
}
7480
}
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

Comments
 (0)