Say I have the following reST input:
Some text ...
:foo: bar
Some text ...
What I would like to end up with is a dict like this:
Here's my ElementTree implementation:
from docutils.core import publish_doctree
from xml.etree.ElementTree import fromstring
source = """Some text ...
:foo: bar
Some text ...
"""
def gen_fields(source):
dom = publish_doctree(source).asdom()
tree = fromstring(dom.toxml())
for field in tree.iter(tag='field'):
name = next(field.iter(tag='field_name'))
body = next(field.iter(tag='field_body'))
yield {name.text: ''.join(body.itertext())}
Usage
>>> next(gen_fields(source))
{'foo': 'bar'}