From 3b9aaf73c76bf2cf273d3f93937a02f663f06f9f Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Thu, 11 Apr 2024 09:43:41 -0700 Subject: [PATCH] non-intrusively disable search plugin for non-html output revert lint fixes to plugins/search/plugin.py Fix spurious exception when another error has occurred. --- sphinx_immaterial/plugins/search/plugin.py | 6 ++---- sphinx_immaterial/search.py | 23 +++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/sphinx_immaterial/plugins/search/plugin.py b/sphinx_immaterial/plugins/search/plugin.py index 78d856428..98caa0028 100644 --- a/sphinx_immaterial/plugins/search/plugin.py +++ b/sphinx_immaterial/plugins/search/plugin.py @@ -143,8 +143,6 @@ 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" @@ -408,7 +406,7 @@ def handle_starttag(self, tag, attrs): # Ignore self-closing tags el = Element(tag, attrs) - if tag not in void: + if not tag in void: self.context.append(el) else: return @@ -511,7 +509,7 @@ def handle_data(self, data): return # Collapse whitespace in non-pre contexts - if "pre" not in self.context: + if not "pre" 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 1275b63f8..ed697d6af 100644 --- a/sphinx_immaterial/search.py +++ b/sphinx_immaterial/search.py @@ -2,7 +2,6 @@ import multiprocessing import pathlib -from typing import Optional import docutils.nodes import jinja2.sandbox @@ -47,15 +46,9 @@ class _Theme: def __init__(self, app: sphinx.application.Sphinx): self._app = app builder = self._app.builder - 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 + assert isinstance(builder, sphinx.builders.html.StandaloneHTMLBuilder) + self._jinja2_env = jinja2.sandbox.SandboxedEnvironment(loader=builder.templates) + self._jinja2_env.globals.update(builder.globalcontext) def get_env(self): return self._jinja2_env @@ -91,7 +84,15 @@ def _html_page_context( queue.append(indexer.entries) -def _build_finished(app: sphinx.application.Sphinx, exception) -> None: +def _build_finished(app: sphinx.application.Sphinx, exc) -> None: + if exc: + # Skip generating search index if an error occurred. + return + + # Only applies to HTML builder. + if not isinstance(app.builder, sphinx.builders.html.StandaloneHTMLBuilder): + return + queue = getattr(app, _SEARCH_QUEUE_KEY) indexer = _make_indexer(app) for entries in queue[:]: