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

Method run

markdown/treeprocessors.py:343–412  ·  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

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