使用 docutils 解析和编写 RST
Parse and write RST using docutils
是否有可用的编写器可以输出 reStructuredText?
我认为 "null" writer 可能是为了这个目的,但它没有产生任何输出。
我的用例是解析现有的 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)
不是一个令人满意的答案,但与此同时我求助于使用正则表达式 - 如 gist
它为文件夹中的所有文件添加了对索引指令的名称引用:
输入:
.. index::
pair: CLASS; BACKGROUNDCOLOR
输出:
.. index::
pair: CLASS; BACKGROUNDCOLOR
:name: mapfile-class-background
是的,一个名为 sphinxcontrib.restbuilder
的 Sphinx 扩展程序位于 https://github.com/sphinx-contrib/restbuilder
免责声明:我在 2014 年开始使用此代码,但直到最近它才开始变得可靠。非常感谢您的贡献。
是否有可用的编写器可以输出 reStructuredText? 我认为 "null" writer 可能是为了这个目的,但它没有产生任何输出。
我的用例是解析现有的 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)
不是一个令人满意的答案,但与此同时我求助于使用正则表达式 - 如 gist
它为文件夹中的所有文件添加了对索引指令的名称引用:
输入:
.. index::
pair: CLASS; BACKGROUNDCOLOR
输出:
.. index::
pair: CLASS; BACKGROUNDCOLOR
:name: mapfile-class-background
是的,一个名为 sphinxcontrib.restbuilder
的 Sphinx 扩展程序位于 https://github.com/sphinx-contrib/restbuilder
免责声明:我在 2014 年开始使用此代码,但直到最近它才开始变得可靠。非常感谢您的贡献。