因为我真的需要一个很好的解决方案来解决这个问题,所以我自己解决了。。。它给我带来了一段时间的工具,解决方案可能并不完美,但至少它工作得很好。我给出了结果,因为它对其他人有用。
我还将该解决方案改编为Sphinx扩展
here
.
我必须定义一个新的替代指令,并重新定义数学指令和角色。所有这些都在文件mathmacro.py中完成:
"""
'Proof of concept' for a new reStructuredText directive *mathmacro*.
Use for example with::
python mathmacro.py example.rst example.html
"""
import re
from docutils.parsers.rst.directives.misc import Replace
from docutils.parsers.rst.directives.body import MathBlock
from docutils.parsers.rst.roles import math_role
def multiple_replacer(replace_dict):
"""Return a function replacing doing multiple replacements.
The produced function replace `replace_dict.keys()` by
`replace_dict.values`, respectively.
"""
def replacement_function(match):
s = match.group(0)
end = s[-1]
if re.match(r'[\W_]', end):
return replace_dict[s[:-1]]+end
else:
return replace_dict[s]
pattern = re.compile("|".join([re.escape(k)+r'[\W_\Z]|'+re.escape(k)+r'\Z'
for k in replace_dict.keys()]),
re.M)
return lambda string: pattern.sub(replacement_function, string)
class MathMacro(Replace):
"""Directive defining a math macro."""
def run(self):
if not hasattr(self.state.document, 'math_macros'):
self.state.document.math_macros = {}
latex_key = '\\'+self.state.parent.rawsource.split('|')[1]
self.state.document.math_macros[latex_key] = ''.join(self.content)
self.state.document.math_macros_replace = \
multiple_replacer(self.state.document.math_macros)
self.content[0] = ':math:`'+self.content[0]
self.content[-1] = self.content[-1]+'`'
return super(MathMacro, self).run()
class NewMathBlock(MathBlock):
"""New math block directive parsing the latex code."""
def run(self):
try:
multiple_replace = self.state.document.math_macros_replace
except AttributeError:
pass
else:
if self.state.document.math_macros:
for i, c in enumerate(self.content):
self.content[i] = multiple_replace(c)
return super(NewMathBlock, self).run()
def new_math_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
"""New math role parsing the latex code."""
try:
multiple_replace = inliner.document.math_macros_replace
except AttributeError:
pass
else:
if inliner.document.math_macros:
rawtext = multiple_replace(rawtext)
return math_role(role, rawtext, text, lineno, inliner,
options=options, content=content)
if __name__ == '__main__':
from docutils.parsers.rst.directives import register_directive
from docutils.parsers.rst.roles import register_canonical_role
register_directive('mathmacro', MathMacro)
register_directive('math', NewMathBlock)
register_canonical_role('math', new_math_role)
from docutils.core import publish_cmdline, default_description
description = ('Generates (X)HTML documents '
'from standalone reStructuredText sources. '
+default_description)
publish_cmdline(writer_name='html', description=description)
文件example.rst的内容:
Here, I show how to use a new mathmacro substitution directive in
reStructuredText. I think even this small example demonstrates that it
is useful.
First some math without math macros. Let's take the example of the
incompressible Navier-Stokes equation:
.. math:: \mbox{D}_t \textbf{v} =
-\boldsymbol{\nabla} p + \nu \boldsymbol{\nabla} ^2 \textbf{v}.
where :math:`\mbox{D}_t` is the convective derivative,
:math:`\textbf{v}` the velocity, :math:`\boldsymbol{\nabla}` the
nabla operator, :math:`\nu` the viscosity and
:math:`\boldsymbol{\nabla}^2` the Laplacian operator.
.. |Dt| mathmacro:: \mbox{D}_t
.. |bnabla| mathmacro:: \boldsymbol{\nabla}
.. |vv| mathmacro:: \textbf{v}
Now, let's use some math macros and try to get the same result... The
Navier-Stokes equation can now be written like this:
.. math:: \Dt \vv = - \bnabla p + \nu \bnabla^2 \vv
where |Dt| is the convective derivative, |vv| the velocity, |bnabla|
the nabla operator, :math:`\nu` the viscosity and :math:`\bnabla^2`
the Laplacian operator.