| 47 | self.formatter = formatter |
| 48 | |
| 49 | def parseText(self): |
| 50 | UNPARSED = 0 |
| 51 | PARSED = 1 |
| 52 | parsed_block = [(UNPARSED, self.text)] |
| 53 | for regex, func in self.regexes: |
| 54 | index = 0 |
| 55 | while index < len(parsed_block): |
| 56 | label, text = parsed_block[index] |
| 57 | |
| 58 | # Already been parsed |
| 59 | if (label == PARSED): |
| 60 | index += 1 |
| 61 | continue |
| 62 | |
| 63 | match = re.search(regex, text) |
| 64 | if match: |
| 65 | parsed_block.pop(index) |
| 66 | start = match.start(0) |
| 67 | end = match.end(0) |
| 68 | |
| 69 | f = self.formatter.clone() |
| 70 | merge = func(match, f) |
| 71 | |
| 72 | if merge: |
| 73 | merged = text[:start] + f.dump() + text[end:] |
| 74 | parsed_block.insert(index, (UNPARSED, merged)) |
| 75 | else: |
| 76 | if text[:start]: |
| 77 | parsed_block.insert(index, |
| 78 | (UNPARSED, text[:start])) |
| 79 | |
| 80 | index += 1 |
| 81 | parsed_block.insert(index, (PARSED, f.dump())) |
| 82 | |
| 83 | index += 1 |
| 84 | if text[end:]: |
| 85 | parsed_block.insert(index, |
| 86 | (UNPARSED, text[end:])) |
| 87 | |
| 88 | else: |
| 89 | index += 1 |
| 90 | |
| 91 | self.lines += [i for _, i in parsed_block] |
| 92 | self.text = ' '.join(self.lines) |
| 93 | |
| 94 | def parse(self): |
| 95 | self.parseText() |