How do I unparse restructured text back into an rst file?

Viewed 212

My question is a follow up to How to parse restructuredtext in python?

I am trying to make a tool that processes .rst files and formats the code blocks in them, then writes them back to the original files. @mbdevpl provided some code samples about how to parse the file into a tree and do some processing on the tree. Now that I've discovered the code blocks and changed their contents, how do I write back the changes into the original file?

1 Answers

Considering the lack of a good solution in my own searching, I would suggest a paragraph from https://docutils.sourceforge.io/docs/dev/hacking.html#parsing-the-document, which is linked in your previous question.

To get an HTML document out of the node tree, we use a Writer,
the HTML writer in this case (docutils/writers/html4css1.py).

The writer receives the node tree and returns the output document.
For HTML output, we can test this using the rst2html.py tool:

So you need a writer of some sort. One for html exists, and perhaps you could pipe that output to html2rst

Related