# HG changeset patch # User Martin Geisler # Date 1286572766 -7200 # Node ID 17f28de168a40583ba19a03851c74dcfa72c1fdb # Parent fed4bb2c8defa4d535063668152c126dc38a4a31 minirst: refactor/simplify findblocks diff -r fed4bb2c8def -r 17f28de168a4 mercurial/minirst.py --- a/mercurial/minirst.py Sat Oct 09 15:41:53 2010 +0200 +++ b/mercurial/minirst.py Fri Oct 08 23:19:26 2010 +0200 @@ -48,25 +48,21 @@ utext = utext.replace(f, t) return utext.encode(encoding.encoding) + +_blockre = re.compile(r"\n(?:\s*\n)+") + def findblocks(text): """Find continuous blocks of lines in text. Returns a list of dictionaries representing the blocks. Each block has an 'indent' field and a 'lines' field. """ - blocks = [[]] - lines = text.splitlines() - for line in lines: - if line.strip(): - blocks[-1].append(line) - elif blocks[-1]: - blocks.append([]) - if not blocks[-1]: - del blocks[-1] - - for i, block in enumerate(blocks): - indent = min((len(l) - len(l.lstrip())) for l in block) - blocks[i] = dict(indent=indent, lines=[l[indent:] for l in block]) + blocks = [] + for b in _blockre.split(text.strip()): + lines = b.splitlines() + indent = min((len(l) - len(l.lstrip())) for l in lines) + lines = [l[indent:] for l in lines] + blocks.append(dict(indent=indent, lines=lines)) return blocks