-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for html_math_renderer = 'mathml' #251
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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 = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be extended to user confval(s)? Does this directly influence the docutils' converter used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is basically equivalent to putting:
in a There is one option that could be specified, an external LaTeX-syntax to MathML converter program to use in place of the one that is built into docutils. Probably there should be a config option for that. Alternatively this extension could be renamed "docutils_math_renderer" and we could require the user to create the separate docutils.conf file to specify the math format. However, it seems unlikely that a user would want to use any of the docutils math formats other than mathml:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really just working with the info from here. I think it would be fine to only use the docutils converter script (latex2mathml). But I was thinking it might be useful if a user already has external converter installed (like pandoc or blahxml) and might want to use that instead. |
||
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, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,3 +85,4 @@ | |
|
||
@import "main/sphinx"; | ||
@import "main/api"; | ||
@import "main/mathml"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
math .boldsymbol { | ||
font-weight: bold; | ||
} | ||
|
||
mtable[columnalign="right left"] { | ||
> mtr { | ||
> mtd:nth-child(1) { | ||
text-align: -webkit-right; | ||
} | ||
> mtd:nth-child(2) { | ||
text-align: -webkit-left; | ||
} | ||
} | ||
} | ||
|
||
mtable[columnalign="right left right"] { | ||
> mtr { | ||
> mtd:nth-child(1) { | ||
text-align: -webkit-right; | ||
} | ||
> mtd:nth-child(2) { | ||
text-align: -webkit-left; | ||
} | ||
> mtd:nth-child(3) { | ||
text-align: -webkit-right; | ||
} | ||
} | ||
} | ||
|
||
mtable[columnspacing="0 2em"] { | ||
> mtr { | ||
> mtd:nth-child(1) { | ||
padding-left: 0px; | ||
padding-right: 0px; | ||
} | ||
> mtd:nth-child(2) { | ||
padding-left: 0px; | ||
padding-right: 2em; | ||
} | ||
} | ||
} | ||
|
||
mtable[columnspacing="0"] { | ||
> mtr { | ||
> mtd { | ||
padding-left: 0px; | ||
padding-right: 0px; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth noting when the escaped line breaks are needed/useful in a doc dedicated to this theme-ext