23
23
rtd_links_prefix: os.PathLike | str = ... # default: '.'
24
24
25
25
# sphinx book theme style
26
- html_context = dict(
26
+ html_theme_options = dict(
27
27
repository_url=...,
28
28
repository_branch=...,
29
29
)
37
37
The ``rtd_links_prefix`` is for figuring out the .py file path relative to the git root,
38
38
that is to construct the path in the GitHub URL.
39
39
40
- Which ``html_context`` style you want to use depends on your theme, e.g.
40
+ Which html configuration style you want to use depends on your theme, e.g.
41
41
:doc:`Sphinx Book Theme <sphinx_book_theme:index>`.
42
42
43
43
``:github_url:`` usage
79
79
def _init_vars (app : Sphinx , config : Config ):
80
80
"""Called when ``conf.py`` has been loaded."""
81
81
global github_base_url , rtd_links_prefix
82
- _check_html_context (config )
82
+ _check_html_config (config )
83
83
try :
84
84
github_base_url = "https://github.com/{github_user}/{github_repo}/tree/{github_version}" .format_map (
85
85
config .html_context
86
86
)
87
87
except KeyError :
88
88
github_base_url = "{repository_url}/tree/{repository_branch}" .format_map (
89
- config .html_context
89
+ config .html_theme_options
90
90
)
91
91
rtd_links_prefix = PurePosixPath (config .rtd_links_prefix )
92
92
@@ -153,7 +153,8 @@ def github_url(qualname: str) -> str:
153
153
qualname: The full qualified name of a function, class, method or module
154
154
155
155
Returns:
156
- A GitHub URL derived from the :confval:`html_context`.
156
+ A GitHub URL derived from the :confval:`html_context`
157
+ or the :confval:`html_theme_options`.
157
158
"""
158
159
try :
159
160
obj , module = _get_obj_module (qualname )
@@ -166,26 +167,32 @@ def github_url(qualname: str) -> str:
166
167
return f"{ github_base_url } /{ path } { fragment } "
167
168
168
169
169
- def _check_html_context (config : Config ):
170
- try :
171
- html_context : dict [str , Any ] = config .html_context
172
- except AttributeError :
173
- raise ValueError (
174
- f"Extension { __name__ } needs “html_context” to be defined in conf.py"
175
- )
176
- options = [
177
- {"github_user" , "github_repo" , "github_version" },
178
- {"repository_url" , "repository_branch" },
179
- ]
180
- missing_value_sets = [opt - html_context .keys () for opt in options ]
181
- if all (missing_value_sets ):
182
- mvs = " or " .join (
183
- ", " .join (repr (mv ) for mv in mvs ) for mvs in missing_value_sets
184
- )
185
- raise ValueError (
186
- f"Extension { __name__ } needs html_context { mvs } to be defined in conf.py.\n "
187
- f"html_context = { html_context !r} "
170
+ def _check_html_config (config : Config ):
171
+ options = dict (
172
+ html_context = {"github_user" , "github_repo" , "github_version" },
173
+ html_theme_options = {"repository_url" , "repository_branch" },
174
+ )
175
+ if not any (name in config for name in options ):
176
+ msg = (
177
+ f"Extension { __name__ } needs “html_context” "
178
+ "or “html_theme_options” to be defined in conf.py"
188
179
)
180
+ raise ValueError (msg )
181
+ missing_value_sets = {
182
+ name : opts - config [name ].keys () for name , opts in options .items ()
183
+ }
184
+ if not all (missing_value_sets .values ()):
185
+ return # a valid configuration was found
186
+
187
+ mvs = " or " .join (
188
+ f"{ name } { ', ' .join (repr (mv ) for mv in mvs )} "
189
+ for name , mvs in missing_value_sets .items ()
190
+ )
191
+ msg = f"Extension { __name__ } needs { mvs } to be defined in conf.py."
192
+ for name in options :
193
+ if name in config :
194
+ msg += f"\n { name } = { config [name ]!r} "
195
+ raise ValueError (msg )
189
196
190
197
191
198
@_setup_sig
0 commit comments