gpt4 book ai didi

python - 如何从 sphinx 编译中获取警告列表

转载 作者:行者123 更新时间:2023-11-30 22:25:07 25 4
gpt4 key购买 nike

我正在开发一个基于 sphinx 的协作写作工具。用户访问Web应用程序(用python/Flask开发)在sphinx中写一本书并将其编译为pdf。

我了解到,为了从 python 中编译 sphinx 文档,我应该使用

import sphinx
result = sphinx.build_main(['-c', 'path/to/conf',
'path/to/source/', 'path/to/out'])

到目前为止一切顺利。

现在我的用户希望应用程序向他们显示语法错误。但输出(上面示例中的结果)只提供了退出代码。

那么,如何从构建过程中获取警告列表?

也许我太过雄心勃勃,但由于 sphinx 是一个 python 工具,我希望该工具有一个漂亮的 pythonic 界面。例如,sphinx.build_main 的输出可能是一个非常丰富的对象,包含警告、行号...

与此相关的是,sphinx.build_main 方法的参数看起来就像命令行界面的包装器。

最佳答案

sphinx.build_main() 调用 sphinx.cmdline.main(),这又创建一个 sphinx.application.Sphinx目的。您可以直接创建这样的对象(而不是“在 python 中进行系统调用”)。使用这样的东西:

import os
from sphinx.application import Sphinx

# Main arguments
srcdir = "/path/to/source"
confdir = srcdir
builddir = os.path.join(srcdir, "_build")
doctreedir = os.path.join(builddir, "doctrees")
builder = "html"

# Write warning messages to a file (instead of stderr)
warning = open("/path/to/warnings.txt", "w")

# Create the Sphinx application object
app = Sphinx(srcdir, confdir, builddir, doctreedir, builder,
warning=warning)

# Run the build
app.build()

关于python - 如何从 sphinx 编译中获取警告列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47624762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com