完整python proj上的rst2html

2024-06-16 12:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将rst2html配置为在完整的Python项目上而不是在单个文件上运行?在

我习惯于epydoc生成我的文档,但是我想使用restructedText,因为它是PyCharm的默认值(我知道我们也可以将PyCharm更改为epytext)


Tags: 文件项目文档restructedtextpycharmepydocepytextrst2html
1条回答
网友
1楼 · 发布于 2024-06-16 12:22:37

rst2html本身无法做到这一点。它是docutils.core.publish_cmdline上的一个可笑的薄包装。检查它的来源:

https://github.com/docutils-mirror/docutils/blob/master/tools/rst2html.py

如果要处理多个.rst文件,可以编写一个简短的shell脚本,在每个文件上调用rst2html。在

或者,您可以编写Python脚本。下面是一个例子:

# https://docs.python.org/3/library/pathlib.html
from pathlib import Path

# https://pypi.org/project/docutils/
import docutils.io, docutils.core

def rst2html(source_path):
    # mostly taken from
    # https://github.com/getpelican/pelican/
    pub = docutils.core.Publisher(
        source_class=docutils.io.FileInput,
        destination_class=docutils.io.StringOutput)
    pub.set_components('standalone', 'restructuredtext', 'html')
    pub.process_programmatic_settings(None, None, None)
    pub.set_source(source_path=str(source_path))
    pub.publish()

    html = pub.writer.parts['whole']

    return html

SRC_DIR = Path('.')
DST_DIR = Path('.')

for rst_file in SRC_DIR.iterdir():
    if rst_file.is_file() and rst_file.suffix == '.rst':

        html = rst2html(rst_file)

        with open(DST_DIR / (rst_file.stem + '.html'), 'w') as f:
            f.write(html)

一个有用的参考可以是:

http://docutils.sourceforge.net/docs/api/publisher.html

相关问题 更多 >