【问题标题】:Python convert sphinx RST to HTMLPython 将 sphinx RST 转换为 HTML
【发布时间】:2017-09-13 13:14:24
【问题描述】:

我试过这段代码:

from docutils.core import publish_string
text = publish_string(open(file_path, 'r').read(), writer_name='html')

但它说:

<p>Unknown directive type &quot;toctree&quot;.</p>

所以它不适用于某些特定的 sphinx 指令。

对 sphinx RST 文件执行相同操作的最简单方法是什么?

更新。 好像应该是这样的:

sphinx-build -b singlehtml -D extensions='sphinx.ext.autodoc' -D master_doc='index' -C /mypath/docs .

如何从 Python 代码而不是控制台调用它?

【问题讨论】:

    标签: python-sphinx restructuredtext docutils


    【解决方案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')
    

    【讨论】:

      【解决方案2】:

      这是另一个例子:

      # 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'))
      
      

      更多参数>

      【讨论】:

        猜你喜欢
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-18
        • 2015-07-08
        • 1970-01-01
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多