使用 docutils 将 scipy 文档字符串 (reST) 转换为 HTML

Convert scipy docstrings (reST) to HTML using docutils

我正在设计过滤器设计 GUI,并希望在 QTextBrowser 中显示来自 Python 的 scipy.signal 的文档字符串,需要 HTML 格式。我认为 docutils 应该为我完成这项工作,我试过了

from docutils.core import publish_string
from scipy.signal import remez
self.txtFiltInfoBox.append(publish_string(remez.__doc__,
          writer_name='html'))

其中 txtFiltInfoBox 是一个 QTextBrowser 实例。但是,publish_string 在文档字符串中遇到的第一个标题 ("Parameters") 上卡住了:

docutils.utils.SystemMessage: <string>:10: (SEVERE/4) Unexpected section title.

Parameters
----------

我认为原因是该方法的整个文档字符串被缩进,导致无效的 reST 标记。有没有一种简单的方法来减少文档字符串或告诉 publish_string 忽略一定数量的前导空格?

可在 pyFDA project 找到完整代码。

您可以使用 textwrap.dedent 来,正如文档所说:

Remove any common leading whitespace from every line in text.

例如(来自a similar question on CodeReview):

>>> import textwrap
>>> print(textwrap.dedent(
        """
        Usage examples:
        Test deployment:
            $ fab [noinput] test deploy
        Staging deployment:
            $ fab [noinput] staging deploy
        Production deployment:
            $ fab [noinput] production deploy
        """
))

Usage examples:
Test deployment:
    $ fab [noinput] test deploy
Staging deployment:
    $ fab [noinput] staging deploy
Production deployment:
    $ fab [noinput] production deploy