使用 rst2html5.py 只得到 html body
Using rst2html5.py to get html body only
与
rst2html5.py foo.rst --math-output=MathJax > foo.html
其中 foo.rst
例如
The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.
.. math::
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
我找回了一个独立的 html 页面 foo.html
。如果我只想 html 的 body 怎么办?以便我可以插入另一个 html 模板?
我知道我可以做类似
from docutils import core
text = open('foo.rst').read()
document = core.publish_parts(text, writer_name='html5')
然后得到document['body']
,但这并没有以 MathJax 的方式处理数学指令
即我希望这个 body 成为
<p>The area of a circle is <span class="math">\(A_\text{c} = (\pi/4) d^2\)</span>.</p>
<div class="math">
\begin{equation*}
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
\end{equation*}
</div>
而不是通常的
<p>The area of a circle is <span class="formula"><i>A</i><sub><span class="text">c</span></sub> = (<i>π</i> ⁄ 4)<i>d</i><sup>2</sup></span>.</p>
<div class="formula">
<span class="fraction"><span class="ignored">(</span><span class="numerator"><span class="limits"><sup class="limit"><i>N</i></sup><span class="limit">⎲</span><span class="limit">⎳</span><sub class="limit"><i>t</i> = 0</sub></span><i>f</i>(<i>t</i>, <i>k</i>)</span><span class="ignored">)/(</span><span class="denominator"><i>N</i></span><span class="ignored">)</span></span>
</div>
首先,如果您使用 rst2html5:
pip install rst2html5
然后,您可以使用这段代码获取正文:
from docutils.core import publish_parts
from rst2html5_ import HTML5Writer
text = r'''
The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.
.. math::
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
'''
body = publish_parts(writer=HTML5Writer(), source=text)['body']
你会得到这个:
<p>The area of a circle is
<span class="math">\(A_ ext{c} = (\pi/4) d^2\)</span>
.</p>
<span class="math">\(\frac{ \sum_{t=0}^{N}f(t,k) }{N}\)</span>
与
rst2html5.py foo.rst --math-output=MathJax > foo.html
其中 foo.rst
例如
The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.
.. math::
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
我找回了一个独立的 html 页面 foo.html
。如果我只想 html 的 body 怎么办?以便我可以插入另一个 html 模板?
我知道我可以做类似
from docutils import core
text = open('foo.rst').read()
document = core.publish_parts(text, writer_name='html5')
然后得到document['body']
,但这并没有以 MathJax 的方式处理数学指令
即我希望这个 body 成为
<p>The area of a circle is <span class="math">\(A_\text{c} = (\pi/4) d^2\)</span>.</p>
<div class="math">
\begin{equation*}
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
\end{equation*}
</div>
而不是通常的
<p>The area of a circle is <span class="formula"><i>A</i><sub><span class="text">c</span></sub> = (<i>π</i> ⁄ 4)<i>d</i><sup>2</sup></span>.</p>
<div class="formula">
<span class="fraction"><span class="ignored">(</span><span class="numerator"><span class="limits"><sup class="limit"><i>N</i></sup><span class="limit">⎲</span><span class="limit">⎳</span><sub class="limit"><i>t</i> = 0</sub></span><i>f</i>(<i>t</i>, <i>k</i>)</span><span class="ignored">)/(</span><span class="denominator"><i>N</i></span><span class="ignored">)</span></span>
</div>
首先,如果您使用 rst2html5:
pip install rst2html5
然后,您可以使用这段代码获取正文:
from docutils.core import publish_parts
from rst2html5_ import HTML5Writer
text = r'''
The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.
.. math::
\frac{ \sum_{t=0}^{N}f(t,k) }{N}
'''
body = publish_parts(writer=HTML5Writer(), source=text)['body']
你会得到这个:
<p>The area of a circle is
<span class="math">\(A_ ext{c} = (\pi/4) d^2\)</span>
.</p>
<span class="math">\(\frac{ \sum_{t=0}^{N}f(t,k) }{N}\)</span>