In the Docutils document tree, a title
node may occur inside a section, or inside the document itself.
For a particular Docutils NodeVisitor
I am creating, I need to be able to distinguish whether the current title
node is the document's title, or is instead within one of several sections in the document: if it's actually the title of the overall document, I just want to skip this title and move on.
I would have expected to be able to do this within Visitor.visit_title
:
class DocumentTitleSkippingVisitor:
# …
def visit_title(self, node):
document_node = section_node.parent
if section_node is document_node:
# This title is actually the document's top level title.
raise self._docutils.nodes.SkipNode
That doesn't work, though: the visitor encounters the document's top level title inside another section
node. Because of this, the above check (correctly) says the parent of the title
is not the document
node.
How can I tell, within the NodeVisitor
, that the title
is actually the special document title? Alternatively, how can I hook into Docutils such that the title
is actually at the document level, to more easily distinguish it from a section
title?