我试过这段代码:
from docutils.core import publish_string
text = publish_string(open(file_path, 'r').read(), writer_name='html')
但它说:
<p>Unknown directive type "toctree".</p>
因此它不适用于某些特定的sphinx指令。
为sphinx RST文件执行相同操作的最简单方法是什么?
UPD。 似乎必须是:
sphinx-build -b singlehtml -D extensions='sphinx.ext.autodoc' -D master_doc='index' -C /mypath/docs .
如何从Python代码而不是控制台调用它?
答案 0 :(得分:1)
这就是我想要做的事情:
import sphinx
args = ". -b singlehtml -D extensions=sphinx.ext.autodoc -D master_doc=index -C /tmp/doc /tmp/out"
sphinx.main(args.split())
result = open('/tmp/out/index.html', 'r')
答案 1 :(得分:0)
这是另一个示例:
# sphinx version = 2.3.1
# Python version = 3.7.3
from sphinx.cmd.build import main as sphinx_main
from pathlib import Path
from os import startfile
import sys
master_doc = 'index'
source_suffix = '.rst'
output_file = 'tmp/dist'
html_theme = 'nature'
build_format = 'html' # singlehtml, ...
args = f". -b {build_format} -D extensions=sphinx.ext.autodoc " \
f"-D master_doc={master_doc} " \
f"-D source_suffix={source_suffix} " \
f"-D html_theme={html_theme} " \
f"-C {output_file} "
sys.path.append(str(Path('.').absolute()))
sphinx_main(args.split())
startfile(Path(output_file).joinpath(master_doc+'.html'))
更多参数>