From 595c120135e38752dc95f600b34c489cec5f1562 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Thu, 23 Mar 2023 09:21:55 -0500 Subject: [PATCH 1/6] don't warn on fallback if no highlight theme requested --- src/pydata_sphinx_theme/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 2e039d85f..567664d6a 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -959,9 +959,11 @@ def _overwrite_pygments_css(app, exception=None): # make sure we can load the style if theme_name not in pygments_styles: - logger.warning( - f"Color theme {theme_name} not found by pygments, falling back to {fallback}." - ) + # only warn if user asked for a highlight theme that we can't find + if theme_name is not None: + logger.warning( + f"Color theme {theme_name} not found by pygments, falling back to {fallback}." + ) theme_name = fallback # assign to the appropriate variable if light_or_dark == "light": From 8cf196973a6238ad94bfc0171f30e052e42a663f Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Sun, 26 Mar 2023 17:53:44 -0500 Subject: [PATCH 2/6] fix it for good --- src/pydata_sphinx_theme/__init__.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 567664d6a..aefcb3105 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -951,12 +951,9 @@ def _overwrite_pygments_css(app, exception=None): # see if user specified a light/dark pygments theme, if not, use the # one we set in theme.conf style_key = f"pygment_{light_or_dark}_style" - - # globalcontext sometimes doesn't exist so this ensures we do not error theme_name = _get_theme_options(app).get(style_key, None) - if theme_name is None and hasattr(app.builder, "globalcontext"): - theme_name = app.builder.globalcontext.get(f"theme_{style_key}") - + if theme_name is None: + theme_name = app.builder.theme.get_options()[style_key] # make sure we can load the style if theme_name not in pygments_styles: # only warn if user asked for a highlight theme that we can't find @@ -1207,11 +1204,11 @@ def setup(app): app.connect("builder-inited", setup_translators) app.connect("builder-inited", update_config) + app.connect("builder-inited", _overwrite_pygments_css) app.connect("html-page-context", setup_edit_url) app.connect("html-page-context", add_toctree_functions) app.connect("html-page-context", update_and_remove_templates) app.connect("html-page-context", setup_logo_path) - app.connect("build-finished", _overwrite_pygments_css) app.connect("build-finished", copy_logo_images) # https://www.sphinx-doc.org/en/master/extdev/i18n.html#extension-internationalization-i18n-and-localization-l10n-using-i18n-api From dc303d92ac90f1df6704ba55cad57080d21d252d Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Sun, 26 Mar 2023 18:01:22 -0500 Subject: [PATCH 3/6] move event back to build-finished --- src/pydata_sphinx_theme/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index aefcb3105..f01f0ba5c 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -1204,11 +1204,11 @@ def setup(app): app.connect("builder-inited", setup_translators) app.connect("builder-inited", update_config) - app.connect("builder-inited", _overwrite_pygments_css) app.connect("html-page-context", setup_edit_url) app.connect("html-page-context", add_toctree_functions) app.connect("html-page-context", update_and_remove_templates) app.connect("html-page-context", setup_logo_path) + app.connect("build-finished", _overwrite_pygments_css) app.connect("build-finished", copy_logo_images) # https://www.sphinx-doc.org/en/master/extdev/i18n.html#extension-internationalization-i18n-and-localization-l10n-using-i18n-api From c6421dc3f497cfd47fd1022966fce194db2ef778 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 28 Mar 2023 11:48:47 -0500 Subject: [PATCH 4/6] better name, better doc --- src/pydata_sphinx_theme/__init__.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index f01f0ba5c..3104fe73c 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -32,19 +32,20 @@ logger = logging.getLogger(__name__) -def _get_theme_options(app): - """Return theme options for the application w/ a fallback if they don't exist. - - In general we want to modify app.builder.theme_options if it exists, so prefer that first. +def _get_theme_options_dict(app): + """Get the Sphinx theme options dictionary (or fallback to an empty dict). + + The "top-level" mapping (the one we should usually check first, and modify + if desired) is ``app.builder.theme_options``. It is created by Sphinx as a + copy of ``app.config.html_theme_options`` (containing user-configs from + their ``conf.py``); sometimes that copy never occurs though which is why we + check both. """ if hasattr(app.builder, "theme_options"): - # In most HTML build cases this will exist except for some circumstances (see below). return app.builder.theme_options elif hasattr(app.config, "html_theme_options"): - # For example, linkcheck will have this configured but won't be in builder obj. return app.config.html_theme_options else: - # Empty dictionary as a fail-safe. return {} @@ -59,7 +60,7 @@ def update_config(app): # At this point, modifying app.config.html_theme_options will NOT update the # page's HTML context (e.g. in jinja, `theme_keyword`). # To do this, you must manually modify `app.builder.theme_options`. - theme_options = _get_theme_options(app) + theme_options = _get_theme_options_dict(app) # TODO: deprecation; remove after 0.14 release if theme_options.get("logo_text"): @@ -951,7 +952,7 @@ def _overwrite_pygments_css(app, exception=None): # see if user specified a light/dark pygments theme, if not, use the # one we set in theme.conf style_key = f"pygment_{light_or_dark}_style" - theme_name = _get_theme_options(app).get(style_key, None) + theme_name = _get_theme_options_dict(app).get(style_key, None) if theme_name is None: theme_name = app.builder.theme.get_options()[style_key] # make sure we can load the style @@ -1169,7 +1170,7 @@ def copy_logo_images(app: Sphinx, exception=None) -> None: If logo image paths are given, copy them to the `_static` folder Then we can link to them directly in an html_page_context event """ - theme_options = _get_theme_options(app) + theme_options = _get_theme_options_dict(app) logo = theme_options.get("logo", {}) staticdir = Path(app.builder.outdir) / "_static" for kind in ["light", "dark"]: From f8253f2d2756bd859f4750ca68a9b4d5c41d21b9 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 28 Mar 2023 11:50:19 -0500 Subject: [PATCH 5/6] cleanup --- src/pydata_sphinx_theme/__init__.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index 3104fe73c..b24a17ad1 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -949,25 +949,26 @@ def _overwrite_pygments_css(app, exception=None): if fallback not in pygments_styles: fallback = pygments_styles[0] # should resolve to "default" - # see if user specified a light/dark pygments theme, if not, use the - # one we set in theme.conf + # see if user specified a light/dark pygments theme: style_key = f"pygment_{light_or_dark}_style" - theme_name = _get_theme_options_dict(app).get(style_key, None) - if theme_name is None: - theme_name = app.builder.theme.get_options()[style_key] + style_name = _get_theme_options_dict(app).get(style_key, None) + # if not, use the one we set in `theme.conf`: + if style_name is None and hasattr(app.builder, "theme"): + style_name = app.builder.theme.get_options()[style_key] # make sure we can load the style - if theme_name not in pygments_styles: + if style_name not in pygments_styles: # only warn if user asked for a highlight theme that we can't find - if theme_name is not None: + if style_name is not None: logger.warning( - f"Color theme {theme_name} not found by pygments, falling back to {fallback}." + f"Highlighting style {style_name} not found by pygments, " + f"falling back to {fallback}." ) - theme_name = fallback + style_name = fallback # assign to the appropriate variable if light_or_dark == "light": - light_theme = theme_name + light_theme = style_name else: - dark_theme = theme_name + dark_theme = style_name # re-write pygments.css pygment_css = Path(app.builder.outdir) / "_static" / "pygments.css" with pygment_css.open("w") as f: From 1c952b2a1dd704e2962d27d07a86defa6de271f0 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 28 Mar 2023 11:59:42 -0500 Subject: [PATCH 6/6] simplify/safer --- src/pydata_sphinx_theme/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pydata_sphinx_theme/__init__.py b/src/pydata_sphinx_theme/__init__.py index b24a17ad1..350dbd708 100644 --- a/src/pydata_sphinx_theme/__init__.py +++ b/src/pydata_sphinx_theme/__init__.py @@ -1171,8 +1171,7 @@ def copy_logo_images(app: Sphinx, exception=None) -> None: If logo image paths are given, copy them to the `_static` folder Then we can link to them directly in an html_page_context event """ - theme_options = _get_theme_options_dict(app) - logo = theme_options.get("logo", {}) + logo = _get_theme_options_dict(app).get("logo", {}) staticdir = Path(app.builder.outdir) / "_static" for kind in ["light", "dark"]: path_image = logo.get(f"image_{kind}")