27
27
28
28
Uses the following config values in ``conf.py``::
29
29
30
- project_dir: Path = ... # default: Path.cwd()
30
+ rtd_links_prefix: os.PathLike | str = ... # default: '.'
31
31
32
32
# sphinx book theme style
33
33
html_context = dict(
41
41
github_version=...,
42
42
)
43
43
44
- The ``project_dir `` is used to figure out the .py file path relative to the git root,
45
- that is to construct the path in the github URL.
44
+ The ``rtd_links_prefix `` is for figuring out the .py file path relative to the git root,
45
+ that is to construct the path in the GitHub URL.
46
46
47
47
Which ``html_context`` style you want to use depends on your theme, e.g.
48
48
:doc:`Sphinx Book Theme <sphinx_book_theme:index>`.
78
78
from .. import _setup_sig , metadata
79
79
80
80
81
- project_dir = None # type: Path
82
- github_base_url = None # type: str
81
+ rtd_links_prefix : Path = None
82
+ github_base_url : str = None
83
83
84
84
85
85
def _init_vars (app : Sphinx , config : Config ):
86
86
"""Called when ``conf.py`` has been loaded."""
87
- global github_base_url , project_dir
87
+ global github_base_url , rtd_links_prefix
88
88
_check_html_context (config )
89
89
try :
90
90
github_base_url = "https://github.com/{github_user}/{github_repo}/tree/{github_version}" .format_map (
@@ -94,7 +94,7 @@ def _init_vars(app: Sphinx, config: Config):
94
94
github_base_url = "{repository_url}/tree/{repository_branch}" .format_map (
95
95
config .html_context
96
96
)
97
- project_dir = Path (config .project_dir )
97
+ rtd_links_prefix = PurePosixPath (config .rtd_links_prefix )
98
98
99
99
100
100
def _get_obj_module (qualname : str ) -> tuple [Any , ModuleType ]:
@@ -137,6 +137,14 @@ def _get_linenos(obj):
137
137
return start , start + len (lines ) - 1
138
138
139
139
140
+ def _module_path (module : ModuleType ) -> PurePosixPath :
141
+ stem = PurePosixPath (* module .__name__ .split ("." ))
142
+ if Path (module .__file__ ).name == "__init__.py" :
143
+ return stem / "__init__.py"
144
+ else :
145
+ return stem .with_suffix (".py" )
146
+
147
+
140
148
def github_url (qualname : str ) -> str :
141
149
"""Get the full GitHub URL for some object’s qualname.
142
150
@@ -151,13 +159,7 @@ def github_url(qualname: str) -> str:
151
159
except Exception :
152
160
print (f"Error in github_url({ qualname !r} ):" , file = sys .stderr )
153
161
raise
154
- try : # only works when installed in dev mode
155
- path = PurePosixPath (Path (module .__file__ ).resolve ().relative_to (project_dir ))
156
- except ValueError :
157
- # no dev mode or something from another package
158
- path = PurePosixPath (* module .__file__ .split ("/" )[- 2 :])
159
- if (project_dir / "src" ).is_dir ():
160
- path = "src" / path
162
+ path = rtd_links_prefix / _module_path (module )
161
163
start , end = _get_linenos (obj )
162
164
fragment = f"#L{ start } -L{ end } " if start and end else ""
163
165
return f"{ github_base_url } /{ path } { fragment } "
@@ -188,14 +190,8 @@ def _check_html_context(config: Config):
188
190
@_setup_sig
189
191
def setup (app : Sphinx ) -> dict [str , Any ]:
190
192
"""Register the :func:`github_url` :ref:`Jinja filter <jinja:filters>`."""
191
- # Guess default project dir
192
- proj_dir = Path .cwd ()
193
- if proj_dir .name == "docs" :
194
- proj_dir = proj_dir .parent
195
- elif not (proj_dir / "docs" ).is_dir ():
196
- proj_dir = proj_dir .parent
197
-
198
- app .add_config_value ("project_dir" , proj_dir , "" )
193
+
194
+ app .add_config_value ("rtd_links_prefix" , PurePosixPath ("." ), "" )
199
195
app .connect ("config-inited" , _init_vars )
200
196
201
197
# if linkcode config not set
0 commit comments