X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/22236fb67da2f27ceb35ad138f4d534a4fd08251..0bd53bfd0a08e9949651ec35bf22048db8e740d9:/forum/markdownext/mdx_auto_linker.py diff --git a/forum/markdownext/mdx_auto_linker.py b/forum/markdownext/mdx_auto_linker.py index 9d77c38..c1ec9aa 100644 --- a/forum/markdownext/mdx_auto_linker.py +++ b/forum/markdownext/mdx_auto_linker.py @@ -14,26 +14,28 @@ TLDS = ('gw', 'gu', 'gt', 'gs', 'gr', 'gq', 'gp', 'gy', 'gg', 'gf', 'ge', 'gd', 'mu', 'mt', 'mw', 'mv', 'mq', 'ms', 'mr', 'im', 'ug', 'my', 'mx', 'il', 'pro', 'ac', 'sa', 'ae', 'ad', 'ag', 'af', 'ai', 'vi', 'is', 'ir', 'am', 'al', 'ao', 'an', 'aq', 'as', 'ar', 'au', 'at', 'aw', 'in', 'ax', 'az', 'ie', 'id', 'sr', 'nl', 'mil', 'no', 'na', 'travel', 'nc', 'ne', 'nf', 'ng', 'nz', 'dm', 'np', - 'so', 'nr', 'nu', 'fr', 'io', 'ni', 'ye', 'sv', 'jsp', 'kz', 'fi', 'fj', 'php', 'fm', 'fo', 'tj', 'sz', 'sy', + 'so', 'nr', 'nu', 'fr', 'io', 'ni', 'ye', 'sv', 'kz', 'fi', 'fj', 'fm', 'fo', 'tj', 'sz', 'sy', 'mobi', 'kg', 'ke', 'doc', 'ki', 'kh', 'kn', 'km', 'st', 'sk', 'kr', 'si', 'kp', 'kw', 'sn', 'sm', 'sl', 'sc', 'biz', 'ky', 'sg', 'se', 'sd') AUTO_LINK_RE = re.compile(r""" (?P.?\s*) (?P - (?P + (?:(?P ((?P[a-z][a-z]+)://)? (?P\w(?:[\w-]*\w)?\.\w(?:[\w-]*\w)?(?:\.\w(?:[\w-]*\w)?)*) ) | (?P ((?P[a-z][a-z]+)://) (?P\w(?:[\w-]*\w)?(?:\.\w(?:[\w-]*\w)?)*) - ) + )) (?P:\d+)? (?P/[^\s<]*)? ) """, re.X | re.I) +EMAIL_LINK_REPLACE_RE = re.compile("(?<= href=\")[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(?=\")") + def is_ip(addr): try: socket.inet_aton(addr) @@ -45,7 +47,7 @@ def replacer(m): ws = m.group('ws') - if ws and ws[0] in ("'", '"'): + if ws and ws[0] in ("'", '"', "@"): return m.group(0) elif not ws: @@ -62,7 +64,7 @@ def replacer(m): if not protocol: domain_chunks = domain.split('.') - if not ((len(domain_chunks) == 1 and domain_chunks[0].lower() == 'localhost') or (domain_chunks[-1].lower() in TLDS)): + if not (len(domain_chunks) == 1 and domain_chunks[0].lower() == 'localhost') or (domain_chunks[-1].lower() in TLDS): return m.group(0) if (not protocol) and is_ip(domain): @@ -92,7 +94,10 @@ def replacer(m): class AutoLinker(markdown.postprocessors.Postprocessor): def run(self, text): - return AUTO_LINK_RE.sub(replacer, text) + text = AUTO_LINK_RE.sub(replacer, text) + text = EMAIL_LINK_REPLACE_RE.sub(lambda m: "mailto:%s" % m.group(0), text) + + return text class AutoLinkerExtension(markdown.Extension):