Match to a stored reference and return link element.
| 1225 | |
| 1226 | |
| 1227 | class ReferenceInlineProcessor(LinkInlineProcessor): |
| 1228 | """ Match to a stored reference and return link element. """ |
| 1229 | NEWLINE_CLEANUP_RE = re.compile(r'\s+', re.MULTILINE) |
| 1230 | |
| 1231 | RE_LINK = re.compile(r'\s?\[([^\]]*)\]', re.DOTALL | re.UNICODE) |
| 1232 | |
| 1233 | def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]: |
| 1234 | """ |
| 1235 | Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`. |
| 1236 | |
| 1237 | """ |
| 1238 | text, index, handled = self.getText(data, m.end(0)) |
| 1239 | if not handled: |
| 1240 | return None, None, None |
| 1241 | |
| 1242 | id, end, handled = self.evalId(data, index, text) |
| 1243 | if not handled: |
| 1244 | return None, None, None |
| 1245 | |
| 1246 | # Clean up line breaks in id |
| 1247 | id = self.NEWLINE_CLEANUP_RE.sub(' ', id) |
| 1248 | if id not in self.md.references: # ignore undefined refs |
| 1249 | return None, m.start(0), end |
| 1250 | |
| 1251 | href, title = self.md.references[id] |
| 1252 | |
| 1253 | return self.makeTag(href, title, text), m.start(0), end |
| 1254 | |
| 1255 | def evalId(self, data: str, index: int, text: str) -> tuple[str | None, int, bool]: |
| 1256 | """ |
| 1257 | Evaluate the id portion of `[ref][id]`. |
| 1258 | |
| 1259 | If `[ref][]` use `[ref]`. |
| 1260 | """ |
| 1261 | m = self.RE_LINK.match(data, pos=index) |
| 1262 | if not m: |
| 1263 | return None, index, False |
| 1264 | else: |
| 1265 | id = m.group(1).lower() |
| 1266 | end = m.end(0) |
| 1267 | if not id: |
| 1268 | id = text.lower() |
| 1269 | return id, end, True |
| 1270 | |
| 1271 | def makeTag(self, href: str, title: str, text: str) -> etree.Element: |
| 1272 | """ Return an `a` [`Element`][xml.etree.ElementTree.Element]. """ |
| 1273 | el = etree.Element('a') |
| 1274 | |
| 1275 | el.set('href', href) |
| 1276 | if title: |
| 1277 | el.set('title', title) |
| 1278 | |
| 1279 | el.text = text |
| 1280 | return el |
| 1281 | |
| 1282 | |
| 1283 | class ShortReferenceInlineProcessor(ReferenceInlineProcessor): |
no outgoing calls
no test coverage detected