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

Method run

markdown/treeprocessors.py:348–417  ·  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
375 while stack:
376 currElement, parents = stack.pop(0)
377
378 self.ancestors = parents
379 self.__build_ancestors(currElement, self.ancestors)
380
381 insertQueue = []
382 for child in currElement:
383 if child.text and not isinstance(
384 child.text, util.AtomicString
385 ):
386 self.ancestors.append(child.tag.lower())
387 text = child.text
388 child.text = None
389 lst = self.__processPlaceholders(
390 self.__handleInline(text), child
391 )
392 for item in lst:
393 self.parent_map[item[0]] = child
394 stack += lst
395 insertQueue.append((child, lst))
396 self.ancestors.pop()
397 if child.tail:
398 tail = self.__handleInline(child.tail)
399 dumby = etree.Element('d')
400 child.tail = None
401 tailResult = self.__processPlaceholders(tail, dumby, False)
402 if dumby.tail:
403 child.tail = dumby.tail
404 pos = list(currElement).index(child) + 1
405 tailResult.reverse()

Callers

nothing calls this directly

Calls 3

__build_ancestorsMethod · 0.95
__processPlaceholdersMethod · 0.95
__handleInlineMethod · 0.95

Tested by

no test coverage detected