Markdown output for Sphinx based documentation [closed]

ぃ、小莉子 提交于 2021-02-04 09:19:08

问题


I found myself with a use case, where in addition to generating HTML and PDF from my Sphinx based documentation sources, I would also like to generate a Markdown version of the reStructuredText source files.

My preliminary research didn't find any core or extension support for this in Sphinx. Other than manually using pandoc or creating a new Sphinx extension for the task, is there be a simpler/more integrated solution for this?


回答1:


I didn't find anything which could take reStructuredText files and convert them to Markdown except for Pandoc, so I wrote a custom writer for Docutils (the reference implementation of reStructuredText and what Sphinx is built upon). The code is available on GitHub.

Note that it is only an initial implementation: it handles any reStructuredText document without error (tested against the standard.txt test document from the Docutils source repository) but many of the reStructuredText constructs (e.g. substitutions, raw directives etc.) are not supported and so not included in the Markdown output. I hope to add support for links, code blocks, images and tables: any help towards this is more than welcome - just go ahead and fork the code.

It seems that to add another writer/output format to Sphinx you need to add a "builder" using an extension.




回答2:


Update Nov'18: sphinx-markdown-builder is now available - thanks to @Jam Risser :

Installation

pip3 install sphinx-markdown-builder

Dependencies

Python 3

Usage

Load extension in configuration.

conf.py

extensions = [
    'sphinx_markdown_builder'
]

If using recommonmark, make sure you explicitly ignore the build files as they will conflict with the system.

conf.py

exclude_patterns = [
    'build/*'
]

Build markdown files with Makefile

make markdown

Build markdown files with sphinx-build command

cd docs
sphinx-build -M markdown ./ build

References

  • https://github.com/codejamninja/sphinx-markdown-builder/
  • https://pypi.org/project/sphinx-markdown-builder/

ps. Outdated original answer (since sphinx-markdown-builder is now available): Created feature request for direct Markdown output support on Sphinx project site: https://github.com/sphinx-doc/sphinx/issues/4219 Thanks everyone who upvoted that github request - that made the difference!




回答3:


If you'd like to use pandoc why won't you simply change the Makefile Sphinx generates when you run sphinx-quickstart.py for the first time to convert the reStructuredText to Markdown?
It's the easiest solution, although Chris's solution should work as well if you incorporate it into the Makefile.



来源:https://stackoverflow.com/questions/13396856/markdown-output-for-sphinx-based-documentation

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