File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 1313#
1414import os
1515import sys
16+ from pathlib import Path
17+
1618sys .path .insert (0 , os .path .abspath ('../' ))
1719
1820
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' ,
192194import functools
193195
194196GITHUB_REPO = "https://github.com/AtsushiSakai/PythonRobotics"
195- GITHUB_BRANCH = "main "
197+ GITHUB_BRANCH = "master "
196198
197199
198200def 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 } " )
You can’t perform that action at this time.
0 commit comments