Rafał Pitoń 11 лет назад
Родитель
Сommit
febd50c93a

+ 6 - 3
misago/markdown/extensions/cleanlinks.py

@@ -1,6 +1,6 @@
 import markdown
 from markdown.util import etree
-from misago.utils.urls import is_url, is_inner, clean_inner
+from misago.utils.urls import is_url, is_inner, clean_inner, clean_outer
 
 class CleanLinksExtension(markdown.Extension):
     def extendMarkdown(self, md):
@@ -21,17 +21,20 @@ class CleanLinksTreeprocessor(markdown.treeprocessors.Treeprocessor):
             if is_inner(node.get('href')):
                 node.set('href', clean_inner(node.get('href')))
             else:
+                node.set('href', clean_outer(node.get('href')))
                 node.set('rel', 'nofollow')
+
         if node.tag == 'img':
             if is_inner(node.get('src')):
                 node.set('src', '%s' % clean_inner(node.get('src')))
-
+            else:
+                node.set('src', '%s' % clean_outer(node.get('src')))
         try:
             if self.inurl and node.text and is_url(node.text) and is_inner(node.text):
                 node.text = clean_inner(node.text)[1:]
         except TypeError:
             pass
-            
+
         for i in node:
             self.walk_tree(i)
         self.inurl = False

+ 12 - 5
misago/markdown/extensions/magiclinks.py

@@ -5,10 +5,10 @@ from markdown.inlinepatterns import LinkPattern
 from markdown.postprocessors import RawHtmlPostprocessor
 from markdown.util import etree
 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
-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):
     def extendMarkdown(self, md):
@@ -24,14 +24,21 @@ class MagicLinksTreeprocessor(markdown.treeprocessors.Treeprocessor):
 
     def walk_tree(self, node):
         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)
-            href = link.sanitize_url(link.unescape(matchobj.group(0).strip()))
+            href = link.sanitize_url(link.unescape(matched_link))
             if href:
                 if is_inner(href):
                     clean = clean_inner(href)
                     return self.markdown.htmlStash.store('<a href="%s">%s</a>' % (clean, clean[1:]), safe=True)
                 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:
                 return matchobj.group(0)
 
@@ -41,4 +48,4 @@ class MagicLinksTreeprocessor(markdown.treeprocessors.Treeprocessor):
             if node.tail and unicode(node.tail).strip():
                 node.tail = MAGICLINKS_RE.sub(parse_link, unicode(node.tail))
             for i in node:
-                self.walk_tree(i)
+                self.walk_tree(i)

+ 8 - 1
misago/utils/urls.py

@@ -21,4 +21,11 @@ def clean_inner(string):
         href += '?%s' % parsed.query
     if parsed.fragment:
         href += '#%s' % parsed.fragment
-    return html_escape(href)
+    return html_escape(href)
+
+
+def clean_outer(string):
+    parsed = urlparse(string.strip())
+    if not parsed.scheme:
+        return 'http://%s' % string
+    return string