Skip to content

Commit ec55c24

Browse files
authored
Fix book theme options (#80)
1 parent 2e29930 commit ec55c24

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

.github/workflows/publish.yml

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
permissions:
1111
contents: read
1212

13+
env:
14+
PIP_ROOT_USER_ACTION: ignore
15+
1316
jobs:
1417
deploy:
1518
runs-on: ubuntu-latest

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
add_module_names = False
4545

4646
html_theme = "scanpydoc"
47-
html_context = dict(
47+
html_theme_options = dict(
4848
repository_url="https://github.com/theislab/scanpydoc",
4949
repository_branch="main",
5050
use_repository_button=True,

src/scanpydoc/rtd_github_links/__init__.py

+31-24
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
rtd_links_prefix: os.PathLike | str = ... # default: '.'
2424
2525
# sphinx book theme style
26-
html_context = dict(
26+
html_theme_options = dict(
2727
repository_url=...,
2828
repository_branch=...,
2929
)
@@ -37,7 +37,7 @@
3737
The ``rtd_links_prefix`` is for figuring out the .py file path relative to the git root,
3838
that is to construct the path in the GitHub URL.
3939
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.
4141
:doc:`Sphinx Book Theme <sphinx_book_theme:index>`.
4242
4343
``:github_url:`` usage
@@ -79,14 +79,14 @@
7979
def _init_vars(app: Sphinx, config: Config):
8080
"""Called when ``conf.py`` has been loaded."""
8181
global github_base_url, rtd_links_prefix
82-
_check_html_context(config)
82+
_check_html_config(config)
8383
try:
8484
github_base_url = "https://github.com/{github_user}/{github_repo}/tree/{github_version}".format_map(
8585
config.html_context
8686
)
8787
except KeyError:
8888
github_base_url = "{repository_url}/tree/{repository_branch}".format_map(
89-
config.html_context
89+
config.html_theme_options
9090
)
9191
rtd_links_prefix = PurePosixPath(config.rtd_links_prefix)
9292

@@ -153,7 +153,8 @@ def github_url(qualname: str) -> str:
153153
qualname: The full qualified name of a function, class, method or module
154154
155155
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`.
157158
"""
158159
try:
159160
obj, module = _get_obj_module(qualname)
@@ -166,26 +167,32 @@ def github_url(qualname: str) -> str:
166167
return f"{github_base_url}/{path}{fragment}"
167168

168169

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"
188179
)
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)
189196

190197

191198
@_setup_sig

0 commit comments

Comments
 (0)