From 55e560f0117feec0e4818519f56886d826335ad0 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Thu, 11 Apr 2024 00:48:50 -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 | 11 ++++++++--- 2 files changed, 12 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..5e82ac9a2 100644 --- a/sphinx_immaterial/search.py +++ b/sphinx_immaterial/search.py @@ -2,6 +2,7 @@ import multiprocessing import pathlib +from typing import Optional import docutils.nodes import jinja2.sandbox @@ -46,9 +47,13 @@ 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) + self._jinja2_env: Optional[jinja2.sandbox.SandboxedEnvironment] + 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