##// END OF EJS Templates
minirst: refactor/simplify findblocks
Martin Geisler -
r12651:17f28de1 default
parent child Browse files
Show More
@@ -48,25 +48,21 b' def replace(text, substs):'
48 48 utext = utext.replace(f, t)
49 49 return utext.encode(encoding.encoding)
50 50
51
52 _blockre = re.compile(r"\n(?:\s*\n)+")
53
51 54 def findblocks(text):
52 55 """Find continuous blocks of lines in text.
53 56
54 57 Returns a list of dictionaries representing the blocks. Each block
55 58 has an 'indent' field and a 'lines' field.
56 59 """
57 blocks = [[]]
58 lines = text.splitlines()
59 for line in lines:
60 if line.strip():
61 blocks[-1].append(line)
62 elif blocks[-1]:
63 blocks.append([])
64 if not blocks[-1]:
65 del blocks[-1]
66
67 for i, block in enumerate(blocks):
68 indent = min((len(l) - len(l.lstrip())) for l in block)
69 blocks[i] = dict(indent=indent, lines=[l[indent:] for l in block])
60 blocks = []
61 for b in _blockre.split(text.strip()):
62 lines = b.splitlines()
63 indent = min((len(l) - len(l.lstrip())) for l in lines)
64 lines = [l[indent:] for l in lines]
65 blocks.append(dict(indent=indent, lines=lines))
70 66 return blocks
71 67
72 68
General Comments 0
You need to be logged in to leave comments. Login now