Skip to content

Commit 4fa2621

Browse files
committed
feat: enhance documentation linking with linkcode and relative path resolution
1 parent a85899c commit 4fa2621

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs/conf.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#
1414
import os
1515
import sys
16+
from pathlib import Path
17+
1618
sys.path.insert(0, os.path.abspath('../'))
1719

1820

@@ -41,7 +43,7 @@
4143
'matplotlib.sphinxext.plot_directive',
4244
'sphinx.ext.autodoc',
4345
'sphinx.ext.mathjax',
44-
'sphinx.ext.viewcode',
46+
'sphinx.ext.linkcode',
4547
'sphinx.ext.napoleon',
4648
'sphinx.ext.imgconverter',
4749
'IPython.sphinxext.ipython_console_highlighting',
@@ -192,7 +194,7 @@
192194
import functools
193195

194196
GITHUB_REPO = "https://github.com/AtsushiSakai/PythonRobotics"
195-
GITHUB_BRANCH = "main"
197+
GITHUB_BRANCH = "master"
196198

197199

198200
def linkcode_resolve(domain, info):
@@ -210,10 +212,19 @@ def linkcode_resolve(domain, info):
210212

211213
try:
212214
srcfile = inspect.getsourcefile(obj)
213-
srcfile = os.path.relpath(srcfile, start=os.path.dirname(
214-
sys.modules[modname].__file__))
215+
srcfile = get_relative_path_from_parent(srcfile, "PythonRobotics")
215216
lineno = inspect.getsourcelines(obj)[1]
216217
except Exception:
217218
return None
218219

219220
return f"{GITHUB_REPO}/blob/{GITHUB_BRANCH}/{srcfile}#L{lineno}"
221+
222+
223+
def get_relative_path_from_parent(file_path: str, parent_dir: str):
224+
path = Path(file_path).resolve()
225+
226+
try:
227+
parent_path = next(p for p in path.parents if p.name == parent_dir)
228+
return str(path.relative_to(parent_path))
229+
except StopIteration:
230+
raise ValueError(f"Parent directory '{parent_dir}' not found in {file_path}")

0 commit comments

Comments
 (0)