MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / FootnoteBlockProcessor

Class FootnoteBlockProcessor

markdown/extensions/footnotes.py:253–350  ·  view source on GitHub ↗

Find footnote definitions and store for later use.

Source from the content-addressed store, hash-verified

251
252
253class FootnoteBlockProcessor(BlockProcessor):
254 """ Find footnote definitions and store for later use. """
255
256 RE = re.compile(r'^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$', re.MULTILINE)
257
258 def __init__(self, footnotes: FootnoteExtension):
259 super().__init__(footnotes.parser)
260 self.footnotes = footnotes
261
262 def test(self, parent: etree.Element, block: str) -> bool:
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

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected