"""Overrides to ``sphinx.ext.mathjax``This fixes two issues:1. Mathjax should not search for ``$`` delimiters, nor LaTeX amsmath environments, since we already achieve this with the dollarmath and amsmath mrakdown-it-py plugins2. amsmath math blocks should be wrapped in mathjax delimiters (default ``\\[...\\]``), and assigned an equation number"""fromdocutilsimportnodesfromsphinx.applicationimportSphinxfromsphinx.extimportmathjaxfromsphinx.localeimport_fromsphinx.utilimportloggingfromsphinx.util.mathimportget_node_equation_numberfromsphinx.writers.htmlimportHTMLTranslatorlogger=logging.getLogger(__name__)
[文档]deflog_override_warning(app:Sphinx,version:int,current:str,new:str)->None:"""Log a warning if MathJax configuration being overridden."""iflogging.is_suppressed_warning("myst","mathjax",app.config.suppress_warnings):returnconfig_name=("mathjax3_config['options']['processHtmlClass']"ifversion==3else"mathjax_config['tex2jax']['processClass']")logger.warning(f"`{config_name}` is being overridden by myst-parser: '{current}' -> '{new}'. ""Set `suppress_warnings=['myst.mathjax']` to ignore this warning, or ""`myst_update_mathjax=False` if this is undesirable.")
[文档]defoverride_mathjax(app:Sphinx):"""Override aspects of the mathjax extension. MyST-Parser parses dollar and latex math, via markdown-it plugins. Therefore, we tell Mathjax to only render these HTML elements. This is accompanied by setting the `ignoreClass` on the top-level section of each MyST document. """if("amsmath"inapp.config["myst_enable_extensions"]and"mathjax"inapp.registry.html_block_math_renderers):app.registry.html_block_math_renderers["mathjax"]=(html_visit_displaymath,None,)if"dollarmath"notinapp.config["myst_enable_extensions"]:returnifnotapp.env.myst_config.update_mathjax:# type: ignore[attr-defined]returnmjax_classes=app.env.myst_config.mathjax_classes# type: ignore[attr-defined]if"mathjax3_config"inapp.config:# sphinx 4 + mathjax 3app.config.mathjax3_config=app.config.mathjax3_configor{}app.config.mathjax3_config.setdefault("options",{})if("processHtmlClass"inapp.config.mathjax3_config["options"]andapp.config.mathjax3_config["options"]["processHtmlClass"]!=mjax_classes):log_override_warning(app,3,app.config.mathjax3_config["options"]["processHtmlClass"],mjax_classes,)app.config.mathjax3_config["options"]["processHtmlClass"]=mjax_classeselif"mathjax_config"inapp.config:# sphinx 3 + mathjax 2app.config.mathjax_config=app.config.mathjax_configor{}app.config.mathjax_config.setdefault("tex2jax",{})if("processClass"inapp.config.mathjax_config["tex2jax"]andapp.config.mathjax_config["tex2jax"]["processClass"]!=mjax_classes):log_override_warning(app,2,app.config.mathjax_config["tex2jax"]["processClass"],mjax_classes,)app.config.mathjax_config["tex2jax"]["processClass"]=mjax_classes
[文档]defhtml_visit_displaymath(self:HTMLTranslator,node:nodes.math_block)->None:"""Override for sphinx.ext.mathjax.html_visit_displaymath to handle amsmath. By default displaymath, are normally wrapped in a prefix/suffix, defined by mathjax_display, and labelled nodes are numbered. However, this is not the case if the math_block is set as 'nowrap', as for amsmath. Therefore, we need to override this behaviour. """if"amsmath"innode.get("classes",[]):self.body.append(self.starttag(node,"div",CLASS="math notranslate nohighlight amsmath"))ifnode["number"]:number=get_node_equation_number(self,node)self.body.append(f'<span class="eqno">({number})')self.add_permalink_ref(node,_("Permalink to this equation"))self.body.append("</span>")prefix,suffix=self.builder.config.mathjax_displayself.body.append(prefix)self.body.append(self.encode(node.astext()))self.body.append(suffix)self.body.append("</div>\n")raisenodes.SkipNodereturnmathjax.html_visit_displaymath(self,node)