diff --git a/mercurial/minirst.py b/mercurial/minirst.py --- a/mercurial/minirst.py +++ b/mercurial/minirst.py @@ -106,6 +106,53 @@ def findliteralblocks(blocks): 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 @@ def formatblock(block, width): 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 @@ def format(text, width, indent=0): 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 @@ if __name__ == "__main__": 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)