46 utext = text.decode(encoding.encoding) |
46 utext = text.decode(encoding.encoding) |
47 for f, t in substs: |
47 for f, t in substs: |
48 utext = utext.replace(f, t) |
48 utext = utext.replace(f, t) |
49 return utext.encode(encoding.encoding) |
49 return utext.encode(encoding.encoding) |
50 |
50 |
|
51 |
|
52 _blockre = re.compile(r"\n(?:\s*\n)+") |
|
53 |
51 def findblocks(text): |
54 def findblocks(text): |
52 """Find continuous blocks of lines in text. |
55 """Find continuous blocks of lines in text. |
53 |
56 |
54 Returns a list of dictionaries representing the blocks. Each block |
57 Returns a list of dictionaries representing the blocks. Each block |
55 has an 'indent' field and a 'lines' field. |
58 has an 'indent' field and a 'lines' field. |
56 """ |
59 """ |
57 blocks = [[]] |
60 blocks = [] |
58 lines = text.splitlines() |
61 for b in _blockre.split(text.strip()): |
59 for line in lines: |
62 lines = b.splitlines() |
60 if line.strip(): |
63 indent = min((len(l) - len(l.lstrip())) for l in lines) |
61 blocks[-1].append(line) |
64 lines = [l[indent:] for l in lines] |
62 elif blocks[-1]: |
65 blocks.append(dict(indent=indent, lines=lines)) |
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]) |
|
70 return blocks |
66 return blocks |
71 |
67 |
72 |
68 |
73 def findliteralblocks(blocks): |
69 def findliteralblocks(blocks): |
74 """Finds literal blocks and adds a 'type' field to the blocks. |
70 """Finds literal blocks and adds a 'type' field to the blocks. |