Parse text for abbreviation references.
| 138 | |
| 139 | |
| 140 | class AbbrBlockprocessor(BlockProcessor): |
| 141 | """ Parse text for abbreviation references. """ |
| 142 | |
| 143 | RE = re.compile(r'^[*]\[(?P<abbr>[^\\]*?)\][ ]?:[ ]*\n?[ ]*(?P<title>.*)$', re.MULTILINE) |
| 144 | |
| 145 | def __init__(self, parser: BlockParser, abbrs: dict): |
| 146 | self.abbrs: dict = abbrs |
| 147 | super().__init__(parser) |
| 148 | |
| 149 | def test(self, parent: etree.Element, block: str) -> bool: |
| 150 | return True |
| 151 | |
| 152 | def run(self, parent: etree.Element, blocks: list[str]) -> bool: |
| 153 | """ |
| 154 | Find and remove all abbreviation references from the text. |
| 155 | Each reference is added to the abbreviation collection. |
| 156 | |
| 157 | """ |
| 158 | block = blocks.pop(0) |
| 159 | m = self.RE.search(block) |
| 160 | if m: |
| 161 | abbr = m.group('abbr').strip() |
| 162 | title = m.group('title').strip() |
| 163 | if title and abbr: |
| 164 | if title == "''" or title == '""': |
| 165 | self.abbrs.pop(abbr) |
| 166 | else: |
| 167 | self.abbrs[abbr] = title |
| 168 | if block[m.end():].strip(): |
| 169 | # Add any content after match back to blocks as separate block |
| 170 | blocks.insert(0, block[m.end():].lstrip('\n')) |
| 171 | if block[:m.start()].strip(): |
| 172 | # Add any content before match back to blocks as separate block |
| 173 | blocks.insert(0, block[:m.start()].rstrip('\n')) |
| 174 | return True |
| 175 | # No match. Restore block. |
| 176 | blocks.insert(0, block) |
| 177 | return False |
| 178 | |
| 179 | |
| 180 | AbbrPreprocessor = deprecated("This class has been renamed to `AbbrBlockprocessor`.")(AbbrBlockprocessor) |