| 1 |
"""Process links like https://example.org/""" |
| 2 |
import re |
| 3 |
|
| 4 |
from .state_inline import StateInline |
| 5 |
|
| 6 |
# RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
| 7 |
SCHEME_RE = re.compile(r"(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$", re.IGNORECASE) |
| 8 |
|
| 9 |
|
| 10 |
def linkify(state: StateInline, silent: bool) -> bool: |
| 11 |
"""Rule for identifying plain-text links.""" |
| 12 |
if not state.md.options.linkify: |
| 13 |
return False |
| 14 |
if state.linkLevel > 0: |
| 15 |
return False |
| 16 |
if not state.md.linkify: |
| 17 |
raise ModuleNotFoundError("Linkify enabled but not installed.") |
| 18 |
|
| 19 |
pos = state.pos |
| 20 |
maximum = state.posMax |
| 21 |
|
| 22 |
if ( |
| 23 |
(pos + 3) > maximum |
| 24 |
or state.src[pos] != ":" |
| 25 |
or state.src[pos + 1] != "/" |
| 26 |
or state.src[pos + 2] != "/" |
| 27 |
): |
| 28 |
return False |
| 29 |
|
| 30 |
if not (match := SCHEME_RE.match(state.pending)): |
| 31 |
return False |
| 32 |
|
| 33 |
proto = match.group(1) |
| 34 |
if not (link := state.md.linkify.match_at_start(state.src[pos - len(proto) :])): |
| 35 |
return False |
| 36 |
url: str = link.url |
| 37 |
|
| 38 |
# disallow '*' at the end of the link (conflicts with emphasis) |
| 39 |
url = url.rstrip("*") |
| 40 |
|
| 41 |
full_url = state.md.normalizeLink(url) |
| 42 |
if not state.md.validateLink(full_url): |
| 43 |
return False |
| 44 |
|
| 45 |
if not silent: |
| 46 |
state.pending = state.pending[: -len(proto)] |
| 47 |
|
| 48 |
token = state.push("link_open", "a", 1) |
| 49 |
token.attrs = {"href": full_url} |
| 50 |
token.markup = "linkify" |
| 51 |
token.info = "auto" |
| 52 |
|
| 53 |
token = state.push("text", "", 0) |
| 54 |
token.content = state.md.normalizeLinkText(url) |
| 55 |
|
| 56 |
token = state.push("link_close", "a", -1) |
| 57 |
token.markup = "linkify" |
| 58 |
token.info = "auto" |
| 59 |
|
| 60 |
state.pos += len(url) - len(proto) |
| 61 |
return True |