diff options
| author | eivanov89 <[email protected]> | 2025-08-29 10:12:02 +0300 |
|---|---|---|
| committer | eivanov89 <[email protected]> | 2025-08-29 10:27:27 +0300 |
| commit | 140ced4d34c422c9f3cbe096f8dd35243b67d6e4 (patch) | |
| tree | b7373341f64151c0ab9839ee692dc919366590d5 /contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py | |
| parent | 136471c8b2f3ab8cd7993200c0de0456b7018118 (diff) | |
Add python/textual to YDB
commit_hash:eda16a869229724fec5479fa27fa5cdbccbe0395
Diffstat (limited to 'contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py')
| -rw-r--r-- | contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py b/contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py new file mode 100644 index 00000000000..6546e2502f9 --- /dev/null +++ b/contrib/python/markdown-it-py/markdown_it/rules_inline/autolink.py @@ -0,0 +1,77 @@ +# Process autolinks '<protocol:...>' +import re + +from .state_inline import StateInline + +EMAIL_RE = re.compile( + r"^([a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$" +) +AUTOLINK_RE = re.compile(r"^([a-zA-Z][a-zA-Z0-9+.\-]{1,31}):([^<>\x00-\x20]*)$") + + +def autolink(state: StateInline, silent: bool) -> bool: + pos = state.pos + + if state.src[pos] != "<": + return False + + start = state.pos + maximum = state.posMax + + while True: + pos += 1 + if pos >= maximum: + return False + + ch = state.src[pos] + + if ch == "<": + return False + if ch == ">": + break + + url = state.src[start + 1 : pos] + + if AUTOLINK_RE.search(url) is not None: + fullUrl = state.md.normalizeLink(url) + if not state.md.validateLink(fullUrl): + return False + + if not silent: + token = state.push("link_open", "a", 1) + token.attrs = {"href": fullUrl} + token.markup = "autolink" + token.info = "auto" + + token = state.push("text", "", 0) + token.content = state.md.normalizeLinkText(url) + + token = state.push("link_close", "a", -1) + token.markup = "autolink" + token.info = "auto" + + state.pos += len(url) + 2 + return True + + if EMAIL_RE.search(url) is not None: + fullUrl = state.md.normalizeLink("mailto:" + url) + if not state.md.validateLink(fullUrl): + return False + + if not silent: + token = state.push("link_open", "a", 1) + token.attrs = {"href": fullUrl} + token.markup = "autolink" + token.info = "auto" + + token = state.push("text", "", 0) + token.content = state.md.normalizeLinkText(url) + + token = state.push("link_close", "a", -1) + token.markup = "autolink" + token.info = "auto" + + state.pos += len(url) + 2 + return True + + return False |
