~ubuntu-branches/ubuntu/karmic/python-docutils/karmic

« back to all changes in this revision

Viewing changes to docutils/transforms/universal.py

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Authors: David Goodger, Ueli Schlaepfer
2
2
# Contact: goodger@users.sourceforge.net
3
 
# Revision: $Revision: 1.24 $
4
 
# Date: $Date: 2004/05/04 00:38:15 $
 
3
# Revision: $Revision: 4183 $
 
4
# Date: $Date: 2005-12-12 05:12:02 +0100 (Mon, 12 Dec 2005) $
5
5
# Copyright: This module has been placed in the public domain.
6
6
 
7
7
"""
32
32
    default_priority = 820
33
33
 
34
34
    def apply(self):
35
 
        header = self.generate_header()
36
 
        footer = self.generate_footer()
37
 
        if header or footer:
38
 
            decoration = nodes.decoration()
39
 
            decoration += header
40
 
            decoration += footer
41
 
            document = self.document
42
 
            index = document.first_child_not_matching_class(
43
 
                nodes.PreDecorative)
44
 
            if index is None:
45
 
                document += decoration
46
 
            else:
47
 
                document[index:index] = [decoration]
 
35
        header_nodes = self.generate_header()
 
36
        if header_nodes:
 
37
            decoration = self.document.get_decoration()
 
38
            header = decoration.get_header()
 
39
            header.extend(header_nodes)
 
40
        footer_nodes = self.generate_footer()
 
41
        if footer_nodes:
 
42
            decoration = self.document.get_decoration()
 
43
            footer = decoration.get_footer()
 
44
            footer.extend(footer_nodes)
48
45
 
49
46
    def generate_header(self):
50
47
        return None
79
76
                    nodes.reference('', 'reStructuredText', refuri='http://'
80
77
                                    'docutils.sourceforge.net/rst.html'),
81
78
                    nodes.Text(' source.\n')])
82
 
            footer = nodes.footer()
83
 
            footer += nodes.paragraph('', '', *text)
84
 
            return footer
 
79
            return [nodes.paragraph('', '', *text)]
85
80
        else:
86
81
            return None
87
82
 
88
83
 
 
84
class ExposeInternals(Transform):
 
85
 
 
86
    """
 
87
    Expose internal attributes if ``expose_internals`` setting is set.
 
88
    """
 
89
 
 
90
    default_priority = 840
 
91
    
 
92
    def not_Text(self, node):
 
93
        return not isinstance(node, nodes.Text)
 
94
 
 
95
    def apply(self): 
 
96
        if self.document.settings.expose_internals:
 
97
            for node in self.document.traverse(self.not_Text):
 
98
                for att in self.document.settings.expose_internals:
 
99
                    value = getattr(node, att, None)
 
100
                    if value is not None:
 
101
                        node['internal:' + att] = value
 
102
 
 
103
 
89
104
class Messages(Transform):
90
105
 
91
106
    """
97
112
 
98
113
    def apply(self):
99
114
        unfiltered = self.document.transform_messages
100
 
        threshold = self.document.reporter['writer'].report_level
 
115
        threshold = self.document.reporter.report_level
101
116
        messages = []
102
117
        for msg in unfiltered:
103
118
            if msg['level'] >= threshold and not msg.parent:
104
119
                messages.append(msg)
105
120
        if messages:
106
 
            section = nodes.section(CLASS='system-messages')
 
121
            section = nodes.section(classes=['system-messages'])
107
122
            # @@@ get this from the language module?
108
123
            section += nodes.title('', 'Docutils System Messages')
109
124
            section += messages
120
135
    default_priority = 870
121
136
 
122
137
    def apply(self):
123
 
        visitor = SystemMessageFilterVisitor(self.document)
124
 
        self.document.walk(visitor)
125
 
 
126
 
 
127
 
class SystemMessageFilterVisitor(nodes.SparseNodeVisitor):
128
 
 
129
 
    def unknown_visit(self, node):
130
 
        pass
131
 
 
132
 
    def visit_system_message(self, node):
133
 
        if node['level'] < self.document.reporter['writer'].report_level:
