Commits

David Cramer committed 098c2c1

Improve auto links

Comments (0)

Files changed (3)

     
 class AutoDetectURL(SelfClosingTagNode):
     # XXX: might be better to enumerate words rather than use regexp?
-    open_pattern = re.compile(r'(?P<before>[^[\]])(?P<url>(?P<protocol>(?:ht|f)tp(?:s?)\:\/\/|~/|/'
-                              ')?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomain'
-                              's)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org'
-                              '|net|gov|mil|biz|info|mobi|name|aero|jobs|museum'
-                              '|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Dire'
-                              'ctories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+'
-                              ')+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|'
-                              '%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)('
-                              '?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.'
-                              ',*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+'
-                              '|.,*:=]|%[a-f\d]{2})*)?)(?P<after>[^\s\[])', re.I)
+    #open_pattern = re.compile(r'(?P<before>[^\]])(?P<url>(?:http|ftp|https)://[\w\-_]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?', re.I)
+    open_pattern = re.compile(r'(?P<before>[^\]=])(?P<url>(?:(?P<protocol>https?|ftp)://(?:\w+@)?|www\.)[\w.-]+(?:/\S+)?)(?P<after>\b)')
+    # open_pattern = re.compile(r'(?P<before>[^[\]])(?P<url>(?P<protocol>(?:ht|f)tp(?:s?)\:\/\/|~/|/'
+    #                           ')?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomain'
+    #                           's)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org'
+    #                           '|net|gov|mil|biz|info|mobi|name|aero|jobs|museum'
+    #                           '|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Dire'
+    #                           'ctories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+'
+    #                           ')+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|'
+    #                           '%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)('
+    #                           '?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.'
+    #                           ',*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+'
+    #                           '|.,*:=]|%[a-f\d]{2})*)?)(?P<after>[^\s\[])', re.I)
     
     def parse(self):
         match = self.match.groupdict()
 register(Img)
 register(Email)
 register(Youtube)
-# TODO: This breaks at least 50% of the time, needs fixed
-#register(AutoDetectURL)
+register(AutoDetectURL)

bbcode/models.py

Empty file added.
+import bbcode
+import unittest
+
+parse = lambda x: bbcode.parse(x, auto_discover=True)[0]
+
+class LinkTestCase(unittest.TestCase):
+    def testAutoLinks(self):
+        self.assertEquals(parse('Check out ]http://example.com'), 'Check out ]http://example.com')
+        self.assertEquals(parse('Check outhttp://example.com'), 'Check outhttp://example.com')
+        self.assertEquals(parse('Check out http://example.com'), 'Check out <a href="http://example.com">http://example.com</a>')
+        self.assertEquals(parse('Check out www.example.com'), 'Check out <a href="http://www.example.com">www.example.com</a>')
+
+    def testURL(self):
+        self.assertEquals(parse('Check out [url]http://example.com[/url]'), 'Check out <a href="http://example.com">http://example.com</a>')
+
+if __name__ == '__main__':
+    unittest.main()