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.
28
28
Return it as a ``problematic`` node linked to a system message if there is a
31
- ``text`` is the interpreted text content.
31
- ``text`` is the interpreted text content, with backslash escapes converted
33
34
- ``lineno`` is the line number where the interpreted text beings.
74
75
__docformat__ = 'reStructuredText'
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
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
178
179
def register_generic_role(canonical_name, node_class):
179
180
"""For roles which simply wrap a given `node_class` around the text."""
195
196
def __call__(self, role, rawtext, text, lineno, inliner,
196
197
options={}, content=[]):
197
return [self.node_class(rawtext, text, **options)], []
199
return [self.node_class(rawtext, utils.unescape(text), **options)], []
200
202
class CustomRole:
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)], []
238
return [nodes.inline(rawtext, utils.unescape(text), **options)], []
237
240
generic_custom_role.options = {'class': directives.class_option}
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
271
return [nodes.reference(rawtext, 'PEP ' + utils.unescape(text), refuri=ref,
269
274
register_canonical_role('pep-reference', pep_reference_role)
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
291
node = nodes.reference(rawtext, 'RFC ' + utils.unescape(text), refuri=ref,
286
293
return [node], []
288
295
register_canonical_role('rfc-reference', rfc_reference_role)
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)
307
node = nodes.raw(rawtext, utils.unescape(text, 1), **options)
310
raw_role.options = {'format': directives.unchanged}
312
register_canonical_role('raw', raw_role)
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)
339
def set_classes(options):
341
Auxiliary function to set options['classes'] and delete
344
if options.has_key('class'):
345
assert not options.has_key('classes')
346
options['classes'] = options['class']