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

2

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.

Acrobatics answered 24/2, 2021 at 14:44 Comment(3)
You could use a package like rst.readthedocs.io/en/latestMarissamarist
doesn't seem to be a serious packageAcrobatics
Looks like a duplicate of Is there a reST Writer?Singlestick
C
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

Consternate answered 18/8, 2022 at 14:41 Comment(0)
S
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 xml2rst processor by Stefan Merten.

The latest version at http://www.merten-home.de/FreeSoftware/xml2rst is underdocumented, the documentation for a previous version points to additonal requirements. Unfortunately the package and custom requirements do not work with Python 3.x (required by current Docutils). The XSLT stylesheet at the core of the converter may still work independent of Python, see Other ways to use xml2rst.

Singlestick answered 8/5, 2023 at 21:1 Comment(2)
Note: the code of xml2rst is broken and does not run because of unresolvable imports.Adagietto
Thanks for pointing this out. I'll edit the answer.Singlestick

© 2022 - 2025 — McMap. All rights reserved.