Return a link element from the given match.
| 688 | |
| 689 | |
| 690 | class LinkInlineProcessor(InlineProcessor): |
| 691 | """ Return a link element from the given match. """ |
| 692 | RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''', 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…