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

Class Delimiter

markdown/inlinepatterns.py:562–626  ·  view source on GitHub ↗

Delimiter.

Source from the content-addressed store, hash-verified

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

Callers 1

addMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected