minirst: refactor/simplify findblocks
authorMartin Geisler <mg@lazybytes.net>
Fri, 08 Oct 2010 23:19:26 +0200
changeset 12651 17f28de168a4
parent 12650 fed4bb2c8def
child 12652 3c31c0e42b11
minirst: refactor/simplify findblocks
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