【问题标题】:Parse and write RST using docutils使用 docutils 解析和编写 RST
【发布时间】:2020-05-07 20:07:18
【问题描述】:

是否有任何可用的编写器可以输出 reStructuredText? 我认为“空”作家可能是为此目的,但它不会产生任何输出。

我的用例是解析现有的 RST 文件,在 Python 中修改文档树(例如自动更新指令),然后将树输出回 RST。

HTML 可以输出如下。对于 RST 输出,我需要编写自定义 Writer 来执行此操作吗?

    import docutils.core
    import docutils.parsers.rst
    from docutils.writers import null
    import docutils.writers.html5_polyglot

    txt = """
    Title
    =====

    .. meta::
       :description: The reStructuredText plaintext markup language
       :keywords: plaintext, markup language

    """

    output = docutils.core.publish_string(
                source=txt,
                parser=docutils.parsers.rst.Parser(),
                #writer_name="null", # docutils_xml
                #writer= docutils.writers.null.Writer()
                writer=docutils.writers.html5_polyglot.Writer()
                )

    print(output)

【问题讨论】:

    标签: python restructuredtext docutils


    【解决方案1】:

    是的,Sphinx 扩展名为 sphinxcontrib.restbuilder https://github.com/sphinx-contrib/restbuilder

    免责声明:我在 2014 年开始编写此代码,但直到最近它才开始变得可靠。非常感谢您的贡献。

    【讨论】:

      【解决方案2】:

      不是一个令人满意的答案,但与此同时我使用了正则表达式 - 就像在这个 gist

      它为文件夹中的所有文件添加对索引指令的名称引用:

      输入:

      .. index::
          pair: CLASS; BACKGROUNDCOLOR
      

      输出:

      .. index::
          pair: CLASS; BACKGROUNDCOLOR
          :name: mapfile-class-background
      

      这个拉取请求的用例是:https://github.com/mapserver/docs/pull/327

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 2020-10-30
        • 1970-01-01
        相关资源
        最近更新 更多