Python convert sphinx RST to HTML

不问归期 提交于 2021-02-10 02:47:49

问题


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 &quot;toctree&quot;.</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!