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

Class MarkdownInHtmlProcessor

markdown/extensions/md_in_html.py:321–421  ·  view source on GitHub ↗

Process Markdown Inside HTML Blocks which have been stored in the `HtmlStash`.

Source from the content-addressed store, hash-verified

319
320
321class MarkdownInHtmlProcessor(BlockProcessor):
322 """Process Markdown Inside HTML Blocks which have been stored in the `HtmlStash`."""
323
324 def test(self, parent: etree.Element, block: str) -> bool:
325 # Always return True. `run` will return `False` it not a valid match.
326 return True
327
328 def parse_element_content(self, element: etree.Element) -> None:
329 """
330 Recursively parse the text content of an `etree` Element as Markdown.
331
332 Any block level elements generated from the Markdown will be inserted as children of the element in place
333 of the text content. All `markdown` attributes are removed. For any elements in which Markdown parsing has
334 been disabled, the text content of it and its children are wrapped in an `AtomicString`.
335 """
336
337 md_attr = element.attrib.pop('markdown', 'off')
338
339 if md_attr == 'block':
340 # Parse the block elements content as Markdown
341 if element.text:
342 block = element.text.rstrip('\n')
343 element.text = ''
344 self.parser.parseBlocks(element, block.split('\n\n'))
345
346 elif md_attr == 'span':
347 # Span elements need to be recursively processed for block elements and raw HTML
348 # as their content is not normally accessed by block processors, so expand stashed
349 # HTML under the span. Span content itself will not be parsed here, but will await
350 # the inline parser.
351 block = element.text if element.text is not None else ''
352 element.text = ''
353 child = None
354 start = 0
355
356 # Search the content for HTML placeholders and process the elements
357 for m in util.HTML_PLACEHOLDER_RE.finditer(block):
358 index = int(m.group(1))
359 el = self.parser.md.htmlStash.rawHtmlBlocks[index]
360 end = m.start()
361
362 if isinstance(el, etree.Element):
363 # Replace the placeholder with the element and process it.
364 # Content after the placeholder should be attached to the tail.
365 if child is None:
366 element.text += block[start:end]
367 else:
368 child.tail += block[start:end]
369 element.append(el)
370 self.parse_element_content(el)
371 child = el
372 if child.tail is None:
373 child.tail = ''
374 self.parser.md.htmlStash.rawHtmlBlocks.pop(index)
375 self.parser.md.htmlStash.rawHtmlBlocks.insert(index, '')
376
377 else:
378 # Not an element object, so insert content back into the element

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected