Match to a stored reference and return link element.
| 1337 | |
| 1338 | |
| 1339 | class ReferenceInlineProcessor(LinkInlineProcessor): |
| 1340 | """ Match to a stored reference and return link element. """ |
| 1341 | NEWLINE_CLEANUP_RE = re.compile(r'\s+', re.MULTILINE) |
| 1342 | |
| 1343 | RE_LINK = re.compile(r'\s?\[([^\]]*)\]', re.DOTALL | re.UNICODE) |
| 1344 | |
| 1345 | def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]: |
| 1346 | """ |
| 1347 | Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`. |
| 1348 | |
| 1349 | """ |
| 1350 | text, index, handled = self.getText(data, m.end(0)) |
| 1351 | if not handled: |
| 1352 | return None, None, None |
| 1353 | |
| 1354 | id, end, handled = self.evalId(data, index, text) |
| 1355 | if not handled: |
| 1356 | return None, None, None |
| 1357 | |
| 1358 | # Clean up line breaks in id |
| 1359 | id = self.NEWLINE_CLEANUP_RE.sub(' ', id) |
| 1360 | if id not in self.md.references: # ignore undefined refs |
| 1361 | return None, m.start(0), end |
| 1362 | |
| 1363 | href, title = self.md.references[id] |
| 1364 | |
| 1365 | return self.makeTag(href, title, text), m.start(0), end |
| 1366 | |
| 1367 | def evalId(self, data: str, index: int, text: str) -> tuple[str | None, int, bool]: |
| 1368 | """ |
| 1369 | Evaluate the id portion of `[ref][id]`. |
| 1370 | |
| 1371 | If `[ref][]` use `[ref]`. |
| 1372 | """ |
| 1373 | m = self.RE_LINK.match(data, pos=index) |
| 1374 | if not m: |
| 1375 | return None, index, False |
| 1376 | else: |
| 1377 | id = m.group(1).lower() |
| 1378 | end = m.end(0) |
| 1379 | if not id: |
| 1380 | id = text.lower() |
| 1381 | return id, end, True |
| 1382 | |
| 1383 | def makeTag(self, href: str, title: str, text: str) -> etree.Element: |
| 1384 | """ Return an `a` [`Element`][xml.etree.ElementTree.Element]. """ |
| 1385 | el = etree.Element('a') |
| 1386 | |
| 1387 | el.set('href', href) |
| 1388 | if title: |
| 1389 | el.set('title', title) |
| 1390 | |
| 1391 | el.text = text |
| 1392 | return el |
| 1393 | |
| 1394 | |
| 1395 | class ShortReferenceInlineProcessor(ReferenceInlineProcessor): |
no outgoing calls
no test coverage detected