Skip to content

Commit 686a27a

Browse files
authored
FIX: Get theme options in a more robust way (#1214)
1 parent 5ab7536 commit 686a27a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/pydata_sphinx_theme/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
logger = logging.getLogger(__name__)
3333

3434

35+
def _get_theme_options(app):
36+
"""Return theme options for the application w/ a fallback if they don't exist.
37+
38+
In general we want to modify app.builder.theme_options if it exists, so prefer that first.
39+
"""
40+
if hasattr(app.builder, "theme_options"):
41+
# In most HTML build cases this will exist except for some circumstances (see below).
42+
return app.builder.theme_options
43+
elif hasattr(app.config, "html_theme_options"):
44+
# For example, linkcheck will have this configured but won't be in builder obj.
45+
return app.config.html_theme_options
46+
else:
47+
# Empty dictionary as a fail-safe.
48+
return {}
49+
50+
3551
def _config_provided_by_user(app, key):
3652
"""Check if the user has manually provided the config."""
3753
return any(key in ii for ii in [app.config.overrides, app.config._raw_config])
@@ -43,7 +59,7 @@ def update_config(app):
4359
# At this point, modifying app.config.html_theme_options will NOT update the
4460
# page's HTML context (e.g. in jinja, `theme_keyword`).
4561
# To do this, you must manually modify `app.builder.theme_options`.
46-
theme_options = app.builder.theme_options
62+
theme_options = _get_theme_options(app)
4763

4864
# TODO: deprecation; remove after 0.14 release
4965
if theme_options.get("logo_text"):
@@ -926,7 +942,7 @@ def _overwrite_pygments_css(app, exception=None):
926942
style_key = f"pygment_{light_or_dark}_style"
927943

928944
# globalcontext sometimes doesn't exist so this ensures we do not error
929-
theme_name = app.builder.theme_options.get(style_key, None)
945+
theme_name = _get_theme_options(app).get(style_key, None)
930946
if theme_name is None and hasattr(app.builder, "globalcontext"):
931947
theme_name = app.builder.globalcontext.get(f"theme_{style_key}")
932948

@@ -1143,7 +1159,7 @@ def copy_logo_images(app: Sphinx, exception=None) -> None:
11431159
If logo image paths are given, copy them to the `_static` folder
11441160
Then we can link to them directly in an html_page_context event
11451161
"""
1146-
theme_options = app.builder.theme_options
1162+
theme_options = _get_theme_options(app)
11471163
logo = theme_options.get("logo", {})
11481164
staticdir = Path(app.builder.outdir) / "_static"
11491165
for kind in ["light", "dark"]:

0 commit comments

Comments
 (0)