Last active
August 29, 2015 14:23
-
-
Save splbio/a5a4e2dd76eaf4deb6d2 to your computer and use it in GitHub Desktop.
Parsing inline rst into nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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