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

Class LinkInlineProcessor

markdown/inlinepatterns.py:1128–1312  ·  view source on GitHub ↗

Return a link element from the given match.

Source from the content-addressed store, hash-verified

1126
1127
1128class LinkInlineProcessor(InlineProcessor):
1129 """ Return a link element from the given match. """
1130 RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''&#x27;, re.DOTALL | re.UNICODE)
1131 RE_TITLE_CLEAN = re.compile(r'\s')
1132
1133 def __init__(self, pattern: str, md: Markdown | None = None):
1134 super().__init__(pattern, md)
1135 # The text `getText` last saw and the closing bracket for each index of it.
1136 self._bracket_data: str | None = None
1137 self._bracket_ends: list[int | None] = []
1138
1139 def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]:
1140 """ Return an `a` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. """
1141 text, index, handled = self.getText(data, m.end(0))
1142
1143 if not handled:
1144 return None, None, None
1145
1146 href, title, index, handled = self.getLink(data, index)
1147 if not handled:
1148 return None, None, None
1149
1150 el = etree.Element("a")
1151 el.text = text
1152
1153 el.set("href", href)
1154
1155 if title is not None:
1156 el.set("title", title)
1157
1158 return el, m.start(0), index
1159
1160 def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]:
1161 """Parse data between `()` of `[Text]()` allowing recursive `()`. """
1162
1163 href = ''
1164 title: str | None = None
1165 handled = False
1166
1167 m = self.RE_LINK.match(data, pos=index)
1168 if m and m.group(1):
1169 # Matches [Text](<link> "title")
1170 href = m.group(1)[1:-1].strip()
1171 if m.group(2):
1172 title = m.group(2)[1:-1]
1173 index = m.end(0)
1174 handled = True
1175 elif m:
1176 # Track bracket nesting and index in string
1177 bracket_count = 1
1178 backtrack_count = 1
1179 start_index = m.end()
1180 index = start_index
1181 last_bracket = -1
1182
1183 # Primary (first found) quote tracking.
1184 quote: str | None = None
1185 start_quote = -1

Callers 1

build_inlinepatternsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected