Skip to content

Commit

Permalink
non-intrusively disable search plugin for non-html output
Browse files Browse the repository at this point in the history
revert lint fixes to plugins/search/plugin.py

Fix spurious exception when another error has occurred.
  • Loading branch information
jbms committed Apr 11, 2024
1 parent acc1c32 commit 3b9aaf7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
6 changes: 2 additions & 4 deletions sphinx_immaterial/plugins/search/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 12 additions & 11 deletions sphinx_immaterial/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import multiprocessing
import pathlib
from typing import Optional

import docutils.nodes
import jinja2.sandbox
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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[:]:
Expand Down

0 comments on commit 3b9aaf7

Please sign in to comment.