Skip to content

Commit a85899c

Browse files
committed
feat: add linkcode_resolve function for enhanced documentation linking
1 parent fc16017 commit a85899c

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

docs/conf.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,36 @@
184184
]
185185

186186

187-
# -- Extension configuration -------------------------------------------------
187+
# -- linkcode setting -------------------------------------------------
188+
189+
import inspect
190+
import os
191+
import sys
192+
import functools
193+
194+
GITHUB_REPO = "https://github.com/AtsushiSakai/PythonRobotics"
195+
GITHUB_BRANCH = "main"
196+
197+
198+
def linkcode_resolve(domain, info):
199+
if domain != "py":
200+
return None
201+
202+
modname = info["module"]
203+
fullname = info["fullname"]
204+
205+
try:
206+
module = __import__(modname, fromlist=[fullname])
207+
obj = functools.reduce(getattr, fullname.split("."), module)
208+
except (ImportError, AttributeError):
209+
return None
210+
211+
try:
212+
srcfile = inspect.getsourcefile(obj)
213+
srcfile = os.path.relpath(srcfile, start=os.path.dirname(
214+
sys.modules[modname].__file__))
215+
lineno = inspect.getsourcelines(obj)[1]
216+
except Exception:
217+
return None
218+
219+
return f"{GITHUB_REPO}/blob/{GITHUB_BRANCH}/{srcfile}#L{lineno}"

0 commit comments

Comments
 (0)