From fd2bf6a7349c984c104770f3c008de913666deae Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Thu, 11 Apr 2024 00:45:48 -0700 Subject: [PATCH] let search plugin fail silently for non-html builders Maybe there's a more elegant way to approach this, but I'm just hacking this into something passable for CI --- sphinx_immaterial/plugins/search/plugin.py | 6 ++++-- sphinx_immaterial/search.py | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/sphinx_immaterial/plugins/search/plugin.py b/sphinx_immaterial/plugins/search/plugin.py index 98caa0028..78d856428 100644 --- a/sphinx_immaterial/plugins/search/plugin.py +++ b/sphinx_immaterial/plugins/search/plugin.py @@ -143,6 +143,8 @@ def on_serve(self, server, *, config, builder): # Translate the given placeholder value def _translate(self, config, value): env = config.theme.get_env() + if env is None: # not using a HTML builder + return value # Load language template and return translation for placeholder language = "partials/language.html" @@ -406,7 +408,7 @@ def handle_starttag(self, tag, attrs): # Ignore self-closing tags el = Element(tag, attrs) - if not tag in void: + if tag not in void: self.context.append(el) else: return @@ -509,7 +511,7 @@ def handle_data(self, data): return # Collapse whitespace in non-pre contexts - if not "pre" in self.context: + if "pre" not in self.context: if not data.isspace(): data = data.replace("\n", " ") else: diff --git a/sphinx_immaterial/search.py b/sphinx_immaterial/search.py index acdfcaae8..f1615f630 100644 --- a/sphinx_immaterial/search.py +++ b/sphinx_immaterial/search.py @@ -46,9 +46,12 @@ class _Theme: def __init__(self, app: sphinx.application.Sphinx): self._app = app builder = self._app.builder - assert isinstance(builder, sphinx.builders.html.StandaloneHTMLBuilder) - self._jinja2_env = jinja2.sandbox.SandboxedEnvironment(loader=builder.templates) - self._jinja2_env.globals.update(builder.globalcontext) + if isinstance(builder, sphinx.builders.html.StandaloneHTMLBuilder): + # only useful if using HTML output + self._jinja2_env = jinja2.sandbox.SandboxedEnvironment(loader=builder.templates) + self._jinja2_env.globals.update(builder.globalcontext) + else: + self._jinja2_env = None def get_env(self): return self._jinja2_env