|
@@ -5,10 +5,10 @@ from markdown.inlinepatterns import LinkPattern
|
|
from markdown.postprocessors import RawHtmlPostprocessor
|
|
from markdown.postprocessors import RawHtmlPostprocessor
|
|
from markdown.util import etree
|
|
from markdown.util import etree
|
|
from misago.utils.strings import html_escape
|
|
from misago.utils.strings import html_escape
|
|
-from misago.utils.urls import is_inner, clean_inner
|
|
|
|
|
|
+from misago.utils.urls import is_inner, clean_inner, clean_outer
|
|
|
|
|
|
# Global vars
|
|
# Global vars
|
|
-MAGICLINKS_RE = re.compile(r'(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))', re.UNICODE)
|
|
|
|
|
|
+MAGICLINKS_RE = re.compile(r'(\<)?(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))(\>)?', re.UNICODE)
|
|
|
|
|
|
class MagicLinksExtension(markdown.Extension):
|
|
class MagicLinksExtension(markdown.Extension):
|
|
def extendMarkdown(self, md):
|
|
def extendMarkdown(self, md):
|
|
@@ -24,14 +24,21 @@ class MagicLinksTreeprocessor(markdown.treeprocessors.Treeprocessor):
|
|
|
|
|
|
def walk_tree(self, node):
|
|
def walk_tree(self, node):
|
|
def parse_link(matchobj):
|
|
def parse_link(matchobj):
|
|
|
|
+ matched_link = matchobj.group(0).strip()
|
|
|
|
+ if matched_link[0] == '<':
|
|
|
|
+ matched_link = matched_link[1:]
|
|
|
|
+ if matched_link[-1] == '>':
|
|
|
|
+ matched_link = matched_link[:-1]
|
|
|
|
+
|
|
link = LinkPattern(MAGICLINKS_RE, self.markdown)
|
|
link = LinkPattern(MAGICLINKS_RE, self.markdown)
|
|
- href = link.sanitize_url(link.unescape(matchobj.group(0).strip()))
|
|
|
|
|
|
+ href = link.sanitize_url(link.unescape(matched_link))
|
|
if href:
|
|
if href:
|
|
if is_inner(href):
|
|
if is_inner(href):
|
|
clean = clean_inner(href)
|
|
clean = clean_inner(href)
|
|
return self.markdown.htmlStash.store('<a href="%s">%s</a>' % (clean, clean[1:]), safe=True)
|
|
return self.markdown.htmlStash.store('<a href="%s">%s</a>' % (clean, clean[1:]), safe=True)
|
|
else:
|
|
else:
|
|
- return self.markdown.htmlStash.store('<a href="%(href)s" rel="nofollow">%(href)s</a>' % {'href': href}, safe=True)
|
|
|
|
|
|
+ clean = clean_outer(href)
|
|
|
|
+ return self.markdown.htmlStash.store('<a href="%s" rel="nofollow">%s</a>' % (clean, href), safe=True)
|
|
else:
|
|
else:
|
|
return matchobj.group(0)
|
|
return matchobj.group(0)
|
|
|
|
|
|
@@ -41,4 +48,4 @@ class MagicLinksTreeprocessor(markdown.treeprocessors.Treeprocessor):
|
|
if node.tail and unicode(node.tail).strip():
|
|
if node.tail and unicode(node.tail).strip():
|
|
node.tail = MAGICLINKS_RE.sub(parse_link, unicode(node.tail))
|
|
node.tail = MAGICLINKS_RE.sub(parse_link, unicode(node.tail))
|
|
for i in node:
|
|
for i in node:
|
|
- self.walk_tree(i)
|
|
|
|
|
|
+ self.walk_tree(i)
|