Classes in CamelCase are abstract base classes or auxiliary classes. The one
exception is Text, for a text (PCDATA) node; uppercase is used to
differentiate from element classes. Classes in lower_case_with_underscores
are element classes, matching the XML element generic identifiers in the DTD.
The position of each node (the level at which it can occur) is significant and
is represented by abstract base classes (Root, Structural, Body,
Inline, etc.). Certain transformations will be easier because we can use
isinstance(node,base_class) to determine the position of the node in the
hierarchy.
Node instances are always true, even if they’re empty. A node is more
than a simple container. Its boolean “truth” does not depend on
having one or more subnodes in the doctree.
Traverse a tree of Node objects, calling the
dispatch_visit() method of visitor when entering each
node. (The walkabout() method is similar, except it also
calls the dispatch_departure() method before exiting each
node.)
This tree traversal supports limited in-place tree
modifications. Replacing one node with one or more nodes is
OK, as is removing an element. However, if the node removed
or replaced occurs after the current node, the old node will
still be traversed, and any new nodes will not.
Within visit methods (and depart methods for
walkabout()), TreePruningException subclasses may be raised
(SkipChildren, SkipSiblings, SkipNode, SkipDeparture).
Parameter visitor: A NodeVisitor object, containing a
visit implementation for each Node subclass encountered.
all descendants in tree traversal order (if descend is true)
the following siblings (if siblings is true) and their
descendants (if also descend is true)
the following siblings of the parent (if ascend is true) and
their descendants (if also descend is true), and so on.
If condition is not None, the iterator yields only nodes
for which condition(node) is true. If condition is a
node class cls, it is equivalent to a function consisting
of returnisinstance(node,cls).
If ascend is true, assume siblings to be true as well.
If the tree structure is modified during iteration, the result
is undefined.
Element is the superclass to all specific elements.
Elements contain attributes and child nodes.
They can be described as a cross between a list and a dictionary.
Elements emulate dictionaries for external [1] attributes, indexing by
attribute name (a string). To set the attribute ‘att’ to ‘value’, do:
element['att']='value'
There are two special attributes: ‘ids’ and ‘names’. Both are
lists of unique identifiers: ‘ids’ conform to the regular expression
[a-z](-?[a-z0-9]+)* (see the make_id() function for rationale and
details). ‘names’ serve as user-friendly interfaces to IDs; they are
case- and whitespace-normalized (see the fully_normalize_name() function).
Elements emulate lists for child nodes (element nodes and/or text
nodes), indexing by integer. To get the first child node, use:
element[0]
to iterate over the child nodes (without descending), use:
forchildinelement:...
Elements may be constructed using the += operator. To add one new
child node to element, do:
element+=node
This is equivalent to element.append(node).
To add a list of multiple child nodes at once, use the same +=
operator:
element+=[node1,node2]
This is equivalent to element.extend([node1,node2]).
Look iteratively in self and parents for a class argument
starting with language- and return the remainder of it
(which should be a BCP49 language tag) or the fallback.
First, convert both self[attr] and value to a non-string sequence
type; if either is not already a sequence, convert it to a list of one
element. Then call append_attr_list.
If attr is an attribute of self and either self[attr] or value is a
list, convert all non-sequence values to a sequence of 1 element and
then concatenate the two sequence, setting the result to self[attr].
If both self[attr] and value are non-sequences and replace is True or
self[attr] is None, replace self[attr] with value. Otherwise, do
nothing.
If attr is an attribute of self and both self[attr] and value are
lists, concatenate the two sequences, setting the result to
self[attr]. If either self[attr] or value are non-sequences and
replace is True or self[attr] is None, replace self[attr] with value.
Otherwise, do nothing.
Updates all attributes from node or dictionary dict_.
Appends the basic attributes (‘ids’, ‘names’, ‘classes’,
‘dupnames’, but not ‘source’) and then, for all other attributes in
dict_, updates the same attribute in self. When attributes with the
same identifier appear in both self and dict_, the two values are
merged based on the value of update_fun. Generally, when replace is
True, the values in self are replaced or merged with the values in
dict_; otherwise, the values in self may be preserved or merged. When
and_source is True, the ‘source’ attribute is included in the copy.
NOTE: When replace is False, and self contains a ‘source’ attribute,
‘source’ is not replaced even when dict_ has a ‘source’
attribute, though it may still be merged into a list depending
on the value of update_fun.
NOTE: It is easier to call the update-specific methods then to pass
Updates all attributes from node or dictionary dict_.
Appends the basic attributes (‘ids’, ‘names’, ‘classes’,
‘dupnames’, but not ‘source’) and then, for all other attributes in
dict_, updates the same attribute in self. When attributes with the
same identifier appear in both self and dict_ and replace is True, the
values in self are replaced with the values in dict_; otherwise, the
values in self are preserved. When and_source is True, the ‘source’
attribute is included in the copy.
NOTE: When replace is False, and self contains a ‘source’ attribute,
‘source’ is not replaced even when dict_ has a ‘source’
attribute, though it may still be merged into a list depending
on the value of update_fun.
Updates all attributes from node or dictionary dict_.
Appends the basic attributes (‘ids’, ‘names’, ‘classes’,
‘dupnames’, but not ‘source’) and then, for all other attributes in
dict_, updates the same attribute in self. When attributes with the
same identifier appear in both self and dict_ whose values aren’t each
lists and replace is True, the values in self are replaced with the
values in dict_; if the values from self and dict_ for the given
identifier are both of list type, then the two lists are concatenated
and the result stored in self; otherwise, the values in self are
preserved. When and_source is True, the ‘source’ attribute is
included in the copy.
NOTE: When replace is False, and self contains a ‘source’ attribute,
‘source’ is not replaced even when dict_ has a ‘source’
attribute, though it may still be merged into a list depending
on the value of update_fun.
Updates all attributes from node or dictionary dict_.
Appends the basic attributes (‘ids’, ‘names’, ‘classes’,
‘dupnames’, but not ‘source’) and then, for all other attributes in
dict_, updates the same attribute in self. When attributes with the
same identifier appear in both self and dict_ whose values are both
not lists and replace is True, the values in self are replaced with
the values in dict_; if either of the values from self and dict_ for
the given identifier are of list type, then first any non-lists are
converted to 1-element lists and then the two lists are concatenated
and the result stored in self; otherwise, the values in self are
preserved. When and_source is True, the ‘source’ attribute is
included in the copy.
NOTE: When replace is False, and self contains a ‘source’ attribute,
‘source’ is not replaced even when dict_ has a ‘source’
attribute, though it may still be merged into a list depending
on the value of update_fun.
Updates all attributes from node or dictionary dict_.
Appends the basic attributes (‘ids’, ‘names’, ‘classes’,
‘dupnames’, but not ‘source’) and then, for all other attributes in
dict_, updates the same attribute in self. When attributes with the
same identifier appear in both self and dict_ then first any non-lists
are converted to 1-element lists and then the two lists are
concatenated and the result stored in self; otherwise, the values in
self are preserved. When and_source is True, the ‘source’ attribute
is included in the copy.
NOTE: When replace is False, and self contains a ‘source’ attribute,
‘source’ is not replaced even when dict_ has a ‘source’
attribute, though it may still be merged into a list depending
on the value of update_fun.
Its children are all Text or Inline subclass nodes. You can
check whether an element’s context is inline simply by checking whether
its immediate parent is a TextElement instance (including subclasses).
This is handy for nodes like image that can appear both inline and as
standalone body elements.
If passing children to __init__(), make sure to set text to
'' or some other suitable value.
self.nameids maps names to IDs, while self.nametypes maps names to
booleans representing hyperlink type (True==explicit,
False==implicit). This method updates the mappings.
The following state transition table shows how self.nameids items
(“id”) and self.nametypes items (“type”) change with new input
(a call to this method), and what actions are performed
(“implicit”-type system messages are INFO/1, and
“explicit”-type system messages are ERROR/3):
Topics are terminal, “leaf” mini-sections, like block quotes with titles,
or textual figures. A topic is just like a section, except that it has no
subsections, and it doesn’t have to conform to section placement rules.
Topics are allowed wherever body elements (list, table, etc.) are allowed,
but only at the top level of a section or document. Topics cannot nest
inside topics, sidebars, or body elements; you can’t have a topic inside a
table, list, block quote, etc.
Sidebars are like miniature, parallel documents that occur inside other
documents, providing related or reference material. A sidebar is
typically offset by a border and “floats” to the side of the page; the
document’s main text may flow around it. Sidebars can also be likened to
super-footnotes; their content is outside of the flow of the document’s
main text.
Sidebars are allowed wherever body elements (list, table, etc.) are
allowed, but only at the top level of a section or document. Sidebars
cannot nest inside sidebars, topics, or body elements; you can’t have a
sidebar inside a table, list, block quote, etc.
The “pending” element is used to encapsulate a pending operation: the
operation (transform), the point at which to apply it, and any data it
requires. Only the pending operation’s location within the document is
stored in the public document tree (by the “pending” object itself); the
operation and its data are stored in the “pending” object’s internal
instance attributes.
For example, say you want a table of contents in your reStructuredText
document. The easiest way to specify where to put it is from within the
document, with a directive:
..contents::
But the “contents” directive can’t do its work until the entire document
has been parsed and possibly transformed to some extent. So the directive
code leaves a placeholder behind that will trigger the second phase of its
processing, something like this:
“Visitor” pattern [GoF95] abstract superclass implementation for
document tree traversals.
Each node class has corresponding methods, doing nothing by
default; override individual methods for specific and useful
behaviour. The dispatch_visit() method is called by
Node.walk() upon entering a node. Node.walkabout() also calls
the dispatch_departure() method before exiting a node.
The dispatch methods call “visit_ + node class name” or
“depart_ + node class name”, resp.
This is a base class for visitors whose visit_... & depart_...
methods must be implemented for all compulsory node types encountered
(such as for docutils.writers.Writer subclasses).
Unimplemented methods will raise exceptions (except for optional nodes).
For sparse traversals, where only certain node types are of interest, use
subclass SparseNodeVisitor instead. When (mostly or entirely) uniform
processing is desired, subclass GenericNodeVisitor.
Base class for sparse traversals, where only certain node types are of
interest. When visit_... & depart_... methods should be
implemented for all node types (such as for docutils.writers.Writer
subclasses), subclass NodeVisitor instead.
Generic “Visitor” abstract superclass, for simple traversals.
Unless overridden, each visit_... method calls default_visit(), and
each depart_... method (when using Node.walkabout()) calls
default_departure(). default_visit() (and default_departure()) must
be overridden in subclasses.
Stop the traversal altogether. The current node’s depart_... method
is not affected. The parent nodes depart_... methods are also called
as usual. No other nodes are visited. This is an alternative to
NodeFound that does not cause exception handling to trickle up to the
caller.
Docutils identifiers will conform to the regular expression
[a-z](-?[a-z0-9]+)*. For CSS compatibility, identifiers (the “class”
and “id” attributes) should have no underscores, colons, or periods.
Hyphens may be used.
The HTML 4.01 spec defines identifiers based on SGML tokens:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens (“-“),
underscores (“_”), colons (“:”), and periods (“.”).
However the CSS1 spec defines identifiers based on the “name” token,
a tighter interpretation (“flex” tokenizer notation; “latin1” and
“escape” 8-bit characters have been replaced with entities):
The CSS1 “nmchar” rule does not include underscores (“_”), colons (“:”),
or periods (“.”), therefore “class” and “id” attributes should not contain
these characters. They should be replaced with hyphens (“-“). Combined
with HTML’s requirements (the first character must be a letter; no
“unicode”, “latin1”, or “escape” characters), this results in the
[a-z](-?[a-z0-9]+)* pattern.