Yang Zhang avatar Yang Zhang committed fa37dc8

add auto_link

Comments (0)

Files changed (2)

 
 _strong = re.compile(r'\*(?P<strong>.+)\*', re.M)
 
-
+_auto_link = re.compile(r'(?P<link>(http|https)://.+[\b|\s]?)')
 
 def parse_head(content):
     if _header1.match(content)!=None:
     return re.sub(_strong,r'<strong>\g<strong></strong>',content)
 
 
-
+def auto_link(content):
+    return re.sub(_auto_link,r'<a href="\g<link>">\g<link></a>',content)
 
 
 

unittest/parser_test.py

 
 from nose.tools import istest, nottest
 
-from reducedown.parser.parser import parse_head,parse_bold
+from reducedown.parser.parser import parse_head,parse_bold,auto_link
 
 
 def eq(result, expect_result):
     expect_result = '<h1>%s</h1>'%content[1:]
     result = parse_head(content)
     eq(result, expect_result)
-    
-    
+
+
 def test_header2():
     content = "##good good study"
     expect_result = '<h2>%s</h2>'%content[2:]
     result = parse_head(content)
     eq(result, expect_result)
-    
+
 def test_strong():
     content = "something *become strong* expection"
     expect_result = 'something <strong>%s</strong> expection'%"become strong"
     result = parse_bold(content)
-    eq(result, expect_result)
+    eq(result, expect_result)
+
+def test_autolink():
+    link = "http://google.com"
+    content = "good good study %s"%link
+    result = auto_link(content)
+    expect_result ='good good study <a href="%s">%s</a>'%(link,link)
+    eq(result, expect_result)
+
+
+
+
+
+
+
+
+
+
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.