179
|
1 |
# $Id: misc.py 4564 2006-05-21 20:44:42Z wiemann $
|
|
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 |
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.', line=node.line)
|
|
117 |
if error:
|
|
118 |
# Insert before node and update index.
|
|
119 |
node.parent.insert(index, error)
|
|
120 |
index += 1
|
|
121 |
assert index < len(node.parent)
|
|
122 |
if index != len(node.parent) - 1:
|
|
123 |
# No need to move the node.
|
|
124 |
return
|
|
125 |
# Node behind which the transition is to be moved.
|
|
126 |
sibling = node
|
|
127 |
# While sibling is the last node of its parent.
|
|
128 |
while index == len(sibling.parent) - 1:
|
|
129 |
sibling = sibling.parent
|
|
130 |
# If sibling is the whole document (i.e. it has no parent).
|
|
131 |
if sibling.parent is None:
|
|
132 |
# Transition at the end of document. Do not move the
|
|
133 |
# transition up, and place an error behind.
|
|
134 |
error = self.document.reporter.error(
|
|
135 |
'Document may not end with a transition.',
|
|
136 |
line=node.line)
|
|
137 |
node.parent.insert(node.parent.index(node) + 1, error)
|
|
138 |
return
|
|
139 |
index = sibling.parent.index(sibling)
|
|
140 |
# Remove the original transition node.
|
|
141 |
node.parent.remove(node)
|
|
142 |
# Insert the transition after the sibling.
|
|
143 |
sibling.parent.insert(index + 1, node)
|