【问题标题】:Convert scipy docstrings (reST) to HTML using docutils使用 docutils 将 scipy 文档字符串(reST)转换为 HTML
【发布时间】:2015-02-24 10:12:01
【问题描述】:

我正在设计一个过滤器设计 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 在它在文档字符串中遇到的第一个标题(“参数”)上阻塞:

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

Parameters
----------

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

完整代码可以在pyFDA project找到。

【问题讨论】:

    标签: python html restructuredtext docstring docutils


    【解决方案1】:

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

    从 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
    

    【讨论】:

    • 谢谢 - 这真的很棒!又一个我不知道的 Python 模块......很抱歉没有足够的声誉来投票给你。
    猜你喜欢
    • 2015-11-16
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多