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