179
|
1 |
# $Id: writer_aux.py 5174 2007-05-31 00:01:52Z wiemann $
|
|
2 |
# Author: Lea Wiemann <LeWiemann@gmail.com>
|
|
3 |
# Copyright: This module has been placed in the public domain.
|
|
4 |
|
|
5 |
"""
|
|
6 |
Auxiliary transforms mainly to be used by Writer components.
|
|
7 |
|
|
8 |
This module is called "writer_aux" because otherwise there would be
|
|
9 |
conflicting imports like this one::
|
|
10 |
|
|
11 |
from docutils import writers
|
|
12 |
from docutils.transforms import writers
|
|
13 |
"""
|
|
14 |
|
|
15 |
__docformat__ = 'reStructuredText'
|
|
16 |
|
|
17 |
from docutils import nodes, utils, languages
|
|
18 |
from docutils.transforms import Transform
|
|
19 |
|
|
20 |
|
|
21 |
class Compound(Transform):
|
|
22 |
|
|
23 |
"""
|
|
24 |
Flatten all compound paragraphs. For example, transform ::
|
|
25 |
|
|
26 |
<compound>
|
|
27 |
<paragraph>
|
|
28 |
<literal_block>
|
|
29 |
<paragraph>
|
|
30 |
|
|
31 |
into ::
|
|
32 |
|
|
33 |
<paragraph>
|
|
34 |
<literal_block classes="continued">
|
|
35 |
<paragraph classes="continued">
|
|
36 |
"""
|
|
37 |
|
|
38 |
default_priority = 910
|
|
39 |
|
|
40 |
def apply(self):
|
|
41 |
for compound in self.document.traverse(nodes.compound):
|
|
42 |
first_child = 1
|
|
43 |
for child in compound:
|
|
44 |
if first_child:
|
|
45 |
if not isinstance(child, nodes.Invisible):
|
|
46 |
first_child = 0
|
|
47 |
else:
|
|
48 |
child['classes'].append('continued')
|
|
49 |
# Substitute children for compound.
|
|
50 |
compound.replace_self(compound[:])
|
|
51 |
|
|
52 |
|
|
53 |
class Admonitions(Transform):
|
|
54 |
|
|
55 |
"""
|
|
56 |
Transform specific admonitions, like this:
|
|
57 |
|
|
58 |
<note>
|
|
59 |
<paragraph>
|
|
60 |
Note contents ...
|
|
61 |
|
|
62 |
into generic admonitions, like this::
|
|
63 |
|
|
64 |
<admonition classes="note">
|
|
65 |
<title>
|
|
66 |
Note
|
|
67 |
<paragraph>
|
|
68 |
Note contents ...
|
|
69 |
|
|
70 |
The admonition title is localized.
|
|
71 |
"""
|
|
72 |
|
|
73 |
default_priority = 920
|
|
74 |
|
|
75 |
def apply(self):
|
|
76 |
lcode = self.document.settings.language_code
|
|
77 |
language = languages.get_language(lcode)
|
|
78 |
for node in self.document.traverse(nodes.Admonition):
|
|
79 |
node_name = node.__class__.__name__
|
|
80 |
# Set class, so that we know what node this admonition came from.
|
|
81 |
node['classes'].append(node_name)
|
|
82 |
if not isinstance(node, nodes.admonition):
|
|
83 |
# Specific admonition. Transform into a generic admonition.
|
|
84 |
admonition = nodes.admonition(node.rawsource, *node.children,
|
|
85 |
**node.attributes)
|
|
86 |
title = nodes.title('', language.labels[node_name])
|
|
87 |
admonition.insert(0, title)
|
|
88 |
node.replace_self(admonition)
|