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

Aug*_* T. 4 python python-sphinx

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

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

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

到目前为止,一切都很好。

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

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

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

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

mzj*_*zjn 5

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()
Run Code Online (Sandbox Code Playgroud)