equal
deleted
inserted
replaced
33 - option lists (supports only long options without arguments) |
33 - option lists (supports only long options without arguments) |
34 |
34 |
35 - inline literals (no other inline markup is not recognized) |
35 - inline literals (no other inline markup is not recognized) |
36 """ |
36 """ |
37 |
37 |
38 import re, sys, textwrap |
38 import re, sys |
39 |
39 import util |
40 |
40 |
41 def findblocks(text): |
41 def findblocks(text): |
42 """Find continuous blocks of lines in text. |
42 """Find continuous blocks of lines in text. |
43 |
43 |
44 Returns a list of dictionaries representing the blocks. Each block |
44 Returns a list of dictionaries representing the blocks. Each block |
302 if block['type'] == 'definition': |
302 if block['type'] == 'definition': |
303 term = indent + block['lines'][0] |
303 term = indent + block['lines'][0] |
304 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) |
304 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) |
305 defindent = indent + hang * ' ' |
305 defindent = indent + hang * ' ' |
306 text = ' '.join(map(str.strip, block['lines'][1:])) |
306 text = ' '.join(map(str.strip, block['lines'][1:])) |
307 return "%s\n%s" % (term, textwrap.fill(text, width=width, |
307 return '%s\n%s' % (term, util.wrap(text, width=width, |
308 initial_indent=defindent, |
308 initindent=defindent, |
309 subsequent_indent=defindent)) |
309 hangindent=defindent)) |
310 subindent = indent |
310 subindent = indent |
311 if block['type'] == 'bullet': |
311 if block['type'] == 'bullet': |
312 if block['lines'][0].startswith('| '): |
312 if block['lines'][0].startswith('| '): |
313 # Remove bullet for line blocks and add no extra |
313 # Remove bullet for line blocks and add no extra |
314 # indention. |
314 # indention. |
336 m = _optionre.match(block['lines'][0]) |
336 m = _optionre.match(block['lines'][0]) |
337 option, arg, rest = m.groups() |
337 option, arg, rest = m.groups() |
338 subindent = indent + (len(option) + len(arg)) * ' ' |
338 subindent = indent + (len(option) + len(arg)) * ' ' |
339 |
339 |
340 text = ' '.join(map(str.strip, block['lines'])) |
340 text = ' '.join(map(str.strip, block['lines'])) |
341 return textwrap.fill(text, width=width, |
341 return util.wrap(text, width=width, |
342 initial_indent=indent, |
342 initindent=indent, |
343 subsequent_indent=subindent) |
343 hangindent=subindent) |
344 |
344 |
345 |
345 |
346 def format(text, width, indent=0, keep=None): |
346 def format(text, width, indent=0, keep=None): |
347 """Parse and format the text according to width.""" |
347 """Parse and format the text according to width.""" |
348 blocks = findblocks(text) |
348 blocks = findblocks(text) |