diff --git a/.ruff.toml b/.ruff.toml index c2b62f25b..cadfb76bd 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -417,7 +417,6 @@ exclude = [ "sphinx/domains/std/__init__.py", "sphinx/ext/inheritance_diagram.py", "sphinx/ext/linkcode.py", - "sphinx/ext/mathjax.py", "sphinx/ext/todo.py", "sphinx/ext/viewcode.py", ] diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index e9357fee6..71ed97c8e 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -31,10 +31,15 @@ logger = sphinx.util.logging.getLogger(__name__) def html_visit_math(self: HTML5Translator, node: nodes.math) -> None: - self.body.append(self.starttag(node, 'span', '', CLASS='math notranslate nohighlight')) - self.body.append(self.builder.config.mathjax_inline[0] + - self.encode(node.astext()) + - self.builder.config.mathjax_inline[1] + '') + self.body.append( + self.starttag(node, 'span', '', CLASS='math notranslate nohighlight') + ) + self.body.append( + self.builder.config.mathjax_inline[0] + + self.encode(node.astext()) + + self.builder.config.mathjax_inline[1] + + '' + ) raise nodes.SkipNode @@ -70,12 +75,16 @@ def html_visit_displaymath(self: HTML5Translator, node: nodes.math_block) -> Non raise nodes.SkipNode -def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict[str, Any], - event_arg: Any) -> None: - if ( - app.builder.format != 'html' or - app.builder.math_renderer_name != 'mathjax' # type: ignore[attr-defined] - ): +def install_mathjax( + app: Sphinx, + pagename: str, + templatename: str, + context: dict[str, Any], + event_arg: Any, +) -> None: + if app.builder.format != 'html': + return + if app.builder.math_renderer_name != 'mathjax': # type: ignore[attr-defined] return if not app.config.mathjax_path: msg = 'mathjax_path config value must be set for the mathjax extension to work' @@ -89,7 +98,8 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict if app.config.mathjax_path == MATHJAX_URL: logger.warning( 'mathjax_config/mathjax2_config does not work ' - 'for the current MathJax version, use mathjax3_config instead') + 'for the current MathJax version, use mathjax3_config instead' + ) body = 'MathJax.Hub.Config(%s)' % json.dumps(app.config.mathjax2_config) builder.add_js_file('', type='text/x-mathjax-config', body=body) if app.config.mathjax3_config: @@ -110,9 +120,11 @@ def install_mathjax(app: Sphinx, pagename: str, templatename: str, context: dict def setup(app: Sphinx) -> ExtensionMetadata: - app.add_html_math_renderer('mathjax', - (html_visit_math, None), - (html_visit_displaymath, None)) + app.add_html_math_renderer( + 'mathjax', + inline_renderers=(html_visit_math, None), + block_renderers=(html_visit_displaymath, None), + ) app.add_config_value('mathjax_path', MATHJAX_URL, 'html') app.add_config_value('mathjax_options', {}, 'html')