问题
I have tried this code:
from docutils.core import publish_string
text = publish_string(open(file_path, 'r').read(), writer_name='html')
But it says:
<p>Unknown directive type "toctree".</p>
So it won't work with some specific sphinx directives.
What is the easiest way to do same stuff for sphinx RST files?
upd. Seems like it must be:
sphinx-build -b singlehtml -D extensions='sphinx.ext.autodoc' -D master_doc='index' -C /mypath/docs .
How can I call that from Python code instead of console?
回答1:
Here is what i wanted to do:
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')
回答2:
here is another example:
# 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'))
more parameters >
- https://www.sphinx-doc.org/en/master/man/sphinx-build.html
- https://www.sphinx-doc.org/en/master/usage/configuration.html
来源:https://stackoverflow.com/questions/46198604/python-convert-sphinx-rst-to-html