Enable automatic formatting for `sphinx/ext/mathjax.py`

This commit is contained in:
Adam Turner 2024-12-25 19:21:48 +00:00
parent da5a67d5a4
commit 7b9a431f31
2 changed files with 26 additions and 15 deletions

View File

@ -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",
]

View File

@ -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] + '</span>')
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]
+ '</span>'
)
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')