我正在开发基于sphinx的协作式写作工具。用户访问Web应用程序(在python / Flask中开发)以在sphinx中编写书籍并将其编译为pdf。
我已经了解到为了从python中编译sphinx文档,我应该使用
import sphinx
result = sphinx.build_main(['-c', 'path/to/conf',
'path/to/source/', 'path/to/out'])
到目前为止一切顺利。
现在我的用户希望该应用向他们展示他们的语法错误。但是输出(上例中的result
)只给出了退出代码。
那么,如何从构建过程中获取警告列表?
也许我过于雄心勃勃,但由于sphinx是一个python工具,我期待与该工具有一个很好的pythonic界面。例如,sphinx.build_main
的输出可能是一个非常丰富的对象,带有警告,行号......
在相关的说明中,方法sphinx.build_main
的参数看起来就像是命令行界面的包装。
答案 0 :(得分:1)
假设您使用sphinx-quickstart
生成带有makefile的初始Sphinx文档集,那么您可以使用make
构建文档,然后使用Sphinx工具{{3} }。您可以sphinx-build
将警告和错误写入文件以及stderr
。
请注意,通过命令行传递的选项会覆盖makefile和conf.py
中设置的任何其他选项。
答案 1 :(得分:1)
sphinx.build_main()
调用sphinx.cmdline.main()
,后者又会创建一个sphinx.application.Sphinx
对象。您可以直接创建这样的对象(而不是"在python&#34中进行系统调用;)。使用这样的东西:
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()