MCPcopy
hub / github.com/Python-Markdown/markdown / LinkInlineProcessor

Class LinkInlineProcessor

markdown/inlinepatterns.py:690–849  ·  view source on GitHub ↗

Return a link element from the given match.

Source from the content-addressed store, hash-verified

688
689
690class LinkInlineProcessor(InlineProcessor):
691 """ Return a link element from the given match. """
692 RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''&#x27;, re.DOTALL | re.UNICODE)
693 RE_TITLE_CLEAN = re.compile(r'\s')
694
695 def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]:
696 """ Return an `a` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. """
697 text, index, handled = self.getText(data, m.end(0))
698
699 if not handled:
700 return None, None, None
701
702 href, title, index, handled = self.getLink(data, index)
703 if not handled:
704 return None, None, None
705
706 el = etree.Element("a")
707 el.text = text
708
709 el.set("href", href)
710
711 if title is not None:
712 el.set("title", title)
713
714 return el, m.start(0), index
715
716 def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]:
717 """Parse data between `()` of `[Text]()` allowing recursive `()`. """
718
719 href = ''
720 title: str | None = None
721 handled = False
722
723 m = self.RE_LINK.match(data, pos=index)
724 if m and m.group(1):
725 # Matches [Text](<link> "title")
726 href = m.group(1)[1:-1].strip()
727 if m.group(2):
728 title = m.group(2)[1:-1]
729 index = m.end(0)
730 handled = True
731 elif m:
732 # Track bracket nesting and index in string
733 bracket_count = 1
734 backtrack_count = 1
735 start_index = m.end()
736 index = start_index
737 last_bracket = -1
738
739 # Primary (first found) quote tracking.
740 quote: str | None = None
741 start_quote = -1
742 exit_quote = -1
743 ignore_matches = False
744
745 # Secondary (second found) quote tracking.
746 alt_quote = None
747 start_alt_quote = -1

Callers 1

build_inlinepatternsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…