comparison env/lib/python3.9/site-packages/docutils/transforms/misc.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # $Id: misc.py 6314 2010-04-26 10:04:17Z milde $
2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
4
5 """
6 Miscellaneous transforms.
7 """
8
9 __docformat__ = 'reStructuredText'
10
11 from docutils import nodes
12 from docutils.transforms import Transform, TransformError
13
14
15 class CallBack(Transform):
16
17 """
18 Inserts a callback into a document. The callback is called when the
19 transform is applied, which is determined by its priority.
20
21 For use with `nodes.pending` elements. Requires a ``details['callback']``
22 entry, a bound method or function which takes one parameter: the pending
23 node. Other data can be stored in the ``details`` attribute or in the
24 object hosting the callback method.
25 """
26
27 default_priority = 990
28
29 def apply(self):
30 pending = self.startnode
31 pending.details['callback'](pending)
32 pending.parent.remove(pending)
33
34
35 class ClassAttribute(Transform):
36
37 """
38 Move the "class" attribute specified in the "pending" node into the
39 immediately following non-comment element.
40 """
41
42 default_priority = 210
43
44 def apply(self):
45 pending = self.startnode
46 parent = pending.parent
47 child = pending
48 while parent:
49 # Check for appropriate following siblings:
50 for index in range(parent.index(child) + 1, len(parent)):
51 element = parent[index]
52 if (isinstance(element, nodes.Invisible) or
53 isinstance(element, nodes.system_message)):
54 continue
55 element['classes'] += pending.details['class']
56 pending.parent.remove(pending)
57 return
58 else:
59 # At end of section or container; apply to sibling
60 child = parent
61 parent = parent.parent
62 error = self.document.reporter.error(
63 'No suitable element following "%s" directive'
64 % pending.details['directive'],
65 nodes.literal_block(pending.rawsource, pending.rawsource),
66 line=pending.line)
67 pending.replace_self(error)
68
69
70 class Transitions(Transform):
71
72 """
73 Move transitions at the end of sections up the tree. Complain
74 on transitions after a title, at the beginning or end of the
75 document, and after another transition.
76
77 For example, transform this::
78
79 <section>
80 ...
81 <transition>
82 <section>
83 ...
84
85 into this::
86
87 <section>
88 ...
89 <transition>
90 <section>
91 ...
92 """
93
94 default_priority = 830
95
96 def apply(self):
97 for node in self.document.traverse(nodes.transition):
98 self.visit_transition(node)
99
100 def visit_transition(self, node):
101 index = node.parent.index(node)
102 error = None
103 if (index == 0 or
104 isinstance(node.parent[0], nodes.title) and
105 (index == 1 or
106 isinstance(node.parent[1], nodes.subtitle) and
107 index == 2)):
108 assert (isinstance(node.parent, nodes.document) or
109 isinstance(node.parent, nodes.section))
110 error = self.document.reporter.error(
111 'Document or section may not begin with a transition.',
112 source=node.source, line=node.line)
113 elif isinstance(node.parent[index - 1], nodes.transition):
114 error = self.document.reporter.error(
115 'At least one body element must separate transitions; '
116 'adjacent transitions are not allowed.',
117 source=node.source, line=node.line)
118 if error:
119 # Insert before node and update index.
120 node.parent.insert(index, error)
121 index += 1
122 assert index < len(node.parent)
123 if index != len(node.parent) - 1:
124 # No need to move the node.
125 return
126 # Node behind which the transition is to be moved.
127 sibling = node
128 # While sibling is the last node of its parent.
129 while index == len(sibling.parent) - 1:
130 sibling = sibling.parent
131 # If sibling is the whole document (i.e. it has no parent).
132 if sibling.parent is None:
133 # Transition at the end of document. Do not move the
134 # transition up, and place an error behind.
135 error = self.document.reporter.error(
136 'Document may not end with a transition.',
137 line=node.line)
138 node.parent.insert(node.parent.index(node) + 1, error)
139 return
140 index = sibling.parent.index(sibling)
141 # Remove the original transition node.
142 node.parent.remove(node)
143 # Insert the transition after the sibling.
144 sibling.parent.insert(index + 1, node)