【问题标题】:Using rst2html5.py to get html body only使用 rst2html5.py 仅获取 html 正文
【发布时间】:2017-05-10 00:38:49
【问题描述】:

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 的主体,例如以便我可以插入另一个 html 模板?

我知道我可以做类似的事情

from docutils import core
text = open('foo.rst').read()
document = core.publish_parts(text, writer_name='html5')

然后得到document['body'],但这并不以MathJax方式处理数学指令

即我希望这个身体是

<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>

【问题讨论】:

    标签: python python-sphinx restructuredtext docutils


    【解决方案1】:

    首先,如果你使用 rst2html5:

    pip install rst2html5
    

    那么,就可以用这段代码来获取body了:

    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>
    

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 2012-08-17
      • 1970-01-01
      • 2012-06-16
      • 2016-02-15
      • 2011-04-21
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多