~ubuntu-branches/ubuntu/intrepid/python-docutils/intrepid

« back to all changes in this revision

Viewing changes to docutils/parsers/rst/roles.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
# Author: Edward Loper
2
2
# Contact: edloper@gradient.cis.upenn.edu
3
 
# Revision: $Revision: 1.8 $
4
 
# Date: $Date: 2004/04/27 19:48:36 $
 
3
# Revision: $Revision: 3155 $
 
4
# Date: $Date: 2005-04-02 23:57:06 +0200 (Sat, 02 Apr 2005) $
5
5
# Copyright: This module has been placed in the public domain.
6
6
 
7
7
"""
28
28
  Return it as a ``problematic`` node linked to a system message if there is a
29
29
  problem.
30
30
 
31
 
- ``text`` is the interpreted text content.
 
31
- ``text`` is the interpreted text content, with backslash escapes converted
 
32
  to nulls (``\x00``).
32
33
 
33
34
- ``lineno`` is the line number where the interpreted text beings.
34
35
 
73
74
 
74
75
__docformat__ = 'reStructuredText'
75
76
 
76
 
from docutils import nodes
 
77
from docutils import nodes, utils
77
78
from docutils.parsers.rst import directives
78
79
from docutils.parsers.rst.languages import en as _fallback_language_module
79
80
 
173
174
    if not hasattr(role_fn, 'options') or role_fn.options is None:
174
175
        role_fn.options = {'class': directives.class_option}
175
176
    elif not role_fn.options.has_key('class'):
176
 
        role_fn.options['class'] = directives.class_option    
 
177
        role_fn.options['class'] = directives.class_option
177
178
 
178
179
def register_generic_role(canonical_name, node_class):
179
180
    """For roles which simply wrap a given `node_class` around the text."""
194
195
 
195
196
    def __call__(self, role, rawtext, text, lineno, inliner,
196
197
                 options={}, content=[]):
197
 
        return [self.node_class(rawtext, text, **options)], []
 
198
        set_classes(options)
 
199
        return [self.node_class(rawtext, utils.unescape(text), **options)], []
198
200
 
199
201
 
200
202
class CustomRole:
232
234
    """"""
233
235
    # Once nested inline markup is implemented, this and other methods should
234
236
    # recursively call inliner.nested_parse().
235
 
    return [nodes.inline(rawtext, text, **options)], []
 
237
    set_classes(options)
 
238
    return [nodes.inline(rawtext, utils.unescape(text), **options)], []
236
239
 
237
240
generic_custom_role.options = {'class': directives.class_option}
238
241
 
263
266
        prb = inliner.problematic(rawtext, rawtext, msg)
264
267
        return [prb], [msg]
265
268
    # Base URL mainly used by inliner.pep_reference; so this is correct:
266
 
    ref = inliner.pep_url % pepnum
267
 
    return [nodes.reference(rawtext, 'PEP ' + text, refuri=ref, **options)], []
 
269
    ref = inliner.document.settings.pep_base_url + inliner.pep_url % pepnum
 
270
    set_classes(options)
 
271
    return [nodes.reference(rawtext, 'PEP ' + utils.unescape(text), refuri=ref,
 
272
                            **options)], []
268
273
 
269
274
register_canonical_role('pep-reference', pep_reference_role)
270
275
 
281
286
        prb = inliner.problematic(rawtext, rawtext, msg)
282
287
        return [prb], [msg]
283
288
    # Base URL mainly used by inliner.rfc_reference, so this is correct:
284
 
    ref = inliner.rfc_url % rfcnum
285
 
    node = nodes.reference(rawtext, 'RFC ' + text, refuri=ref, **options)
 
289
    ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum
 
290
    set_classes(options)
 
291
    node = nodes.reference(rawtext, 'RFC ' + utils.unescape(text), refuri=ref,
 
292
                           **options)
286
293
    return [node], []
287
294
 
288
295
register_canonical_role('rfc-reference', rfc_reference_role)
289
296
 
 
297
def raw_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
 
298
    if not options.has_key('format'):
 
299
        msg = inliner.reporter.error(
 
300
            'No format (Writer name) is associated with this role: "%s".\n'
 
301
            'The "raw" role cannot be used directly.\n'
 
302
            'Instead, use the "role" directive to create a new role with '
 
303
            'an associated format.' % role, line=lineno)
 
304
        prb = inliner.problematic(rawtext, rawtext, msg)
 
305
        return [prb], [msg]
 
306
    set_classes(options)
 
307
    node = nodes.raw(rawtext, utils.unescape(text, 1), **options)
 
308
    return [node], []
 
309
 
 
310
raw_role.options = {'format': directives.unchanged}
 
311
 
 
312
register_canonical_role('raw', raw_role)
 
313
 
290
314
 
291
315
######################################################################
292
316
# Register roles that are currently unimplemented.
310
334
# This should remain unimplemented, for testing purposes:
311
335
register_canonical_role('restructuredtext-unimplemented-role',
312
336
                        unimplemented_role)
 
337
 
 
338
 
 
339
def set_classes(options):
 
340
    """
 
341
    Auxiliary function to set options['classes'] and delete
 
342
    options['class'].
 
343
    """
 
344
    if options.has_key('class'):
 
345
        assert not options.has_key('classes')
 
346
        options['classes'] = options['class']
 
347
        del options['class']