Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_inline/linkify.py: 11%
37 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:15 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:15 +0000
1"""Process links like https://example.org/"""
2import re
4from .state_inline import StateInline
6# RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
7SCHEME_RE = re.compile(r"(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$", re.IGNORECASE)
10def 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.")
19 pos = state.pos
20 maximum = state.posMax
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
30 if not (match := SCHEME_RE.match(state.pending)):
31 return False
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
38 # disallow '*' at the end of the link (conflicts with emphasis)
39 url = url.rstrip("*")
41 full_url = state.md.normalizeLink(url)
42 if not state.md.validateLink(full_url):
43 return False
45 if not silent:
46 state.pending = state.pending[: -len(proto)]
48 token = state.push("link_open", "a", 1)
49 token.attrs = {"href": full_url}
50 token.markup = "linkify"
51 token.info = "auto"
53 token = state.push("text", "", 0)
54 token.content = state.md.normalizeLinkText(url)
56 token = state.push("link_close", "a", -1)
57 token.markup = "linkify"
58 token.info = "auto"
60 state.pos += len(url) - len(proto)
61 return True