-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for html_math_renderer = 'mathml'
Fixes #233.
- Loading branch information
Showing
4 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Extension that enables docutils' MathML-based math rendering.""" | ||
|
||
import docutils.nodes | ||
import docutils.utils.math | ||
from docutils.writers._html_base import HTMLTranslator as BaseHTMLTranslator | ||
from sphinx.application import Sphinx | ||
|
||
# Sphinx overrides Docutils' math rendering. Here, we override Sphinx's | ||
# override to revert to docutils' math rendering. | ||
|
||
|
||
def visit_math(self: BaseHTMLTranslator, node: docutils.nodes.math): | ||
self.math_output = "mathml" | ||
self.math_output_options = [] | ||
BaseHTMLTranslator.visit_math(self, node) | ||
|
||
|
||
def visit_math_block(self: BaseHTMLTranslator, node: docutils.nodes.math_block): | ||
self.math_output = "mathml" | ||
self.math_output_options = [] | ||
# Note: We can't call `BaseHTMLTranslator.visit_math_block` here, because | ||
# that just forwards to `self.visit_math`, which ultimately calls back into | ||
# our `visit_math` function defined above but without the `math_env` | ||
# argument. | ||
BaseHTMLTranslator.visit_math( | ||
self, node, math_env=docutils.utils.math.pick_math_environment(node.astext()) | ||
) | ||
|
||
|
||
def setup(app: Sphinx): | ||
"""Setup the extension.""" | ||
app.add_html_math_renderer( | ||
"mathml", | ||
(visit_math, None), # type: ignore[arg-type] | ||
(visit_math_block, None), # type: ignore[arg-type] | ||
) | ||
return { | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |