Commits

Yang Zhang committed 03ee442

add basic parser function

Comments (0)

Files changed (2)

+# -*- coding: utf-8 -*-
+
+import re
+
+
+_header1 = re.compile(r'^#(?!#)(?P<header>.+)', re.M)
+_header2 = re.compile(r'^##(?!#)(?P<header>.+)', re.M)
+
+_strong = re.compile(r'\*(?P<strong>.+)\*', re.M)
+
+
+
+def parse_head(content):
+    if _header1.match(content)!=None:
+        return re.sub(_header1,r'<h1>\g<header></h1>',content)
+    if _header2.match(content)!=None:
+        return re.sub(_header2,r'<h2>\g<header></h2>',content)
+    return content
+
+def parse_bold(content):
+    return re.sub(_strong,r'<strong>\g<strong></strong>',content)
+
+
+
+
+
+
+
+
+
+
+
+
+
+

unittest/parser_test.py

+# -*- coding: utf-8 -*-
+
+from reducedown.parser.parser import parse_head,parse_bold
+
+
+def test_header1():
+    content = "#good good study"
+    expect_result = '<h1>%s</h1>'%content[1:]
+    result = parse_head(content)
+    print "the result", result
+    print "expect result:", expect_result
+    assert  result == expect_result
+    
+def test_header2():
+    content = "##good good study"
+    expect_result = '<h2>%s</h2>'%content[2:]
+    result = parse_head(content)
+    print "the result", result
+    print "expect result:", expect_result
+    assert  result == expect_result
+    
+def test_strong():
+    content = "*become strong*"
+    expect_result = '<strong>%s</strong>'%content[1:-1]
+    result = parse_bold(content)
+    print "the result", result
+    print "expect result:", expect_result
+    assert  result == expect_result