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

Class Delimiter

markdown/inlinepatterns.py:573–637  ·  view source on GitHub ↗

Delimiter.

Source from the content-addressed store, hash-verified

571
572
573class Delimiter:
574 """Delimiter."""
575
576 def __init__(self, token: str, tags: str, smart: bool, double: bool):
577 """Initialize."""
578
579 # An unclosed delimiter.
580 #
581 # 0. Start position of the (remaining) run in the text.
582 # 1. End position of the (remaining) run in the text.
583 # 2. Whether the run is ambiguous (could be a start or an end).
584 # 3. Original length of the run.
585 # 4. Current length of the run.
586 self.stack: deque[tuple[int, int, bool, int, int]] = deque()
587 temp = tags.split(',')
588 self.tag_count = len(temp)
589 self.tags: tuple[str, str] = (temp[0], temp[1]) if self.tag_count == 2 else (temp[0], temp[0])
590 self.double = len(temp) != 2 and double
591 self.single = len(temp) != 2 and not double
592 self.smart = smart
593 self._build_patterns(token)
594
595 def _build_patterns(self, token: str) -> str:
596 """Build regular expression patterns."""
597
598 # Build up patterns
599 self.token = token
600 etoken = re.escape(token)
601 # Avoid at start and end
602 xstart = fr'(?:(?<=_)|(?<![\w{etoken}]))' if token != '_' else fr'(?<![\w{etoken}])'
603 xend = fr'(?:(?=_)|(?![\w{etoken}]))' if token != '_' else fr'(?![\w{etoken}])'
604 self.max_size = 2
605 if self.tag_count != 2 and not self.double:
606 self.max_size = 1
607
608 # Patterns for "smart" cases.
609 if self.smart:
610 self.boundary = re.compile(
611 fr''&#x27;(?x)
612 (?P<ambiguous>(?<!^)(?<![\s{etoken}]){xstart}{etoken}{{1,}}{xend}(?![\s{etoken}])(?!$))|
613 (?P<end>(?<!^)(?<![\s{etoken}]){etoken}{{1,}}{xend})|
614 (?P<start>{xstart}{etoken}{{1,}}(?![\s{etoken}])(?!$))
615 ''&#x27;,
616 flags=re.UNICODE
617 )
618 # Patterns for "dumb" cases.
619 else:
620 self.boundary = re.compile(
621 fr''&#x27;(?x)
622 (?P<ambiguous>(?<!^)(?<![\s{etoken}]){etoken}{{1,}}(?![\s{etoken}])(?!$))|
623 (?P<end>(?<!^)(?<![\s{etoken}]){etoken}{{1,}})|
624 (?P<start>{etoken}{{1,}}(?![\s{etoken}])(?!$))
625 ''&#x27;,
626 flags=re.UNICODE
627 )
628
629 self.bad = re.compile(fr'{etoken}+')
630

Callers 1

addMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected