Match to a stored reference and return link element.
| 874 | |
| 875 | |
| 876 | class ReferenceInlineProcessor(LinkInlineProcessor): |
| 877 | """ Match to a stored reference and return link element. """ |
| 878 | NEWLINE_CLEANUP_RE = re.compile(r'\s+', re.MULTILINE) |
| 879 | |
| 880 | RE_LINK = re.compile(r'\s?\[([^\]]*)\]', re.DOTALL | re.UNICODE) |
| 881 | |
| 882 | def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]: |
| 883 | """ |
| 884 | Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`. |
| 885 | |
| 886 | """ |
| 887 | text, index, handled = self.getText(data, m.end(0)) |
| 888 | if not handled: |
| 889 | return None, None, None |
| 890 | |
| 891 | id, end, handled = self.evalId(data, index, text) |
| 892 | if not handled: |
| 893 | return None, None, None |
| 894 | |
| 895 | # Clean up line breaks in id |
| 896 | id = self.NEWLINE_CLEANUP_RE.sub(' ', id) |
| 897 | if id not in self.md.references: # ignore undefined refs |
| 898 | return None, m.start(0), end |
| 899 | |
| 900 | href, title = self.md.references[id] |
| 901 | |
| 902 | return self.makeTag(href, title, text), m.start(0), end |
| 903 | |
| 904 | def evalId(self, data: str, index: int, text: str) -> tuple[str | None, int, bool]: |
| 905 | """ |
| 906 | Evaluate the id portion of `[ref][id]`. |
| 907 | |
| 908 | If `[ref][]` use `[ref]`. |
| 909 | """ |
| 910 | m = self.RE_LINK.match(data, pos=index) |
| 911 | if not m: |
| 912 | return None, index, False |
| 913 | else: |
| 914 | id = m.group(1).lower() |
| 915 | end = m.end(0) |
| 916 | if not id: |
| 917 | id = text.lower() |
| 918 | return id, end, True |
| 919 | |
| 920 | def makeTag(self, href: str, title: str, text: str) -> etree.Element: |
| 921 | """ Return an `a` [`Element`][xml.etree.ElementTree.Element]. """ |
| 922 | el = etree.Element('a') |
| 923 | |
| 924 | el.set('href', href) |
| 925 | if title: |
| 926 | el.set('title', title) |
| 927 | |
| 928 | el.text = text |
| 929 | return el |
| 930 | |
| 931 | |
| 932 | class ShortReferenceInlineProcessor(ReferenceInlineProcessor): |
no outgoing calls
no test coverage detected
searching dependent graphs…