2 # Author: David Goodger <goodger@python.org>
3 # Maintainer: docutils-develop@lists.sourceforge.net
4 # Copyright: This module has been placed in the public domain.
7 Docutils document tree element class library.
9 Classes in CamelCase are abstract base classes or auxiliary classes. The one
10 exception is `Text`, for a text (PCDATA) node; uppercase is used to
11 differentiate from element classes. Classes in lower_case_with_underscores
12 are element classes, matching the XML element generic identifiers in the DTD_.
14 The position of each node (the level at which it can occur) is significant and
15 is represented by abstract base classes (`Root`, `Structural`, `Body`,
16 `Inline`, etc.). Certain transformations will be easier because we can use
17 ``isinstance(node, base_class)`` to determine the position of the node in the
20 .. _DTD: http://docutils.sourceforge.net/docs/ref/docutils.dtd
23 __docformat__
= 'reStructuredText'
32 # ==============================
33 # Functional Node Base Classes
34 # ==============================
38 """Abstract base class of nodes in a document tree."""
41 """Back-reference to the Node immediately containing this Node."""
44 """The `document` node at the root of the tree containing this Node."""
47 """Path or description of the input source which generated this Node."""
50 """The line number (1-based) of the beginning of this Node in `source`."""
52 def __nonzero__(self
):
54 Node instances are always true, even if they're empty. A node is more
55 than a simple container. Its boolean "truth" does not depend on
56 having one or more subnodes in the doctree.
58 Use `len()` to check node length. Use `None` to represent a boolean
63 if sys
.version_info
< (3,):
64 # on 2.x, str(node) will be a byte string with Unicode
65 # characters > 255 escaped; on 3.x this is no longer necessary
67 return unicode(self
).encode('raw_unicode_escape')
69 def asdom(self
, dom
=None):
70 """Return a DOM **fragment** representation of this Node."""
72 import xml
.dom
.minidom
as dom
73 domroot
= dom
.Document()
74 return self
._dom
_node
(domroot
)
76 def pformat(self
, indent
=' ', level
=0):
78 Return an indented pseudo-XML representation, for test purposes.
80 Override in subclasses.
82 raise NotImplementedError
85 """Return a copy of self."""
86 raise NotImplementedError
89 """Return a deep copy of self (also copying children)."""
90 raise NotImplementedError
92 def setup_child(self
, child
):
95 child
.document
= self
.document
96 if child
.source
is None:
97 child
.source
= self
.document
.current_source
98 if child
.line
is None:
99 child
.line
= self
.document
.current_line
101 def walk(self
, visitor
):
103 Traverse a tree of `Node` objects, calling the
104 `dispatch_visit()` method of `visitor` when entering each
105 node. (The `walkabout()` method is similar, except it also
106 calls the `dispatch_departure()` method before exiting each
109 This tree traversal supports limited in-place tree
110 modifications. Replacing one node with one or more nodes is
111 OK, as is removing an element. However, if the node removed
112 or replaced occurs after the current node, the old node will
113 still be traversed, and any new nodes will not.
115 Within ``visit`` methods (and ``depart`` methods for
116 `walkabout()`), `TreePruningException` subclasses may be raised
117 (`SkipChildren`, `SkipSiblings`, `SkipNode`, `SkipDeparture`).
119 Parameter `visitor`: A `NodeVisitor` object, containing a
120 ``visit`` implementation for each `Node` subclass encountered.
122 Return true if we should stop the traversal.
125 visitor
.document
.reporter
.debug(
126 'docutils.nodes.Node.walk calling dispatch_visit for %s'
127 % self
.__class
__.__name
__)
130 visitor
.dispatch_visit(self
)
131 except (SkipChildren
, SkipNode
):
133 except SkipDeparture
: # not applicable; ignore
135 children
= self
.children
137 for child
in children
[:]:
138 if child
.walk(visitor
):
143 except StopTraversal
:
147 def walkabout(self
, visitor
):
149 Perform a tree traversal similarly to `Node.walk()` (which
150 see), except also call the `dispatch_departure()` method
151 before exiting each node.
153 Parameter `visitor`: A `NodeVisitor` object, containing a
154 ``visit`` and ``depart`` implementation for each `Node`
155 subclass encountered.
157 Return true if we should stop the traversal.
161 visitor
.document
.reporter
.debug(
162 'docutils.nodes.Node.walkabout calling dispatch_visit for %s'
163 % self
.__class
__.__name
__)
166 visitor
.dispatch_visit(self
)
169 except SkipDeparture
:
171 children
= self
.children
173 for child
in children
[:]:
174 if child
.walkabout(visitor
):
181 except StopTraversal
:
184 visitor
.document
.reporter
.debug(
185 'docutils.nodes.Node.walkabout calling dispatch_departure '
186 'for %s' % self
.__class
__.__name
__)
187 visitor
.dispatch_departure(self
)
190 def _fast_traverse(self
, cls
):
191 """Specialized traverse() that only supports instance checks."""
193 if isinstance(self
, cls
):
195 for child
in self
.children
:
196 result
.extend(child
._fast
_traverse
(cls
))
199 def _all_traverse(self
):
200 """Specialized traverse() that doesn't check for a condition."""
203 for child
in self
.children
:
204 result
.extend(child
._all
_traverse
())
207 def traverse(self
, condition
=None, include_self
=True, descend
=True,
208 siblings
=False, ascend
=False):
210 Return an iterable containing
212 * self (if include_self is true)
213 * all descendants in tree traversal order (if descend is true)
214 * all siblings (if siblings is true) and their descendants (if
215 also descend is true)
216 * the siblings of the parent (if ascend is true) and their
217 descendants (if also descend is true), and so on
219 If `condition` is not None, the iterable contains only nodes
220 for which ``condition(node)`` is true. If `condition` is a
221 node class ``cls``, it is equivalent to a function consisting
222 of ``return isinstance(node, cls)``.
224 If ascend is true, assume siblings to be true as well.
226 For example, given the following tree::
229 <emphasis> <--- emphasis.traverse() and
230 <strong> <--- strong.traverse() are called.
233 <reference name="Baz" refid="baz">
236 Then list(emphasis.traverse()) equals ::
238 [<emphasis>, <strong>, <#text: Foo>, <#text: Bar>]
240 and list(strong.traverse(ascend=True)) equals ::
242 [<strong>, <#text: Foo>, <#text: Bar>, <reference>, <#text: Baz>]
246 # Check for special argument combinations that allow using an
247 # optimized version of traverse()
248 if include_self
and descend
and not siblings
:
249 if condition
is None:
250 return self
._all
_traverse
()
251 elif isinstance(condition
, (types
.ClassType
, type)):
252 return self
._fast
_traverse
(condition
)
253 # Check if `condition` is a class (check for TypeType for Python
254 # implementations that use only new-style classes, like PyPy).
255 if isinstance(condition
, (types
.ClassType
, type)):
256 node_class
= condition
257 def condition(node
, node_class
=node_class
):
258 return isinstance(node
, node_class
)
260 if include_self
and (condition
is None or condition(self
)):
262 if descend
and len(self
.children
):
264 r
.extend(child
.traverse(include_self
=True, descend
=True,
265 siblings
=False, ascend
=False,
266 condition
=condition
))
267 if siblings
or ascend
:
270 index
= node
.parent
.index(node
)
271 for sibling
in node
.parent
[index
+1:]:
272 r
.extend(sibling
.traverse(include_self
=True,
274 siblings
=False, ascend
=False,
275 condition
=condition
))
282 def next_node(self
, condition
=None, include_self
=False, descend
=True,
283 siblings
=False, ascend
=False):
285 Return the first node in the iterable returned by traverse(),
286 or None if the iterable is empty.
288 Parameter list is the same as of traverse. Note that
289 include_self defaults to 0, though.
291 iterable
= self
.traverse(condition
=condition
,
292 include_self
=include_self
, descend
=descend
,
293 siblings
=siblings
, ascend
=ascend
)
299 if sys
.version_info
< (3,):
300 class reprunicode(unicode):
302 A unicode sub-class that removes the initial u from unicode's repr.
306 return unicode.__repr
__(self
)[1:]
310 reprunicode
= unicode
315 Failsave conversion of `unicode` to `str`.
317 if sys
.version_info
< (3,) and isinstance(s
, unicode):
318 return s
.encode('ascii', 'backslashreplace')
322 class Text(Node
, reprunicode
):
325 Instances are terminal nodes (leaves) containing text only; no child
326 nodes or attributes. Initialize by passing a string to the constructor.
327 Access the text itself with the `astext` method.
333 """Text nodes have no children, and cannot have children."""
335 if sys
.version_info
> (3,):
336 def __new__(cls
, data
, rawsource
=None):
337 """Prevent the rawsource argument from propagating to str."""
338 if isinstance(data
, bytes
):
339 raise TypeError('expecting str data, not bytes')
340 return reprunicode
.__new
__(cls
, data
)
342 def __new__(cls
, data
, rawsource
=None):
343 """Prevent the rawsource argument from propagating to str."""
344 return reprunicode
.__new
__(cls
, data
)
346 def __init__(self
, data
, rawsource
=''):
348 self
.rawsource
= rawsource
349 """The raw text from which this element was constructed."""
351 def shortrepr(self
, maxlen
=18):
353 if len(data
) > maxlen
:
354 data
= data
[:maxlen
-4] + ' ...'
355 return '<%s: %r>' % (self
.tagname
, reprunicode(data
))
358 return self
.shortrepr(maxlen
=68)
360 def _dom_node(self
, domroot
):
361 return domroot
.createTextNode(unicode(self
))
364 return reprunicode(self
)
366 # Note about __unicode__: The implementation of __unicode__ here,
367 # and the one raising NotImplemented in the superclass Node had
368 # to be removed when changing Text to a subclass of unicode instead
369 # of UserString, since there is no way to delegate the __unicode__
370 # call to the superclass unicode:
371 # unicode itself does not have __unicode__ method to delegate to
372 # and calling unicode(self) or unicode.__new__ directly creates
376 return self
.__class
__(reprunicode(self
), rawsource
=self
.rawsource
)
381 def pformat(self
, indent
=' ', level
=0):
383 indent
= indent
* level
384 for line
in self
.splitlines():
385 result
.append(indent
+ line
+ '\n')
386 return ''.join(result
)
388 # rstrip and lstrip are used by substitution definitions where
389 # they are expected to return a Text instance, this was formerly
390 # taken care of by UserString. Note that then and now the
391 # rawsource member is lost.
393 def rstrip(self
, chars
=None):
394 return self
.__class
__(reprunicode
.rstrip(self
, chars
))
395 def lstrip(self
, chars
=None):
396 return self
.__class
__(reprunicode
.lstrip(self
, chars
))
401 `Element` is the superclass to all specific elements.
403 Elements contain attributes and child nodes. Elements emulate
404 dictionaries for attributes, indexing by attribute name (a string). To
405 set the attribute 'att' to 'value', do::
407 element['att'] = 'value'
409 There are two special attributes: 'ids' and 'names'. Both are
410 lists of unique identifiers, and names serve as human interfaces
411 to IDs. Names are case- and whitespace-normalized (see the
412 fully_normalize_name() function), and IDs conform to the regular
413 expression ``[a-z](-?[a-z0-9]+)*`` (see the make_id() function).
415 Elements also emulate lists for child nodes (element nodes and/or text
416 nodes), indexing by integer. To get the first child node, use::
420 Elements may be constructed using the ``+=`` operator. To add one new
421 child node to element, do::
425 This is equivalent to ``element.append(node)``.
427 To add a list of multiple child nodes at once, use the same ``+=``
430 element += [node1, node2]
432 This is equivalent to ``element.extend([node1, node2])``.
435 basic_attributes
= ('ids', 'classes', 'names', 'dupnames')
436 """List attributes which are defined for every Element-derived class
437 instance and can be safely transferred to a different node."""
439 local_attributes
= ('backrefs',)
440 """A list of class-specific attributes that should not be copied with the
441 standard attributes when replacing a node.
443 NOTE: Derived classes should override this value to prevent any of its
444 attributes being copied by adding to the value in its parent class."""
446 list_attributes
= basic_attributes
+ local_attributes
447 """List attributes, automatically initialized to empty lists for
450 known_attributes
= list_attributes
+ ('source',)
451 """List attributes that are known to the Element base class."""
454 """The element generic identifier. If None, it is set as an instance
455 attribute to the name of the class."""
457 child_text_separator
= '\n\n'
458 """Separator for child nodes, used by `astext()` method."""
460 def __init__(self
, rawsource
='', *children
, **attributes
):
461 self
.rawsource
= rawsource
462 """The raw text from which this element was constructed."""
465 """List of child nodes (elements and/or `Text`)."""
467 self
.extend(children
) # maintain parent info
470 """Dictionary of attribute {name: value}."""
472 # Initialize list attributes.
473 for att
in self
.list_attributes
:
474 self
.attributes
[att
] = []
476 for att
, value
in attributes
.items():
478 if att
in self
.list_attributes
:
479 # mutable list; make a copy for this node
480 self
.attributes
[att
] = value
[:]
482 self
.attributes
[att
] = value
484 if self
.tagname
is None:
485 self
.tagname
= self
.__class
__.__name
__
487 def _dom_node(self
, domroot
):
488 element
= domroot
.createElement(self
.tagname
)
489 for attribute
, value
in self
.attlist():
490 if isinstance(value
, list):
491 value
= ' '.join([serial_escape('%s' % (v
,)) for v
in value
])
492 element
.setAttribute(attribute
, '%s' % value
)
493 for child
in self
.children
:
494 element
.appendChild(child
._dom
_node
(domroot
))
499 for c
in self
.children
:
500 data
+= c
.shortrepr()
502 data
= data
[:56] + ' ...'
505 return '<%s "%s": %s>' % (self
.__class
__.__name
__,
506 '; '.join([ensure_str(n
) for n
in self
['names']]), data
)
508 return '<%s: %s>' % (self
.__class
__.__name
__, data
)
512 return '<%s "%s"...>' % (self
.__class
__.__name
__,
513 '; '.join([ensure_str(n
) for n
in self
['names']]))
515 return '<%s...>' % self
.tagname
517 def __unicode__(self
):
519 return u
'%s%s%s' % (self
.starttag(),
520 ''.join([unicode(c
) for c
in self
.children
]),
523 return self
.emptytag()
525 if sys
.version_info
> (3,):
526 # 2to3 doesn't convert __unicode__ to __str__
527 __str__
= __unicode__
529 def starttag(self
, quoteattr
=None):
530 # the optional arg is used by the docutils_xml writer
531 if quoteattr
is None:
532 quoteattr
= pseudo_quoteattr
533 parts
= [self
.tagname
]
534 for name
, value
in self
.attlist():
535 if value
is None: # boolean attribute
536 parts
.append('%s="True"' % name
)
538 if isinstance(value
, list):
539 values
= [serial_escape('%s' % (v
,)) for v
in value
]
540 value
= ' '.join(values
)
542 value
= unicode(value
)
543 value
= quoteattr(value
)
544 parts
.append(u
'%s=%s' % (name
, value
))
545 return u
'<%s>' % u
' '.join(parts
)
548 return '</%s>' % self
.tagname
551 return u
'<%s/>' % u
' '.join([self
.tagname
] +
553 for n
, v
in self
.attlist()])
556 return len(self
.children
)
558 def __contains__(self
, key
):
559 # support both membership test for children and attributes
560 # (has_key is translated to "in" by 2to3)
561 if isinstance(key
, basestring
):
562 return key
in self
.attributes
563 return key
in self
.children
565 def __getitem__(self
, key
):
566 if isinstance(key
, basestring
):
567 return self
.attributes
[key
]
568 elif isinstance(key
, int):
569 return self
.children
[key
]
570 elif isinstance(key
, types
.SliceType
):
571 assert key
.step
in (None, 1), 'cannot handle slice with stride'
572 return self
.children
[key
.start
:key
.stop
]
574 raise TypeError, ('element index must be an integer, a slice, or '
575 'an attribute name string')
577 def __setitem__(self
, key
, item
):
578 if isinstance(key
, basestring
):
579 self
.attributes
[str(key
)] = item
580 elif isinstance(key
, int):
581 self
.setup_child(item
)
582 self
.children
[key
] = item
583 elif isinstance(key
, types
.SliceType
):
584 assert key
.step
in (None, 1), 'cannot handle slice with stride'
586 self
.setup_child(node
)
587 self
.children
[key
.start
:key
.stop
] = item
589 raise TypeError, ('element index must be an integer, a slice, or '
590 'an attribute name string')
592 def __delitem__(self
, key
):
593 if isinstance(key
, basestring
):
594 del self
.attributes
[key
]
595 elif isinstance(key
, int):
596 del self
.children
[key
]
597 elif isinstance(key
, types
.SliceType
):
598 assert key
.step
in (None, 1), 'cannot handle slice with stride'
599 del self
.children
[key
.start
:key
.stop
]
601 raise TypeError, ('element index must be an integer, a simple '
602 'slice, or an attribute name string')
604 def __add__(self
, other
):
605 return self
.children
+ other
607 def __radd__(self
, other
):
608 return other
+ self
.children
610 def __iadd__(self
, other
):
611 """Append a node or a list of nodes to `self.children`."""
612 if isinstance(other
, Node
):
614 elif other
is not None:
619 return self
.child_text_separator
.join(
620 [child
.astext() for child
in self
.children
])
622 def non_default_attributes(self
):
624 for key
, value
in self
.attributes
.items():
625 if self
.is_not_default(key
):
630 attlist
= self
.non_default_attributes().items()
634 def get(self
, key
, failobj
=None):
635 return self
.attributes
.get(key
, failobj
)
637 def hasattr(self
, attr
):
638 return attr
in self
.attributes
640 def delattr(self
, attr
):
641 if attr
in self
.attributes
:
642 del self
.attributes
[attr
]
644 def setdefault(self
, key
, failobj
=None):
645 return self
.attributes
.setdefault(key
, failobj
)
649 # support operator ``in``
650 __contains__
= hasattr
652 def get_language_code(self
, fallback
=''):
653 """Return node's language tag.
655 Look iteratively in self and parents for a class argument
656 starting with ``language-`` and return the remainder of it
657 (which should be a `BCP49` language tag) or the `fallback`.
659 for cls
in self
.get('classes', []):
660 if cls
.startswith('language-'):
663 return self
.parent
.get_language(fallback
)
664 except AttributeError:
667 def append(self
, item
):
668 self
.setup_child(item
)
669 self
.children
.append(item
)
671 def extend(self
, item
):
675 def insert(self
, index
, item
):
676 if isinstance(item
, Node
):
677 self
.setup_child(item
)
678 self
.children
.insert(index
, item
)
679 elif item
is not None:
680 self
[index
:index
] = item
683 return self
.children
.pop(i
)
685 def remove(self
, item
):
686 self
.children
.remove(item
)
688 def index(self
, item
):
689 return self
.children
.index(item
)
691 def is_not_default(self
, key
):
692 if self
[key
] == [] and key
in self
.list_attributes
:
697 def update_basic_atts(self
, dict_
):
699 Update basic attributes ('ids', 'names', 'classes',
700 'dupnames', but not 'source') from node or dictionary `dict_`.
702 if isinstance(dict_
, Node
):
703 dict_
= dict_
.attributes
704 for att
in self
.basic_attributes
:
705 self
.append_attr_list(att
, dict_
.get(att
, []))
707 def append_attr_list(self
, attr
, values
):
709 For each element in values, if it does not exist in self[attr], append
712 NOTE: Requires self[attr] and values to be sequence type and the
713 former should specifically be a list.
717 if not value
in self
[attr
]:
718 self
[attr
].append(value
)
720 def coerce_append_attr_list(self
, attr
, value
):
722 First, convert both self[attr] and value to a non-string sequence
723 type; if either is not already a sequence, convert it to a list of one
724 element. Then call append_attr_list.
726 NOTE: self[attr] and value both must not be None.
729 if not isinstance(self
.get(attr
), list):
730 self
[attr
] = [self
[attr
]]
731 if not isinstance(value
, list):
733 self
.append_attr_list(attr
, value
)
735 def replace_attr(self
, attr
, value
, force
= True):
737 If self[attr] does not exist or force is True or omitted, set
738 self[attr] to value, otherwise do nothing.
741 if force
or self
.get(attr
) is None:
744 def copy_attr_convert(self
, attr
, value
, replace
= True):
746 If attr is an attribute of self, set self[attr] to
747 [self[attr], value], otherwise set self[attr] to value.
749 NOTE: replace is not used by this function and is kept only for
750 compatibility with the other copy functions.
752 if self
.get(attr
) is not value
:
753 self
.coerce_append_attr_list(attr
, value
)
755 def copy_attr_coerce(self
, attr
, value
, replace
):
757 If attr is an attribute of self and either self[attr] or value is a
758 list, convert all non-sequence values to a sequence of 1 element and
759 then concatenate the two sequence, setting the result to self[attr].
760 If both self[attr] and value are non-sequences and replace is True or
761 self[attr] is None, replace self[attr] with value. Otherwise, do
764 if self
.get(attr
) is not value
:
765 if isinstance(self
.get(attr
), list) or \
766 isinstance(value
, list):
767 self
.coerce_append_attr_list(attr
, value
)
769 self
.replace_attr(attr
, value
, replace
)
771 def copy_attr_concatenate(self
, attr
, value
, replace
):
773 If attr is an attribute of self and both self[attr] and value are
774 lists, concatenate the two sequences, setting the result to
775 self[attr]. If either self[attr] or value are non-sequences and
776 replace is True or self[attr] is None, replace self[attr] with value.
777 Otherwise, do nothing.
779 if self
.get(attr
) is not value
:
780 if isinstance(self
.get(attr
), list) and \
781 isinstance(value
, list):
782 self
.append_attr_list(attr
, value
)
784 self
.replace_attr(attr
, value
, replace
)
786 def copy_attr_consistent(self
, attr
, value
, replace
):
788 If replace is True or selfpattr] is None, replace self[attr] with
789 value. Otherwise, do nothing.
791 if self
.get(attr
) is not value
:
792 self
.replace_attr(attr
, value
, replace
)
794 def update_all_atts(self
, dict_
, update_fun
= copy_attr_consistent
,
795 replace
= True, and_source
= False):
797 Updates all attributes from node or dictionary `dict_`.
799 Appends the basic attributes ('ids', 'names', 'classes',
800 'dupnames', but not 'source') and then, for all other attributes in
801 dict_, updates the same attribute in self. When attributes with the
802 same identifier appear in both self and dict_, the two values are
803 merged based on the value of update_fun. Generally, when replace is
804 True, the values in self are replaced or merged with the values in
805 dict_; otherwise, the values in self may be preserved or merged. When
806 and_source is True, the 'source' attribute is included in the copy.
808 NOTE: When replace is False, and self contains a 'source' attribute,
809 'source' is not replaced even when dict_ has a 'source'
810 attribute, though it may still be merged into a list depending
811 on the value of update_fun.
812 NOTE: It is easier to call the update-specific methods then to pass
813 the update_fun method to this function.
815 if isinstance(dict_
, Node
):
816 dict_
= dict_
.attributes
818 # Include the source attribute when copying?
820 filter_fun
= self
.is_not_list_attribute
822 filter_fun
= self
.is_not_known_attribute
824 # Copy the basic attributes
825 self
.update_basic_atts(dict_
)
827 # Grab other attributes in dict_ not in self except the
828 # (All basic attributes should be copied already)
829 for att
in filter(filter_fun
, dict_
):
830 update_fun(self
, att
, dict_
[att
], replace
)
832 def update_all_atts_consistantly(self
, dict_
, replace
= True,
835 Updates all attributes from node or dictionary `dict_`.
837 Appends the basic attributes ('ids', 'names', 'classes',
838 'dupnames', but not 'source') and then, for all other attributes in
839 dict_, updates the same attribute in self. When attributes with the
840 same identifier appear in both self and dict_ and replace is True, the
841 values in self are replaced with the values in dict_; otherwise, the
842 values in self are preserved. When and_source is True, the 'source'
843 attribute is included in the copy.
845 NOTE: When replace is False, and self contains a 'source' attribute,
846 'source' is not replaced even when dict_ has a 'source'
847 attribute, though it may still be merged into a list depending
848 on the value of update_fun.
850 self
.update_all_atts(dict_
, Element
.copy_attr_consistent
, replace
,
853 def update_all_atts_concatenating(self
, dict_
, replace
= True,
856 Updates all attributes from node or dictionary `dict_`.
858 Appends the basic attributes ('ids', 'names', 'classes',
859 'dupnames', but not 'source') and then, for all other attributes in
860 dict_, updates the same attribute in self. When attributes with the
861 same identifier appear in both self and dict_ whose values aren't each
862 lists and replace is True, the values in self are replaced with the
863 values in dict_; if the values from self and dict_ for the given
864 identifier are both of list type, then the two lists are concatenated
865 and the result stored in self; otherwise, the values in self are
866 preserved. When and_source is True, the 'source' attribute is
867 included in the copy.
869 NOTE: When replace is False, and self contains a 'source' attribute,
870 'source' is not replaced even when dict_ has a 'source'
871 attribute, though it may still be merged into a list depending
872 on the value of update_fun.
874 self
.update_all_atts(dict_
, Element
.copy_attr_concatenate
, replace
,
877 def update_all_atts_coercion(self
, dict_
, replace
= True,
880 Updates all attributes from node or dictionary `dict_`.
882 Appends the basic attributes ('ids', 'names', 'classes',
883 'dupnames', but not 'source') and then, for all other attributes in
884 dict_, updates the same attribute in self. When attributes with the
885 same identifier appear in both self and dict_ whose values are both
886 not lists and replace is True, the values in self are replaced with
887 the values in dict_; if either of the values from self and dict_ for
888 the given identifier are of list type, then first any non-lists are
889 converted to 1-element lists and then the two lists are concatenated
890 and the result stored in self; otherwise, the values in self are
891 preserved. When and_source is True, the 'source' attribute is
892 included in the copy.
894 NOTE: When replace is False, and self contains a 'source' attribute,
895 'source' is not replaced even when dict_ has a 'source'
896 attribute, though it may still be merged into a list depending
897 on the value of update_fun.
899 self
.update_all_atts(dict_
, Element
.copy_attr_coerce
, replace
,
902 def update_all_atts_convert(self
, dict_
, and_source
= False):
904 Updates all attributes from node or dictionary `dict_`.
906 Appends the basic attributes ('ids', 'names', 'classes',
907 'dupnames', but not 'source') and then, for all other attributes in
908 dict_, updates the same attribute in self. When attributes with the
909 same identifier appear in both self and dict_ then first any non-lists
910 are converted to 1-element lists and then the two lists are
911 concatenated and the result stored in self; otherwise, the values in
912 self are preserved. When and_source is True, the 'source' attribute
913 is included in the copy.
915 NOTE: When replace is False, and self contains a 'source' attribute,
916 'source' is not replaced even when dict_ has a 'source'
917 attribute, though it may still be merged into a list depending
918 on the value of update_fun.
920 self
.update_all_atts(dict_
, Element
.copy_attr_convert
,
921 and_source
= and_source
)
926 def replace(self
, old
, new
):
927 """Replace one child `Node` with another child or children."""
928 index
= self
.index(old
)
929 if isinstance(new
, Node
):
930 self
.setup_child(new
)
932 elif new
is not None:
933 self
[index
:index
+1] = new
935 def replace_self(self
, new
):
937 Replace `self` node with `new`, where `new` is a node or a
941 if not isinstance(new
, Node
):
942 # `new` is a list; update first child.
947 if isinstance(update
, Element
):
948 update
.update_basic_atts(self
)
950 # `update` is a Text node or `new` is an empty list.
951 # Assert that we aren't losing any attributes.
952 for att
in self
.basic_attributes
:
953 assert not self
[att
], \
954 'Losing "%s" attribute: %s' % (att
, self
[att
])
955 self
.parent
.replace(self
, new
)
957 def first_child_matching_class(self
, childclass
, start
=0, end
=sys
.maxint
):
959 Return the index of the first child whose class exactly matches.
963 - `childclass`: A `Node` subclass to search for, or a tuple of `Node`
964 classes. If a tuple, any of the classes may match.
965 - `start`: Initial index to check.
966 - `end`: Initial index to *not* check.
968 if not isinstance(childclass
, tuple):
969 childclass
= (childclass
,)
970 for index
in range(start
, min(len(self
), end
)):
972 if isinstance(self
[index
], c
):
976 def first_child_not_matching_class(self
, childclass
, start
=0,
979 Return the index of the first child whose class does *not* match.
983 - `childclass`: A `Node` subclass to skip, or a tuple of `Node`
984 classes. If a tuple, none of the classes may match.
985 - `start`: Initial index to check.
986 - `end`: Initial index to *not* check.
988 if not isinstance(childclass
, tuple):
989 childclass
= (childclass
,)
990 for index
in range(start
, min(len(self
), end
)):
992 if isinstance(self
.children
[index
], c
):
998 def pformat(self
, indent
=' ', level
=0):
999 return ''.join(['%s%s\n' % (indent
* level
, self
.starttag())] +
1000 [child
.pformat(indent
, level
+1)
1001 for child
in self
.children
])
1004 return self
.__class
__(rawsource
=self
.rawsource
, **self
.attributes
)
1008 copy
.extend([child
.deepcopy() for child
in self
.children
])
1011 def set_class(self
, name
):
1012 """Add a new class to the "classes" attribute."""
1013 warnings
.warn('docutils.nodes.Element.set_class deprecated; '
1014 "append to Element['classes'] list attribute directly",
1015 DeprecationWarning, stacklevel
=2)
1016 assert ' ' not in name
1017 self
['classes'].append(name
.lower())
1019 def note_referenced_by(self
, name
=None, id=None):
1020 """Note that this Element has been referenced by its name
1021 `name` or id `id`."""
1023 # Element.expect_referenced_by_* dictionaries map names or ids
1024 # to nodes whose ``referenced`` attribute is set to true as
1025 # soon as this node is referenced by the given name or id.
1026 # Needed for target propagation.
1027 by_name
= getattr(self
, 'expect_referenced_by_name', {}).get(name
)
1028 by_id
= getattr(self
, 'expect_referenced_by_id', {}).get(id)
1030 assert name
is not None
1031 by_name
.referenced
= 1
1033 assert id is not None
1034 by_id
.referenced
= 1
1037 def is_not_list_attribute(cls
, attr
):
1039 Returns True if and only if the given attribute is NOT one of the
1040 basic list attributes defined for all Elements.
1042 return attr
not in cls
.list_attributes
1045 def is_not_known_attribute(cls
, attr
):
1047 Returns True if and only if the given attribute is NOT recognized by
1050 return attr
not in cls
.known_attributes
1053 class TextElement(Element
):
1056 An element which directly contains text.
1058 Its children are all `Text` or `Inline` subclass nodes. You can
1059 check whether an element's context is inline simply by checking whether
1060 its immediate parent is a `TextElement` instance (including subclasses).
1061 This is handy for nodes like `image` that can appear both inline and as
1062 standalone body elements.
1064 If passing children to `__init__()`, make sure to set `text` to
1065 ``''`` or some other suitable value.
1068 child_text_separator
= ''
1069 """Separator for child nodes, used by `astext()` method."""
1071 def __init__(self
, rawsource
='', text
='', *children
, **attributes
):
1073 textnode
= Text(text
)
1074 Element
.__init
__(self
, rawsource
, textnode
, *children
,
1077 Element
.__init
__(self
, rawsource
, *children
, **attributes
)
1080 class FixedTextElement(TextElement
):
1082 """An element which directly contains preformatted text."""
1084 def __init__(self
, rawsource
='', text
='', *children
, **attributes
):
1085 TextElement
.__init
__(self
, rawsource
, text
, *children
, **attributes
)
1086 self
.attributes
['xml:space'] = 'preserve'
1100 def add_backref(self
, refid
):
1101 self
['backrefs'].append(refid
)
1104 # ====================
1105 # Element Categories
1106 # ====================
1112 class PreBibliographic
:
1113 """Category of Node which may occur before Bibliographic Nodes."""
1115 class Bibliographic
: pass
1117 class Decorative(PreBibliographic
): pass
1119 class Structural
: pass
1123 class General(Body
): pass
1125 class Sequential(Body
):
1126 """List-like elements."""
1128 class Admonition(Body
): pass
1130 class Special(Body
):
1131 """Special internal body elements."""
1133 class Invisible(PreBibliographic
):
1134 """Internal elements that don't appear in output."""
1140 class Referential(Resolvable
): pass
1143 class Targetable(Resolvable
):
1147 indirect_reference_name
= None
1148 """Holds the whitespace_normalized_name (contains mixed case) of a target.
1149 Required for MoinMoin/reST compatibility."""
1153 """Contains a `label` as its first element."""
1160 class document(Root
, Structural
, Element
):
1163 The document root element.
1165 Do not instantiate this class directly; use
1166 `docutils.utils.new_document()` instead.
1169 def __init__(self
, settings
, reporter
, *args
, **kwargs
):
1170 Element
.__init
__(self
, *args
, **kwargs
)
1172 self
.current_source
= None
1173 """Path to or description of the input source being processed."""
1175 self
.current_line
= None
1176 """Line number (1-based) of `current_source`."""
1178 self
.settings
= settings
1179 """Runtime settings data record."""
1181 self
.reporter
= reporter
1182 """System message generator."""
1184 self
.indirect_targets
= []
1185 """List of indirect target nodes."""
1187 self
.substitution_defs
= {}
1188 """Mapping of substitution names to substitution_definition nodes."""
1190 self
.substitution_names
= {}
1191 """Mapping of case-normalized substitution names to case-sensitive
1195 """Mapping of names to lists of referencing nodes."""
1198 """Mapping of ids to lists of referencing nodes."""
1201 """Mapping of names to unique id's."""
1204 """Mapping of names to hyperlink type (boolean: True => explicit,
1205 False => implicit."""
1208 """Mapping of ids to nodes."""
1210 self
.footnote_refs
= {}
1211 """Mapping of footnote labels to lists of footnote_reference nodes."""
1213 self
.citation_refs
= {}
1214 """Mapping of citation labels to lists of citation_reference nodes."""
1216 self
.autofootnotes
= []
1217 """List of auto-numbered footnote nodes."""
1219 self
.autofootnote_refs
= []
1220 """List of auto-numbered footnote_reference nodes."""
1222 self
.symbol_footnotes
= []
1223 """List of symbol footnote nodes."""
1225 self
.symbol_footnote_refs
= []
1226 """List of symbol footnote_reference nodes."""
1229 """List of manually-numbered footnote nodes."""
1232 """List of citation nodes."""
1234 self
.autofootnote_start
= 1
1235 """Initial auto-numbered footnote number."""
1237 self
.symbol_footnote_start
= 0
1238 """Initial symbol footnote symbol index."""
1241 """Initial ID number."""
1243 self
.parse_messages
= []
1244 """System messages generated while parsing."""
1246 self
.transform_messages
= []
1247 """System messages generated while applying transforms."""
1249 import docutils
.transforms
1250 self
.transformer
= docutils
.transforms
.Transformer(self
)
1251 """Storage for transforms to be applied to this document."""
1253 self
.decoration
= None
1254 """Document's `decoration` node."""
1256 self
.document
= self
1258 def __getstate__(self
):
1260 Return dict with unpicklable references removed.
1262 state
= self
.__dict
__.copy()
1263 state
['reporter'] = None
1264 state
['transformer'] = None
1267 def asdom(self
, dom
=None):
1268 """Return a DOM representation of this document."""
1270 import xml
.dom
.minidom
as dom
1271 domroot
= dom
.Document()
1272 domroot
.appendChild(self
._dom
_node
(domroot
))
1275 def set_id(self
, node
, msgnode
=None):
1276 for id in node
['ids']:
1277 if id in self
.ids
and self
.ids
[id] is not node
:
1278 msg
= self
.reporter
.severe('Duplicate ID: "%s".' % id)
1282 for name
in node
['names']:
1283 id = self
.settings
.id_prefix
+ make_id(name
)
1284 if id and id not in self
.ids
:
1288 while not id or id in self
.ids
:
1289 id = (self
.settings
.id_prefix
+
1290 self
.settings
.auto_id_prefix
+ str(self
.id_start
))
1292 node
['ids'].append(id)
1296 def set_name_id_map(self
, node
, id, msgnode
=None, explicit
=None):
1298 `self.nameids` maps names to IDs, while `self.nametypes` maps names to
1299 booleans representing hyperlink type (True==explicit,
1300 False==implicit). This method updates the mappings.
1302 The following state transition table shows how `self.nameids` ("ids")
1303 and `self.nametypes` ("types") change with new input (a call to this
1304 method), and what actions are performed ("implicit"-type system
1305 messages are INFO/1, and "explicit"-type system messages are ERROR/3):
1307 ==== ===== ======== ======== ======= ==== ===== =====
1308 Old State Input Action New State Notes
1309 ----------- -------- ----------------- ----------- -----
1310 ids types new type sys.msg. dupname ids types
1311 ==== ===== ======== ======== ======= ==== ===== =====
1312 - - explicit - - new True
1313 - - implicit - - new False
1314 None False explicit - - new True
1315 old False explicit implicit old new True
1316 None True explicit explicit new None True
1317 old True explicit explicit new,old None True [#]_
1318 None False implicit implicit new None False
1319 old False implicit implicit new,old None False
1320 None True implicit implicit new None True
1321 old True implicit implicit new old True
1322 ==== ===== ======== ======== ======= ==== ===== =====
1324 .. [#] Do not clear the name-to-id map or invalidate the old target if
1325 both old and new targets are external and refer to identical URIs.
1326 The new target is invalidated regardless.
1328 for name
in node
['names']:
1329 if name
in self
.nameids
:
1330 self
.set_duplicate_name_id(node
, id, name
, msgnode
, explicit
)
1332 self
.nameids
[name
] = id
1333 self
.nametypes
[name
] = explicit
1335 def set_duplicate_name_id(self
, node
, id, name
, msgnode
, explicit
):
1336 old_id
= self
.nameids
[name
]
1337 old_explicit
= self
.nametypes
[name
]
1338 self
.nametypes
[name
] = old_explicit
or explicit
1342 if old_id
is not None:
1343 old_node
= self
.ids
[old_id
]
1344 if 'refuri' in node
:
1345 refuri
= node
['refuri']
1346 if old_node
['names'] \
1347 and 'refuri' in old_node \
1348 and old_node
['refuri'] == refuri
:
1349 level
= 1 # just inform if refuri's identical
1351 dupname(old_node
, name
)
1352 self
.nameids
[name
] = None
1353 msg
= self
.reporter
.system_message(
1354 level
, 'Duplicate explicit target name: "%s".' % name
,
1355 backrefs
=[id], base_node
=node
)
1360 self
.nameids
[name
] = id
1361 if old_id
is not None:
1362 old_node
= self
.ids
[old_id
]
1363 dupname(old_node
, name
)
1365 if old_id
is not None and not old_explicit
:
1366 self
.nameids
[name
] = None
1367 old_node
= self
.ids
[old_id
]
1368 dupname(old_node
, name
)
1370 if not explicit
or (not old_explicit
and old_id
is not None):
1371 msg
= self
.reporter
.info(
1372 'Duplicate implicit target name: "%s".' % name
,
1373 backrefs
=[id], base_node
=node
)
1377 def has_name(self
, name
):
1378 return name
in self
.nameids
1380 # "note" here is an imperative verb: "take note of".
1381 def note_implicit_target(self
, target
, msgnode
=None):
1382 id = self
.set_id(target
, msgnode
)
1383 self
.set_name_id_map(target
, id, msgnode
, explicit
=None)
1385 def note_explicit_target(self
, target
, msgnode
=None):
1386 id = self
.set_id(target
, msgnode
)
1387 self
.set_name_id_map(target
, id, msgnode
, explicit
=True)
1389 def note_refname(self
, node
):
1390 self
.refnames
.setdefault(node
['refname'], []).append(node
)
1392 def note_refid(self
, node
):
1393 self
.refids
.setdefault(node
['refid'], []).append(node
)
1395 def note_indirect_target(self
, target
):
1396 self
.indirect_targets
.append(target
)
1398 self
.note_refname(target
)
1400 def note_anonymous_target(self
, target
):
1403 def note_autofootnote(self
, footnote
):
1404 self
.set_id(footnote
)
1405 self
.autofootnotes
.append(footnote
)
1407 def note_autofootnote_ref(self
, ref
):
1409 self
.autofootnote_refs
.append(ref
)
1411 def note_symbol_footnote(self
, footnote
):
1412 self
.set_id(footnote
)
1413 self
.symbol_footnotes
.append(footnote
)
1415 def note_symbol_footnote_ref(self
, ref
):
1417 self
.symbol_footnote_refs
.append(ref
)
1419 def note_footnote(self
, footnote
):
1420 self
.set_id(footnote
)
1421 self
.footnotes
.append(footnote
)
1423 def note_footnote_ref(self
, ref
):
1425 self
.footnote_refs
.setdefault(ref
['refname'], []).append(ref
)
1426 self
.note_refname(ref
)
1428 def note_citation(self
, citation
):
1429 self
.citations
.append(citation
)
1431 def note_citation_ref(self
, ref
):
1433 self
.citation_refs
.setdefault(ref
['refname'], []).append(ref
)
1434 self
.note_refname(ref
)
1436 def note_substitution_def(self
, subdef
, def_name
, msgnode
=None):
1437 name
= whitespace_normalize_name(def_name
)
1438 if name
in self
.substitution_defs
:
1439 msg
= self
.reporter
.error(
1440 'Duplicate substitution definition name: "%s".' % name
,
1444 oldnode
= self
.substitution_defs
[name
]
1445 dupname(oldnode
, name
)
1446 # keep only the last definition:
1447 self
.substitution_defs
[name
] = subdef
1448 # case-insensitive mapping:
1449 self
.substitution_names
[fully_normalize_name(name
)] = name
1451 def note_substitution_ref(self
, subref
, refname
):
1452 subref
['refname'] = whitespace_normalize_name(refname
)
1454 def note_pending(self
, pending
, priority
=None):
1455 self
.transformer
.add_pending(pending
, priority
)
1457 def note_parse_message(self
, message
):
1458 self
.parse_messages
.append(message
)
1460 def note_transform_message(self
, message
):
1461 self
.transform_messages
.append(message
)
1463 def note_source(self
, source
, offset
):
1464 self
.current_source
= source
1466 self
.current_line
= offset
1468 self
.current_line
= offset
+ 1
1471 return self
.__class
__(self
.settings
, self
.reporter
,
1474 def get_decoration(self
):
1475 if not self
.decoration
:
1476 self
.decoration
= decoration()
1477 index
= self
.first_child_not_matching_class(Titular
)
1479 self
.append(self
.decoration
)
1481 self
.insert(index
, self
.decoration
)
1482 return self
.decoration
1489 class title(Titular
, PreBibliographic
, TextElement
): pass
1490 class subtitle(Titular
, PreBibliographic
, TextElement
): pass
1491 class rubric(Titular
, TextElement
): pass
1494 # ========================
1495 # Bibliographic Elements
1496 # ========================
1498 class docinfo(Bibliographic
, Element
): pass
1499 class author(Bibliographic
, TextElement
): pass
1500 class authors(Bibliographic
, Element
): pass
1501 class organization(Bibliographic
, TextElement
): pass
1502 class address(Bibliographic
, FixedTextElement
): pass
1503 class contact(Bibliographic
, TextElement
): pass
1504 class version(Bibliographic
, TextElement
): pass
1505 class revision(Bibliographic
, TextElement
): pass
1506 class status(Bibliographic
, TextElement
): pass
1507 class date(Bibliographic
, TextElement
): pass
1508 class copyright(Bibliographic
, TextElement
): pass
1511 # =====================
1512 # Decorative Elements
1513 # =====================
1515 class decoration(Decorative
, Element
):
1517 def get_header(self
):
1518 if not len(self
.children
) or not isinstance(self
.children
[0], header
):
1519 self
.insert(0, header())
1520 return self
.children
[0]
1522 def get_footer(self
):
1523 if not len(self
.children
) or not isinstance(self
.children
[-1], footer
):
1524 self
.append(footer())
1525 return self
.children
[-1]
1528 class header(Decorative
, Element
): pass
1529 class footer(Decorative
, Element
): pass
1532 # =====================
1533 # Structural Elements
1534 # =====================
1536 class section(Structural
, Element
): pass
1539 class topic(Structural
, Element
):
1542 Topics are terminal, "leaf" mini-sections, like block quotes with titles,
1543 or textual figures. A topic is just like a section, except that it has no
1544 subsections, and it doesn't have to conform to section placement rules.
1546 Topics are allowed wherever body elements (list, table, etc.) are allowed,
1547 but only at the top level of a section or document. Topics cannot nest
1548 inside topics, sidebars, or body elements; you can't have a topic inside a
1549 table, list, block quote, etc.
1553 class sidebar(Structural
, Element
):
1556 Sidebars are like miniature, parallel documents that occur inside other
1557 documents, providing related or reference material. A sidebar is
1558 typically offset by a border and "floats" to the side of the page; the
1559 document's main text may flow around it. Sidebars can also be likened to
1560 super-footnotes; their content is outside of the flow of the document's
1563 Sidebars are allowed wherever body elements (list, table, etc.) are
1564 allowed, but only at the top level of a section or document. Sidebars
1565 cannot nest inside sidebars, topics, or body elements; you can't have a
1566 sidebar inside a table, list, block quote, etc.
1570 class transition(Structural
, Element
): pass
1577 class paragraph(General
, TextElement
): pass
1578 class compound(General
, Element
): pass
1579 class container(General
, Element
): pass
1580 class bullet_list(Sequential
, Element
): pass
1581 class enumerated_list(Sequential
, Element
): pass
1582 class list_item(Part
, Element
): pass
1583 class definition_list(Sequential
, Element
): pass
1584 class definition_list_item(Part
, Element
): pass
1585 class term(Part
, TextElement
): pass
1586 class classifier(Part
, TextElement
): pass
1587 class definition(Part
, Element
): pass
1588 class field_list(Sequential
, Element
): pass
1589 class field(Part
, Element
): pass
1590 class field_name(Part
, TextElement
): pass
1591 class field_body(Part
, Element
): pass
1594 class option(Part
, Element
):
1596 child_text_separator
= ''
1599 class option_argument(Part
, TextElement
):
1602 return self
.get('delimiter', ' ') + TextElement
.astext(self
)
1605 class option_group(Part
, Element
):
1607 child_text_separator
= ', '
1610 class option_list(Sequential
, Element
): pass
1613 class option_list_item(Part
, Element
):
1615 child_text_separator
= ' '
1618 class option_string(Part
, TextElement
): pass
1619 class description(Part
, Element
): pass
1620 class literal_block(General
, FixedTextElement
): pass
1621 class doctest_block(General
, FixedTextElement
): pass
1622 class math_block(General
, FixedTextElement
): pass
1623 class line_block(General
, Element
): pass
1626 class line(Part
, TextElement
):
1631 class block_quote(General
, Element
): pass
1632 class attribution(Part
, TextElement
): pass
1633 class attention(Admonition
, Element
): pass
1634 class caution(Admonition
, Element
): pass
1635 class danger(Admonition
, Element
): pass
1636 class error(Admonition
, Element
): pass
1637 class important(Admonition
, Element
): pass
1638 class note(Admonition
, Element
): pass
1639 class tip(Admonition
, Element
): pass
1640 class hint(Admonition
, Element
): pass
1641 class warning(Admonition
, Element
): pass
1642 class admonition(Admonition
, Element
): pass
1643 class comment(Special
, Invisible
, FixedTextElement
): pass
1644 class substitution_definition(Special
, Invisible
, TextElement
): pass
1645 class target(Special
, Invisible
, Inline
, TextElement
, Targetable
): pass
1646 class footnote(General
, BackLinkable
, Element
, Labeled
, Targetable
): pass
1647 class citation(General
, BackLinkable
, Element
, Labeled
, Targetable
): pass
1648 class label(Part
, TextElement
): pass
1649 class figure(General
, Element
): pass
1650 class caption(Part
, TextElement
): pass
1651 class legend(Part
, Element
): pass
1652 class table(General
, Element
): pass
1653 class tgroup(Part
, Element
): pass
1654 class colspec(Part
, Element
): pass
1655 class thead(Part
, Element
): pass
1656 class tbody(Part
, Element
): pass
1657 class row(Part
, Element
): pass
1658 class entry(Part
, Element
): pass
1661 class system_message(Special
, BackLinkable
, PreBibliographic
, Element
):
1664 System message element.
1666 Do not instantiate this class directly; use
1667 ``document.reporter.info/warning/error/severe()`` instead.
1670 def __init__(self
, message
=None, *children
, **attributes
):
1672 p
= paragraph('', message
)
1673 children
= (p
,) + children
1675 Element
.__init
__(self
, '', *children
, **attributes
)
1677 print 'system_message: children=%r' % (children
,)
1681 line
= self
.get('line', '')
1682 return u
'%s:%s: (%s/%s) %s' % (self
['source'], line
, self
['type'],
1683 self
['level'], Element
.astext(self
))
1686 class pending(Special
, Invisible
, Element
):
1689 The "pending" element is used to encapsulate a pending operation: the
1690 operation (transform), the point at which to apply it, and any data it
1691 requires. Only the pending operation's location within the document is
1692 stored in the public document tree (by the "pending" object itself); the
1693 operation and its data are stored in the "pending" object's internal
1694 instance attributes.
1696 For example, say you want a table of contents in your reStructuredText
1697 document. The easiest way to specify where to put it is from within the
1698 document, with a directive::
1702 But the "contents" directive can't do its work until the entire document
1703 has been parsed and possibly transformed to some extent. So the directive
1704 code leaves a placeholder behind that will trigger the second phase of its
1705 processing, something like this::
1707 <pending ...public attributes...> + internal attributes
1709 Use `document.note_pending()` so that the
1710 `docutils.transforms.Transformer` stage of processing can run all pending
1714 def __init__(self
, transform
, details
=None,
1715 rawsource
='', *children
, **attributes
):
1716 Element
.__init
__(self
, rawsource
, *children
, **attributes
)
1718 self
.transform
= transform
1719 """The `docutils.transforms.Transform` class implementing the pending
1722 self
.details
= details
or {}
1723 """Detail data (dictionary) required by the pending operation."""
1725 def pformat(self
, indent
=' ', level
=0):
1727 '.. internal attributes:',
1728 ' .transform: %s.%s' % (self
.transform
.__module
__,
1729 self
.transform
.__name
__),
1731 details
= self
.details
.items()
1733 for key
, value
in details
:
1734 if isinstance(value
, Node
):
1735 internals
.append('%7s%s:' % ('', key
))
1736 internals
.extend(['%9s%s' % ('', line
)
1737 for line
in value
.pformat().splitlines()])
1738 elif value
and isinstance(value
, list) \
1739 and isinstance(value
[0], Node
):
1740 internals
.append('%7s%s:' % ('', key
))
1742 internals
.extend(['%9s%s' % ('', line
)
1743 for line
in v
.pformat().splitlines()])
1745 internals
.append('%7s%s: %r' % ('', key
, value
))
1746 return (Element
.pformat(self
, indent
, level
)
1747 + ''.join([(' %s%s\n' % (indent
* level
, line
))
1748 for line
in internals
]))
1751 return self
.__class
__(self
.transform
, self
.details
, self
.rawsource
,
1755 class raw(Special
, Inline
, PreBibliographic
, FixedTextElement
):
1758 Raw data that is to be passed untouched to the Writer.
1768 class emphasis(Inline
, TextElement
): pass
1769 class strong(Inline
, TextElement
): pass
1770 class literal(Inline
, TextElement
): pass
1771 class reference(General
, Inline
, Referential
, TextElement
): pass
1772 class footnote_reference(Inline
, Referential
, TextElement
): pass
1773 class citation_reference(Inline
, Referential
, TextElement
): pass
1774 class substitution_reference(Inline
, TextElement
): pass
1775 class title_reference(Inline
, TextElement
): pass
1776 class abbreviation(Inline
, TextElement
): pass
1777 class acronym(Inline
, TextElement
): pass
1778 class superscript(Inline
, TextElement
): pass
1779 class subscript(Inline
, TextElement
): pass
1780 class math(Inline
, TextElement
): pass
1783 class image(General
, Inline
, Element
):
1786 return self
.get('alt', '')
1789 class inline(Inline
, TextElement
): pass
1790 class problematic(Inline
, TextElement
): pass
1791 class generated(Inline
, TextElement
): pass
1794 # ========================================
1795 # Auxiliary Classes, Functions, and Data
1796 # ========================================
1798 node_class_names
= """
1800 abbreviation acronym address admonition attention attribution author
1802 block_quote bullet_list
1803 caption caution citation citation_reference classifier colspec comment
1804 compound contact container copyright
1805 danger date decoration definition definition_list definition_list_item
1806 description docinfo doctest_block document
1807 emphasis entry enumerated_list error
1808 field field_body field_list field_name figure footer
1809 footnote footnote_reference
1812 image important inline
1813 label legend line line_block list_item literal literal_block
1816 option option_argument option_group option_list option_list_item
1817 option_string organization
1818 paragraph pending problematic
1819 raw reference revision row rubric
1820 section sidebar status strong subscript substitution_definition
1821 substitution_reference subtitle superscript system_message
1822 table target tbody term tgroup thead tip title title_reference topic
1826 """A list of names of all concrete Node subclasses."""
1832 "Visitor" pattern [GoF95]_ abstract superclass implementation for
1833 document tree traversals.
1835 Each node class has corresponding methods, doing nothing by
1836 default; override individual methods for specific and useful
1837 behaviour. The `dispatch_visit()` method is called by
1838 `Node.walk()` upon entering a node. `Node.walkabout()` also calls
1839 the `dispatch_departure()` method before exiting a node.
1841 The dispatch methods call "``visit_`` + node class name" or
1842 "``depart_`` + node class name", resp.
1844 This is a base class for visitors whose ``visit_...`` & ``depart_...``
1845 methods should be implemented for *all* node types encountered (such as
1846 for `docutils.writers.Writer` subclasses). Unimplemented methods will
1849 For sparse traversals, where only certain node types are of interest,
1850 subclass `SparseNodeVisitor` instead. When (mostly or entirely) uniform
1851 processing is desired, subclass `GenericNodeVisitor`.
1853 .. [GoF95] Gamma, Helm, Johnson, Vlissides. *Design Patterns: Elements of
1854 Reusable Object-Oriented Software*. Addison-Wesley, Reading, MA, USA,
1860 Tuple containing node class names (as strings).
1862 No exception will be raised if writers do not implement visit
1863 or departure functions for these node classes.
1865 Used to ensure transitional compatibility with existing 3rd-party writers.
1868 def __init__(self
, document
):
1869 self
.document
= document
1871 def dispatch_visit(self
, node
):
1873 Call self."``visit_`` + node class name" with `node` as
1874 parameter. If the ``visit_...`` method does not exist, call
1877 node_name
= node
.__class
__.__name
__
1878 method
= getattr(self
, 'visit_' + node_name
, self
.unknown_visit
)
1879 self
.document
.reporter
.debug(
1880 'docutils.nodes.NodeVisitor.dispatch_visit calling %s for %s'
1881 % (method
.__name
__, node_name
))
1884 def dispatch_departure(self
, node
):
1886 Call self."``depart_`` + node class name" with `node` as
1887 parameter. If the ``depart_...`` method does not exist, call
1888 self.unknown_departure.
1890 node_name
= node
.__class
__.__name
__
1891 method
= getattr(self
, 'depart_' + node_name
, self
.unknown_departure
)
1892 self
.document
.reporter
.debug(
1893 'docutils.nodes.NodeVisitor.dispatch_departure calling %s for %s'
1894 % (method
.__name
__, node_name
))
1897 def unknown_visit(self
, node
):
1899 Called when entering unknown `Node` types.
1901 Raise an exception unless overridden.
1903 if (self
.document
.settings
.strict_visitor
1904 or node
.__class
__.__name
__ not in self
.optional
):
1905 raise NotImplementedError(
1906 '%s visiting unknown node type: %s'
1907 % (self
.__class
__, node
.__class
__.__name
__))
1909 def unknown_departure(self
, node
):
1911 Called before exiting unknown `Node` types.
1913 Raise exception unless overridden.
1915 if (self
.document
.settings
.strict_visitor
1916 or node
.__class
__.__name
__ not in self
.optional
):
1917 raise NotImplementedError(
1918 '%s departing unknown node type: %s'
1919 % (self
.__class
__, node
.__class
__.__name
__))
1922 class SparseNodeVisitor(NodeVisitor
):
1925 Base class for sparse traversals, where only certain node types are of
1926 interest. When ``visit_...`` & ``depart_...`` methods should be
1927 implemented for *all* node types (such as for `docutils.writers.Writer`
1928 subclasses), subclass `NodeVisitor` instead.
1932 class GenericNodeVisitor(NodeVisitor
):
1935 Generic "Visitor" abstract superclass, for simple traversals.
1937 Unless overridden, each ``visit_...`` method calls `default_visit()`, and
1938 each ``depart_...`` method (when using `Node.walkabout()`) calls
1939 `default_departure()`. `default_visit()` (and `default_departure()`) must
1940 be overridden in subclasses.
1942 Define fully generic visitors by overriding `default_visit()` (and
1943 `default_departure()`) only. Define semi-generic visitors by overriding
1944 individual ``visit_...()`` (and ``depart_...()``) methods also.
1946 `NodeVisitor.unknown_visit()` (`NodeVisitor.unknown_departure()`) should
1947 be overridden for default behavior.
1950 def default_visit(self
, node
):
1951 """Override for generic, uniform traversals."""
1952 raise NotImplementedError
1954 def default_departure(self
, node
):
1955 """Override for generic, uniform traversals."""
1956 raise NotImplementedError
1958 def _call_default_visit(self
, node
):
1959 self
.default_visit(node
)
1961 def _call_default_departure(self
, node
):
1962 self
.default_departure(node
)
1964 def _nop(self
, node
):
1967 def _add_node_class_names(names
):
1968 """Save typing with dynamic assignments:"""
1970 setattr(GenericNodeVisitor
, "visit_" + _name
, _call_default_visit
)
1971 setattr(GenericNodeVisitor
, "depart_" + _name
, _call_default_departure
)
1972 setattr(SparseNodeVisitor
, 'visit_' + _name
, _nop
)
1973 setattr(SparseNodeVisitor
, 'depart_' + _name
, _nop
)
1975 _add_node_class_names(node_class_names
)
1978 class TreeCopyVisitor(GenericNodeVisitor
):
1981 Make a complete copy of a tree or branch, including element attributes.
1984 def __init__(self
, document
):
1985 GenericNodeVisitor
.__init
__(self
, document
)
1986 self
.parent_stack
= []
1989 def get_tree_copy(self
):
1990 return self
.parent
[0]
1992 def default_visit(self
, node
):
1993 """Copy the current node, and make it the new acting parent."""
1994 newnode
= node
.copy()
1995 self
.parent
.append(newnode
)
1996 self
.parent_stack
.append(self
.parent
)
1997 self
.parent
= newnode
1999 def default_departure(self
, node
):
2000 """Restore the previous acting parent."""
2001 self
.parent
= self
.parent_stack
.pop()
2004 class TreePruningException(Exception):
2007 Base class for `NodeVisitor`-related tree pruning exceptions.
2009 Raise subclasses from within ``visit_...`` or ``depart_...`` methods
2010 called from `Node.walk()` and `Node.walkabout()` tree traversals to prune
2017 class SkipChildren(TreePruningException
):
2020 Do not visit any children of the current node. The current node's
2021 siblings and ``depart_...`` method are not affected.
2027 class SkipSiblings(TreePruningException
):
2030 Do not visit any more siblings (to the right) of the current node. The
2031 current node's children and its ``depart_...`` method are not affected.
2037 class SkipNode(TreePruningException
):
2040 Do not visit the current node's children, and do not call the current
2041 node's ``depart_...`` method.
2047 class SkipDeparture(TreePruningException
):
2050 Do not call the current node's ``depart_...`` method. The current node's
2051 children and siblings are not affected.
2057 class NodeFound(TreePruningException
):
2060 Raise to indicate that the target of a search has been found. This
2061 exception must be caught by the client; it is not caught by the traversal
2068 class StopTraversal(TreePruningException
):
2071 Stop the traversal alltogether. The current node's ``depart_...`` method
2072 is not affected. The parent nodes ``depart_...`` methods are also called
2073 as usual. No other nodes are visited. This is an alternative to
2074 NodeFound that does not cause exception handling to trickle up to the
2081 def make_id(string
):
2083 Convert `string` into an identifier and return it.
2085 Docutils identifiers will conform to the regular expression
2086 ``[a-z](-?[a-z0-9]+)*``. For CSS compatibility, identifiers (the "class"
2087 and "id" attributes) should have no underscores, colons, or periods.
2088 Hyphens may be used.
2090 - The `HTML 4.01 spec`_ defines identifiers based on SGML tokens:
2092 ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
2093 followed by any number of letters, digits ([0-9]), hyphens ("-"),
2094 underscores ("_"), colons (":"), and periods (".").
2096 - However the `CSS1 spec`_ defines identifiers based on the "name" token,
2097 a tighter interpretation ("flex" tokenizer notation; "latin1" and
2098 "escape" 8-bit characters have been replaced with entities)::
2100 unicode \\[0-9a-f]{1,4}
2101 latin1 [¡-ÿ]
2102 escape {unicode}|\\[ -~¡-ÿ]
2103 nmchar [-a-z0-9]|{latin1}|{escape}
2106 The CSS1 "nmchar" rule does not include underscores ("_"), colons (":"),
2107 or periods ("."), therefore "class" and "id" attributes should not contain
2108 these characters. They should be replaced with hyphens ("-"). Combined
2109 with HTML's requirements (the first character must be a letter; no
2110 "unicode", "latin1", or "escape" characters), this results in the
2111 ``[a-z](-?[a-z0-9]+)*`` pattern.
2113 .. _HTML 4.01 spec: http://www.w3.org/TR/html401
2114 .. _CSS1 spec: http://www.w3.org/TR/REC-CSS1
2117 if not isinstance(id, unicode):
2119 id = id.translate(_non_id_translate_digraphs
)
2120 id = id.translate(_non_id_translate
)
2121 # get rid of non-ascii characters.
2122 # 'ascii' lowercase to prevent problems with turkish locale.
2123 id = unicodedata
.normalize('NFKD', id).\
2124 encode('ascii', 'ignore').decode('ascii')
2125 # shrink runs of whitespace and replace by hyphen
2126 id = _non_id_chars
.sub('-', ' '.join(id.split()))
2127 id = _non_id_at_ends
.sub('', id)
2130 _non_id_chars
= re
.compile('[^a-z0-9]+')
2131 _non_id_at_ends
= re
.compile('^[-0-9]+|-+$')
2132 _non_id_translate
= {
2133 0x00f8: u
'o', # o with stroke
2134 0x0111: u
'd', # d with stroke
2135 0x0127: u
'h', # h with stroke
2136 0x0131: u
'i', # dotless i
2137 0x0142: u
'l', # l with stroke
2138 0x0167: u
't', # t with stroke
2139 0x0180: u
'b', # b with stroke
2140 0x0183: u
'b', # b with topbar
2141 0x0188: u
'c', # c with hook
2142 0x018c: u
'd', # d with topbar
2143 0x0192: u
'f', # f with hook
2144 0x0199: u
'k', # k with hook
2145 0x019a: u
'l', # l with bar
2146 0x019e: u
'n', # n with long right leg
2147 0x01a5: u
'p', # p with hook
2148 0x01ab: u
't', # t with palatal hook
2149 0x01ad: u
't', # t with hook
2150 0x01b4: u
'y', # y with hook
2151 0x01b6: u
'z', # z with stroke
2152 0x01e5: u
'g', # g with stroke
2153 0x0225: u
'z', # z with hook
2154 0x0234: u
'l', # l with curl
2155 0x0235: u
'n', # n with curl
2156 0x0236: u
't', # t with curl
2157 0x0237: u
'j', # dotless j
2158 0x023c: u
'c', # c with stroke
2159 0x023f: u
's', # s with swash tail
2160 0x0240: u
'z', # z with swash tail
2161 0x0247: u
'e', # e with stroke
2162 0x0249: u
'j', # j with stroke
2163 0x024b: u
'q', # q with hook tail
2164 0x024d: u
'r', # r with stroke
2165 0x024f: u
'y', # y with stroke
2167 _non_id_translate_digraphs
= {
2168 0x00df: u
'sz', # ligature sz
2170 0x0153: u
'oe', # ligature oe
2171 0x0238: u
'db', # db digraph
2172 0x0239: u
'qp', # qp digraph
2175 def dupname(node
, name
):
2176 node
['dupnames'].append(name
)
2177 node
['names'].remove(name
)
2178 # Assume that this method is referenced, even though it isn't; we
2179 # don't want to throw unnecessary system_messages.
2182 def fully_normalize_name(name
):
2183 """Return a case- and whitespace-normalized name."""
2184 return ' '.join(name
.lower().split())
2186 def whitespace_normalize_name(name
):
2187 """Return a whitespace-normalized name."""
2188 return ' '.join(name
.split())
2190 def serial_escape(value
):
2191 """Escape string values that are elements of a list, for serialization."""
2192 return value
.replace('\\', r
'\\').replace(' ', r
'\ ')
2194 def pseudo_quoteattr(value
):
2195 """Quote attributes for pseudo-xml"""
2196 return '"%s"' % value
2201 # indent-tabs-mode: nil
2202 # sentence-end-double-space: t