Say I have some reStructuredText source in a string
source = """
============
Introduction
============
Hello world.
.. code-block:: bash
$ echo Greetings.
"""
import sys
import docutils.nodes
import docutils.parsers.rst
import docutils.utils
import sphinx.writers.text
import sphinx.builders.text
def parse_rst(text: str) -> docutils.nodes.document:
parser = docutils.parsers.rst.Parser()
components = (docutils.parsers.rst.Parser,)
settings = docutils.frontend.OptionParser(components=components).get_default_values()
document = docutils.utils.new_document('<rst-doc>', settings=settings)
parser.parse(text, document)
return document
if __name__ == '__main__':
document = parse_rst(source)
I'd like to convert it into plain text without the reST markup using Python.
I tried to use sphinx.builders.text.TextBuilder
but it seems to want an App
object, not a string.
- Here is a related question about doing it manually on the command-line with files instead of strings.
- Parsing code comes from this answer