Docutils: traverse sections?
Asked Answered
S

1

6

How can I traverse each of the section names of a document in Sphinx?

(and where is the documentation for docutils? It is maddeningly difficult to find anything useful beyond the Sphinx Application API; even looking at the source code for docutils/nodes.py doesn't add much help. )

Sagacious answered 26/8, 2016 at 17:45 Comment(0)
S
7

Finally figured it out through trial and error :/

import docutils

def doctree_resolved(app, doctree, docname):
    for section in doctree.traverse(docutils.nodes.section):
        title = section.next_node(docutils.nodes.Titular)
        if title:
            print title.astext()

def setup(app):
    app.connect('doctree-resolved', doctree_resolved)
Sagacious answered 26/8, 2016 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.