Search code examples
pythondocutils

add node to rst doctree and save to file


I'd like to insert a custom node into a doctree and save these changes to file.

I'm reading the following RST file...

Hello World!
############

:date: 2010-10-03 10:20
:author: James Bond

This is a test.

like this:

doctree = docutils.core.publish_doctree(open('test.rst').read())

I'd now like to insert a new node :id: 234123 to the docinfo and save it as test.rst. But how?


Solution

  • There is no reStructuredText writer.

    See the answer here: Is there a reST Writer?

    if it's a trivial change you're better off writing a simple function that reads the file as lines of text, which it writes out to a new text file, inserting the new line at some point during parsing/writing.