Skip to content

Instantly share code, notes, and snippets.

@splbio
Last active August 29, 2015 14:23
Show Gist options
  • Save splbio/a5a4e2dd76eaf4deb6d2 to your computer and use it in GitHub Desktop.
Save splbio/a5a4e2dd76eaf4deb6d2 to your computer and use it in GitHub Desktop.
Parsing inline rst into nodes
import docutils.frontend
# http://docutils.sourceforge.net/docs/dev/hacking.html#parsing-the-document
parser = docutils.parsers.rst.Parser()
src = """
This is a heading
=================
some item
#. hello
#. world
"""
settings = docutils.frontend.OptionParser(
components=(docutils.parsers.rst.Parser,)
).get_default_values()
document = docutils.utils.new_document("my file", settings)
parser.parse(src, document)
print dir(document)
def dumpdoc(document, level=0):
for child in document.children:
print "================================="
#if child.tagname == '#text':
# print child.astext()
print "%d -> %s" % (level, dir(child))
if hasattr(child, 'text'):
print child.text
print ">> %s <<" % child.astext()
dumpdoc(child, level + 1)
dumpdoc(document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment