Return a link element from the given match.
| 1039 | |
| 1040 | |
| 1041 | class LinkInlineProcessor(InlineProcessor): |
| 1042 | """ Return a link element from the given match. """ |
| 1043 | RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''', re.DOTALL | re.UNICODE) |
| 1044 | RE_TITLE_CLEAN = re.compile(r'\s') |
| 1045 | |
| 1046 | def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]: |
| 1047 | """ Return an `a` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. """ |
| 1048 | text, index, handled = self.getText(data, m.end(0)) |
| 1049 | |
| 1050 | if not handled: |
| 1051 | return None, None, None |
| 1052 | |
| 1053 | href, title, index, handled = self.getLink(data, index) |
| 1054 | if not handled: |
| 1055 | return None, None, None |
| 1056 | |
| 1057 | el = etree.Element("a") |
| 1058 | el.text = text |
| 1059 | |
| 1060 | el.set("href", href) |
| 1061 | |
| 1062 | if title is not None: |
| 1063 | el.set("title", title) |
| 1064 | |
| 1065 | return el, m.start(0), index |
| 1066 | |
| 1067 | def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]: |
| 1068 | """Parse data between `()` of `[Text]()` allowing recursive `()`. """ |
| 1069 | |
| 1070 | href = '' |
| 1071 | title: str | None = None |
| 1072 | handled = False |
| 1073 | |
| 1074 | m = self.RE_LINK.match(data, pos=index) |
| 1075 | if m and m.group(1): |
| 1076 | # Matches [Text](<link> "title") |
| 1077 | href = m.group(1)[1:-1].strip() |
| 1078 | if m.group(2): |
| 1079 | title = m.group(2)[1:-1] |
| 1080 | index = m.end(0) |
| 1081 | handled = True |
| 1082 | elif m: |
| 1083 | # Track bracket nesting and index in string |
| 1084 | bracket_count = 1 |
| 1085 | backtrack_count = 1 |
| 1086 | start_index = m.end() |
| 1087 | index = start_index |
| 1088 | last_bracket = -1 |
| 1089 | |
| 1090 | # Primary (first found) quote tracking. |
| 1091 | quote: str | None = None |
| 1092 | start_quote = -1 |
| 1093 | exit_quote = -1 |
| 1094 | ignore_matches = False |
| 1095 | |
| 1096 | # Secondary (second found) quote tracking. |
| 1097 | alt_quote = None |
| 1098 | start_alt_quote = -1 |
no outgoing calls
no test coverage detected