Skip to content

Commit

Permalink
let search plugin fail silently for non-html builders
Browse files Browse the repository at this point in the history
Maybe there's a more elegant way to approach this, but I'm just hacking this into something passable for CI
  • Loading branch information
2bndy5 committed Apr 11, 2024
1 parent a8d5819 commit 55e560f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 4 additions & 2 deletions sphinx_immaterial/plugins/search/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions sphinx_immaterial/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import multiprocessing
import pathlib
from typing import Optional

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

0 comments on commit 55e560f

Please sign in to comment.