|
17 | 17 |
|
18 | 18 | class PlatformSectionPreprocessor(Preprocessor): |
19 | 19 |
|
20 | | - BLOCK_RE = re.compile(r''' |
21 | | -^@!\[(?P<sections>[\w, ]+)\]\W*\n |
22 | | -(?P<content>.*?)(?<=\n) |
23 | | -!@\W*$''', re.MULTILINE | re.DOTALL | re.VERBOSE) |
| 20 | + PLATFORM_SECTION_RE = re.compile(r'''@!\[(?P<sections>[\w, ]+)\](?P<content>.*?)!@''', re.DOTALL | re.VERBOSE) |
24 | 21 |
|
25 | | - INLINE_RE = re.compile(r'''@!\[(?P<sections>[\w, ]+)\](?P<content>.*?)!@''', re.DOTALL | re.VERBOSE) |
| 22 | + STARTSWITH_WHITESPACE_RE = re.compile(r'^\W+(@!\[|\n)') |
26 | 23 |
|
27 | 24 | def __init__(self, platform_section, **kwargs): |
28 | 25 | self.platform_section = platform_section.lower().strip() |
29 | 26 | super(PlatformSectionPreprocessor, self).__init__(**kwargs) |
30 | 27 |
|
31 | 28 | def run(self, lines): |
32 | 29 | text = "\n".join(lines) |
33 | | - text = self.process_inline(text) |
34 | | - text = self.process_block(text) |
| 30 | + text = self.process_platform_sections(text) |
35 | 31 | return text.split("\n") |
36 | 32 |
|
37 | 33 | def split_sections(self, sections_group): |
38 | 34 | return [section.lower().strip() for section in sections_group.split(',')] |
39 | 35 |
|
40 | | - def process_inline(self, text): |
| 36 | + def process_platform_sections(self, text): |
41 | 37 | while 1: |
42 | | - m = self.INLINE_RE.search(text) |
| 38 | + m = self.PLATFORM_SECTION_RE.search(text) |
43 | 39 | if m: |
44 | 40 | sections = self.split_sections(m.group('sections')) |
45 | 41 |
|
46 | | - if self.platform_section in sections: |
47 | | - content = m.group('content') |
48 | | - text = '%s%s%s' % (text[:m.start()], content, text[m.end():]) |
49 | | - else: |
50 | | - text = '%s%s' % (text[:m.start()], text[m.end():].lstrip()) |
51 | | - else: |
52 | | - break |
53 | | - return text |
54 | | - |
55 | | - def process_block(self, text): |
56 | | - while 1: |
57 | | - m = self.BLOCK_RE.search(text) |
58 | | - if m: |
59 | | - sections = self.split_sections(m.group('sections')) |
| 42 | + start = text[:m.start()] |
| 43 | + end = text[m.end():] |
| 44 | + if self.STARTSWITH_WHITESPACE_RE.match(end): |
| 45 | + end = end.lstrip() |
60 | 46 |
|
61 | 47 | if self.platform_section in sections: |
62 | 48 | content = m.group('content') |
63 | | - text = '%s\n%s\n%s' % (text[:m.start()], content, text[m.end():]) |
| 49 | + text = '%s%s%s' % (start, content, end) |
64 | 50 | else: |
65 | | - text = '%s\n%s' % (text[:m.start()], text[m.end():]) |
| 51 | + text = '%s%s' % (start, end) |
66 | 52 | else: |
67 | 53 | break |
68 | 54 | return text |
|
0 commit comments