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

Class DelimiterProcessor

markdown/inlinepatterns.py:640–1125  ·  view source on GitHub ↗

Processor for handling complex nested patterns such as strong and em matches.

Source from the content-addressed store, hash-verified

638
639
640class DelimiterProcessor(InlineProcessor):
641 """Processor for handling complex nested patterns such as strong and em matches."""
642
643 SPACE = re.compile(r'\s')
644
645 def __init__(
646 self,
647 token: str,
648 tags: str,
649 md: Markdown,
650 smart: bool = False,
651 double: bool = False
652 ) -> None:
653 """Initialize."""
654
655 md.delimiters = self
656 self.regions: list[tuple[int, int, int, int, tuple[str, str], int]] = []
657 # Unpaired delimiter. The entries are the same as `Delimiter.stack`.
658 # HTML element cache of valid regions. Each entry holds the element, the location it replaces,
659 # and the location of the token Python Markdown will call us for once the element has been replaced.
660 self.cache: list[tuple[etree.Element, int, int, int]] = []
661 # The entries are the same as `Delimiter.stack`.
662 self.stack: list[tuple[int, int, bool, int, int]] = []
663 # Position of every delimiter token (valid or not) encountered while pairing, in ascending order.
664 self.positions: list[int] = []
665 # For each token and run length, how deep the last failed rule of 3 search went and the entry at that depth.
666 self.searched: dict[tuple[str, int], tuple[int, tuple[int, int, bool, int, int]]] = {}
667 self.tokens: list[str] = []
668 self.delimiters: dict[str, Delimiter] = {}
669 self.cache_index = 0
670 self.cache_pos = 0
671 # Info about the unused part of a closing delimiter run that directly follows the last returned element
672 self.remainder: tuple[str, str] | None = None
673 self.md = md
674 # API for Markdown to pass `safe_mode` into instance
675 self.safe_mode = False
676 self.add(token, tags, smart, double)
677
678 def add(
679 self,
680 token: str,
681 tags: str,
682 smart: bool = False,
683 double: bool = False
684 ) -> None:
685 """Add a delimiter."""
686
687 if token not in self.tokens:
688 self.tokens.append(token)
689 self.delimiters[token] = Delimiter(token, tags, smart, double)
690 self.pattern = '|'.join([re.escape(t) for t in self.tokens])
691 self.compiled_re = re.compile(self.pattern, re.DOTALL | re.UNICODE)
692
693 def remove(self, token: str) -> None:
694 """Remove a token."""
695
696 try:
697 i = self.tokens.index(token)

Callers 2

build_inlinepatternsFunction · 0.85
extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected