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

Method run

markdown/treeprocessors.py:348–419  ·  view source on GitHub ↗

Apply inline patterns to a parsed Markdown tree. Iterate over `Element`, find elements with inline tag, apply inline patterns and append newly created Elements to tree. To avoid further processing of string with inline patterns, instead of normal string, use subclas

(self, tree: etree.Element, ancestors: list[str] | None = None)

Source from the content-addressed store, hash-verified

346 parents.extend(ancestors)
347
348 def run(self, tree: etree.Element, ancestors: list[str] | None = None) -> etree.Element:
349 """Apply inline patterns to a parsed Markdown tree.
350
351 Iterate over `Element`, find elements with inline tag, apply inline
352 patterns and append newly created Elements to tree. To avoid further
353 processing of string with inline patterns, instead of normal string,
354 use subclass [`AtomicString`][markdown.util.AtomicString]:
355
356 node.text = markdown.util.AtomicString("This will not be processed.")
357
358 Arguments:
359 tree: `Element` object, representing Markdown tree.
360 ancestors: List of parent tag names that precede the tree node (if needed).
361
362 Returns:
363 An element tree object with applied inline patterns.
364
365 """
366 self.stashed_nodes: dict[str, etree.Element | str] = {}
367
368 # Ensure a valid parent list, but copy passed in lists
369 # to ensure we don't have the user accidentally change it on us.
370 tree_parents = [] if ancestors is None else ancestors[:]
371
372 self.parent_map = {c: p for p in tree.iter() for c in p}
373 stack = [(tree, tree_parents)]
374 self.__build_ancestors(tree, tree_parents)
375
376 while stack:
377 currElement, parents = stack.pop(0)
378
379 self.ancestors = parents
380 if currElement is not tree:
381 self.ancestors.append(currElement.tag.lower())
382
383 insertQueue = []
384 for child in currElement:
385 if child.text and not isinstance(
386 child.text, util.AtomicString
387 ):
388 self.ancestors.append(child.tag.lower())
389 text = child.text
390 child.text = None
391 lst = self.__processPlaceholders(
392 self.__handleInline(text), child
393 )
394 for item in lst:
395 self.parent_map[item[0]] = child
396 stack += lst
397 insertQueue.append((child, lst))
398 self.ancestors.pop()
399 if child.tail:
400 tail = self.__handleInline(child.tail)
401 dumby = etree.Element('d')
402 child.tail = None
403 tailResult = self.__processPlaceholders(tail, dumby, False)
404 if dumby.tail:
405 child.tail = dumby.tail

Callers

nothing calls this directly

Calls 3

__build_ancestorsMethod · 0.95
__processPlaceholdersMethod · 0.95
__handleInlineMethod · 0.95

Tested by

no test coverage detected