Find, set, and remove footnote definitions.
(self, parent: etree.Element, blocks: list[str])
| 263 | return True |
| 264 | |
| 265 | def run(self, parent: etree.Element, blocks: list[str]) -> bool: |
| 266 | """ Find, set, and remove footnote definitions. """ |
| 267 | block = blocks.pop(0) |
| 268 | |
| 269 | m = self.RE.search(block) |
| 270 | if m: |
| 271 | preceding = block[:m.start()] |
| 272 | # Record the definitions which are directly followed by another |
| 273 | # definition in this pass rather than one per pass through the block |
| 274 | # processors. |
| 275 | for m2 in self.RE.finditer(block, m.end()): |
| 276 | if block[m.end():m2.start()].strip('\n'): |
| 277 | break |
| 278 | self.footnotes.setFootnote(m.group(1), m.group(2).rstrip()) |
| 279 | m = m2 |
| 280 | |
| 281 | id = m.group(1) |
| 282 | fn_blocks = [m.group(2)] |
| 283 | |
| 284 | # Handle rest of block |
| 285 | therest = block[m.end():].lstrip('\n') |
| 286 | m2 = self.RE.search(therest) |
| 287 | if m2: |
| 288 | # Another footnote exists in the rest of this block. |
| 289 | # Any content before match is continuation of this footnote, which may be lazily indented. |
| 290 | before = therest[:m2.start()].rstrip('\n') |
| 291 | fn_blocks[0] = '\n'.join([fn_blocks[0], self.detab(before)]).lstrip('\n') |
| 292 | # Add back to blocks everything from beginning of match forward for next iteration. |
| 293 | blocks.insert(0, therest[m2.start():]) |
| 294 | else: |
| 295 | # All remaining lines of block are continuation of this footnote, which may be lazily indented. |
| 296 | fn_blocks[0] = '\n'.join([fn_blocks[0], self.detab(therest)]).strip('\n') |
| 297 | |
| 298 | # Check for child elements in remaining blocks. |
| 299 | fn_blocks.extend(self.detectTabbed(blocks)) |
| 300 | |
| 301 | footnote = "\n\n".join(fn_blocks) |
| 302 | self.footnotes.setFootnote(id, footnote.rstrip()) |
| 303 | |
| 304 | if preceding.strip(): |
| 305 | # Add any content before match back to blocks as separate block |
| 306 | blocks.insert(0, preceding.rstrip('\n')) |
| 307 | return True |
| 308 | # No match. Restore block. |
| 309 | blocks.insert(0, block) |
| 310 | return False |
| 311 | |
| 312 | def detectTabbed(self, blocks: list[str]) -> list[str]: |
| 313 | """ Find indented text and remove indent before further processing. |
nothing calls this directly
no test coverage detected