# HG changeset patch # User Martin Geisler # Date 1257463835 -3600 # Node ID 5f101af4a92130266de9598022fcccc34b0ed79a # Parent 26d3ade60fa69ec954cea6b630d1a784a297b1d8 minirst: combine list parsing in one function Bullet, option, field, and definition lists were parsed very similar code. They are now parsed by a single function (splitparagraphs). Some logic from the old parsing functions has been moved down to formatblock. This simplifies the parsing while putting the logic where it's really needed. diff -r 26d3ade60fa6 -r 5f101af4a921 mercurial/minirst.py --- a/mercurial/minirst.py Thu Nov 05 21:53:22 2009 +0100 +++ b/mercurial/minirst.py Fri Nov 06 00:30:35 2009 +0100 @@ -106,6 +106,53 @@ i += 1 return blocks +_bulletre = re.compile(r'- ') +_optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$') +_fieldre = re.compile(r':(?![: ])([^:]*)(? 1 and - not blocks[i]['lines'][0].startswith(' ') and - blocks[i]['lines'][1].startswith(' ')): - definitions = [] - for line in blocks[i]['lines']: - if not line.startswith(' '): - definitions.append(dict(type='definition', lines=[], - indent=blocks[i]['indent'])) - definitions[-1]['lines'].append(line) - definitions[-1]['hang'] = len(line) - len(line.lstrip()) - blocks[i:i+1] = definitions - i += len(definitions) - 1 - i += 1 - return blocks - - def inlineliterals(blocks): for b in blocks: if b['type'] == 'paragraph': @@ -298,19 +212,29 @@ return indent + ('\n' + indent).join(block['lines']) if block['type'] == 'definition': term = indent + block['lines'][0] - defindent = indent + block['hang'] * ' ' + hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) + defindent = indent + hang * ' ' text = ' '.join(map(str.strip, block['lines'][1:])) return "%s\n%s" % (term, textwrap.fill(text, width=width, initial_indent=defindent, subsequent_indent=defindent)) initindent = subindent = indent - text = ' '.join(map(str.strip, block['lines'])) if block['type'] == 'bullet': - initindent = indent + '- ' subindent = indent + ' ' - elif block['type'] in ('option', 'field'): - subindent = indent + block['width'] * ' ' + elif block['type'] == 'field': + m = _fieldre.match(block['lines'][0]) + if m: + key, spaces, rest = m.groups() + # Turn ":foo: bar" into "foo bar". + block['lines'][0] = '%s %s%s' % (key, spaces, rest) + subindent = indent + (2 + len(key) + len(spaces)) * ' ' + elif block['type'] == 'option': + m = _optionre.match(block['lines'][0]) + if m: + option, arg, rest = m.groups() + subindent = indent + (len(option) + len(arg)) * ' ' + text = ' '.join(map(str.strip, block['lines'])) return textwrap.fill(text, width=width, initial_indent=initindent, subsequent_indent=subindent) @@ -323,11 +247,8 @@ b['indent'] += indent blocks = findliteralblocks(blocks) blocks = inlineliterals(blocks) + blocks = splitparagraphs(blocks) blocks = findsections(blocks) - blocks = findbulletlists(blocks) - blocks = findoptionlists(blocks) - blocks = findfieldlists(blocks) - blocks = finddefinitionlists(blocks) blocks = addmargins(blocks) return '\n'.join(formatblock(b, width) for b in blocks) @@ -345,10 +266,7 @@ text = open(sys.argv[1]).read() blocks = debug(findblocks, text) blocks = debug(findliteralblocks, blocks) + blocks = debug(splitparagraphs, blocks) blocks = debug(findsections, blocks) - blocks = debug(findbulletlists, blocks) - blocks = debug(findoptionlists, blocks) - blocks = debug(findfieldlists, blocks) - blocks = debug(finddefinitionlists, blocks) blocks = debug(addmargins, blocks) print '\n'.join(formatblock(b, 30) for b in blocks)