134
 
            node.parent.remove(node)
 
138
        for node in self.document.traverse(nodes.system_message):
 
139
            if node['level'] < self.document.reporter.report_level:
 
140
                node.parent.remove(node)
135
141
 
136
142
 
137
143
class TestMessages(Transform):
138
144
 
139
145
    """
140
146
    Append all post-parse system messages to the end of the document.
 
147
 
 
148
    Used for testing purposes.
141
149
    """
142
150
 
143
 
    default_priority = 890
 
151
    default_priority = 880
144
152
 
145
153
    def apply(self):
146
154
        for msg in self.document.transform_messages:
148
156
                self.document += msg
149
157
 
150
158
 
151
 
class FinalChecks(Transform):
152
 
 
153
 
    """
154
 
    Perform last-minute checks.
155
 
 
156
 
    - Check for dangling references (incl. footnote & citation).
157
 
    """
158
 
 
159
 
    default_priority = 840
 
159
class StripComments(Transform):
 
160
 
 
161
    """
 
162
    Remove comment elements from the document tree (only if the
 
163
    ``strip_comments`` setting is enabled).
 
164
    """
 
165
 
 
166
    default_priority = 740
160
167
 
161
168
    def apply(self):
162
 
        visitor = FinalCheckVisitor(
163
 
            self.document,
164
 
            self.document.transformer.unknown_reference_resolvers)
165
 
        self.document.walk(visitor)
166
 
        if self.document.settings.expose_internals:
167
 
            visitor = InternalAttributeExposer(self.document)
168
 
            self.document.walk(visitor)
169
 
 
170
 
 
171
 
class FinalCheckVisitor(nodes.SparseNodeVisitor):
172
 
    
173
 
    def __init__(self, document, unknown_reference_resolvers):
174
 
        nodes.SparseNodeVisitor.__init__(self, document)
175
 
        self.document = document
176
 
        self.unknown_reference_resolvers = unknown_reference_resolvers
177
 
 
178
 
    def unknown_visit(self, node):
179
 
        pass
180
 
 
181
 
    def visit_reference(self, node):
182
 
        if node.resolved or not node.hasattr('refname'):
183
 
            return
184
 
        refname = node['refname']
185
 
        id = self.document.nameids.get(refname)
186
 
        if id is None:
187
 
            for resolver_function in self.unknown_reference_resolvers:
188
 
                if resolver_function(node):
189
 
                    break
190
 
            else:
191
 
                if self.document.nameids.has_key(refname):
192
 
                    msg = self.document.reporter.error(
193
 
                        'Duplicate target name, cannot be used as a unique '
194
 
                        'reference: "%s".' % (node['refname']), base_node=node)
195
 
                else:
196
 
                    msg = self.document.reporter.error(
197
 
                        'Unknown target name: "%s".' % (node['refname']),
198
 
                        base_node=node)
199
 
                msgid = self.document.set_id(msg)
200
 
                prb = nodes.problematic(
201
 
                      node.rawsource, node.rawsource, refid=msgid)
202
 
                prbid = self.document.set_id(prb)
203
 
                msg.add_backref(prbid)
204
 
                node.parent.replace(node, prb)
205
 
        else:
206
 
            del node['refname']
207
 
            node['refid'] = id
208
 
            self.document.ids[id].referenced = 1
209
 
            node.resolved = 1
210
 
 
211
 
    visit_footnote_reference = visit_citation_reference = visit_reference
212
 
 
213
 
 
214
 
class InternalAttributeExposer(nodes.GenericNodeVisitor):
215
 
 
216
 
    def __init__(self, document):
217
 
        nodes.GenericNodeVisitor.__init__(self, document)
218
 
        self.internal_attributes = document.settings.expose_internals
219
 
 
220
 
    def default_visit(self, node):
221
 
        for att in self.internal_attributes:
222
 
            value = getattr(node, att, None)
223
 
            if value is not None:
224
 
                node['internal:' + att] = value
 
169
        if self.document.settings.strip_comments:
 
170
            for node in self.document.traverse(nodes.comment):
 
171
                node.parent.remove(node)