2

My question is a follow up to How to parse restructuredtext in python? where @mbdevpl provided code for parsing the file into a tree and doing some processing on the tree.

I want to autoformat Python code in RST files. Now that I've discovered the code blocks in the tree and changed their contents, how do I write the changes back over the original file? Right now I'm doing this with regexes.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103

2 Answers2

0

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

bignose
  • 30,281
  • 14
  • 77
  • 110
DeepDeadpool
  • 1,441
  • 12
  • 36
0

An rST writer for Docutils is a long standing TODO item. To generate reStructuredText documents with Docutils, you may try the XML (Docutils native) writer and the contributed xml2rst processor in the Docutils "sandbox".

G. Milde
  • 66
  • 3