I'm trying to use the docutils package to convert ReST to HTML. This answer succinctly uses the docutils publish_*
convenience functions to achieve this in one step. The ReST documents that I want to convert have multiple sections that I want to separate in the resulting HTML. As such, I want to break this process down:
- Parse the ReST into a tree of nodes.
- Separate the nodes as appropriate.
- Convert the nodes I want into HTML.
It's step three that I'm struggling with. Here's how I do steps one and two:
from docutils import utils
from docutils.frontend import OptionParser
from docutils.parsers.rst import Parser
# preamble
rst = '*NB:* just an example.' # will actually have many sections
path = 'some.url.com'
settings = OptionParser(components=(Parser,)).get_default_values()
# step 1
document = utils.new_document(path, settings)
Parser().parse(rst, document)
# step 2
for node in document:
do_something_with(node)
# step 3: Help!
for node in filtered(document):
print(convert_to_html(node))
I've found the HTMLTranslator
class and the Publisher
class. They seem relevant but I'm struggling to find good documentation. How should I implement the convert_to_html
function?