文档:遍历部分?

Docutils: traverse sections?

如何在 Sphinx 中遍历文档的每个部分名称?

(docutils 的文档在哪里?很难找到任何有用的东西,除了 Sphinx Application API; even looking at the source code for docutils/nodes.py 没有太多帮助。)

终于通过反复试验弄明白了:/

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)