102
109
'epigraph': ('body', 'epigraph'),
103
110
'highlights': ('body', 'highlights'),
104
111
'pull-quote': ('body', 'pull_quote'),
105
'table': ('body', 'table'),
112
'compound': ('body', 'compound'),
113
'container': ('body', 'container'),
106
114
#'questions': ('body', 'question_list'),
115
'table': ('tables', 'table'),
116
'csv-table': ('tables', 'csv_table'),
117
'list-table': ('tables', 'list_table'),
107
118
'image': ('images', 'image'),
108
119
'figure': ('images', 'figure'),
109
120
'contents': ('parts', 'contents'),
110
121
'sectnum': ('parts', 'sectnum'),
122
'header': ('parts', 'header'),
123
'footer': ('parts', 'footer'),
111
124
#'footnotes': ('parts', 'footnotes'),
112
125
#'citations': ('parts', 'citations'),
113
126
'target-notes': ('references', 'target_notes'),
119
132
'unicode': ('misc', 'unicode_directive'),
120
133
'class': ('misc', 'class_directive'),
121
134
'role': ('misc', 'role'),
135
'default-role': ('misc', 'default_role'),
136
'title': ('misc', 'title'),
137
'date': ('misc', 'date'),
122
138
'restructuredtext-test-directive': ('misc', 'directive_test_function'),}
123
139
"""Mapping of directive name to (module name, function name). The directive
124
140
name is canonical & must be lowercase. Language-dependent names are defined
241
255
Return the path argument unwrapped (with newlines removed).
242
256
(Directive option conversion function.)
244
Raise ``ValueError`` if no argument is found or if the path contains
258
Raise ``ValueError`` if no argument is found.
247
260
if argument is None:
248
261
raise ValueError('argument required but none supplied')
250
263
path = ''.join([s.strip() for s in argument.splitlines()])
251
if path.find(' ') == -1:
254
raise ValueError('path contains whitespace')
268
Return the URI argument with whitespace removed.
269
(Directive option conversion function.)
271
Raise ``ValueError`` if no argument is found.
274
raise ValueError('argument required but none supplied')
276
uri = ''.join(argument.split())
256
279
def nonnegative_int(argument):
263
286
raise ValueError('negative value; must be positive or zero')
289
length_units = ['em', 'ex', 'px', 'in', 'cm', 'mm', 'pt', 'pc']
291
def get_measure(argument, units):
293
Check for a positive argument of one of the units and return a
294
normalized string of the form "<value><unit>" (without space in
297
To be called from directive option conversion functions.
299
match = re.match(r'^([0-9.]+) *(%s)$' % '|'.join(units), argument)
301
assert match is not None
302
float(match.group(1))
303
except (AssertionError, ValueError):
305
'not a positive measure of one of the following units:\n%s'
306
% ' '.join(['"%s"' % i for i in units]))
307
return match.group(1) + match.group(2)
309
def length_or_unitless(argument):
310
return get_measure(argument, length_units + [''])
312
def length_or_percentage_or_unitless(argument):
313
return get_measure(argument, length_units + ['%', ''])
267
315
def class_option(argument):
269
Convert the argument into an ID-compatible string and return it.
317
Convert the argument into a list of ID-compatible strings and return it.
270
318
(Directive option conversion function.)
272
320
Raise ``ValueError`` if no argument is found.
274
322
if argument is None:
275
323
raise ValueError('argument required but none supplied')
276
class_name = nodes.make_id(argument)
278
raise ValueError('cannot make "%s" into a class name' % argument)
281
def format_values(values):
282
return '%s, or "%s"' % (', '.join(['"%s"' % s for s in values[:-1]]),
324
names = argument.split()
327
class_name = nodes.make_id(name)
329
raise ValueError('cannot make "%s" into a class name' % name)
330
class_names.append(class_name)
333
unicode_pattern = re.compile(
334
r'(?:0x|x|\\x|U\+?|\\u)([0-9a-f]+)$|&#x([0-9a-f]+);$', re.IGNORECASE)
336
def unicode_code(code):
338
Convert a Unicode character code to a Unicode character.
339
(Directive option conversion function.)
341
Codes may be decimal numbers, hexadecimal numbers (prefixed by ``0x``,
342
``x``, ``\x``, ``U+``, ``u``, or ``\u``; e.g. ``U+262E``), or XML-style
343
numeric character entities (e.g. ``☮``). Other text remains as-is.
345
Raise ValueError for illegal Unicode code values.
348
if code.isdigit(): # decimal number
349
return unichr(int(code))
351
match = unicode_pattern.match(code)
352
if match: # hex number
353
value = match.group(1) or match.group(2)
354
return unichr(int(value, 16))
357
except OverflowError, detail:
358
raise ValueError('code too large (%s)' % detail)
360
def single_char_or_unicode(argument):
362
A single character is returned as-is. Unicode characters codes are
363
converted as in `unicode_code`. (Directive option conversion function.)
365
char = unicode_code(argument)
367
raise ValueError('%r invalid; must be a single character or '
368
'a Unicode code' % char)
371
def single_char_or_whitespace_or_unicode(argument):
373
As with `single_char_or_unicode`, but "tab" and "space" are also supported.
374
(Directive option conversion function.)
376
if argument == 'tab':
378
elif argument == 'space':
381
char = single_char_or_unicode(argument)
384
def positive_int(argument):
386
Converts the argument into an integer. Raises ValueError for negative,
387
zero, or non-integer values. (Directive option conversion function.)
389
value = int(argument)
391
raise ValueError('negative or zero value; must be positive')
394
def positive_int_list(argument):
396
Converts a space- or comma-separated list of values into a Python list
398
(Directive option conversion function.)
400
Raises ValueError for non-positive-integer values.
403
entries = argument.split(',')
405
entries = argument.split()
406
return [positive_int(entry) for entry in entries]
408
def encoding(argument):
410
Verfies the encoding argument by lookup.
411
(Directive option conversion function.)
413
Raises ValueError for unknown encodings.
416
codecs.lookup(argument)
418
raise ValueError('unknown encoding: "%s"' % argument)
285
421
def choice(argument, values):