changeset 12651:17f28de168a4

minirst: refactor/simplify findblocks
author Martin Geisler <mg@lazybytes.net>
date Fri, 08 Oct 2010 23:19:26 +0200
parents fed4bb2c8def
children 3c31c0e42b11
files mercurial/minirst.py
diffstat 1 files changed, 9 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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