Permalink
Browse files

Add autolinking and tests.

  • Loading branch information...
rsenk330 committed Dec 20, 2012
1 parent 8e9716e commit fe7eae8813289ff7ed0104f11b114e5202cfb033
Showing with 83 additions and 0 deletions.
  1. +1 −0 pypeline/filters/__init__.py
  2. +8 −0 pypeline/filters/autolink.py
  3. +74 −0 pypeline/filters/tests/autolink_tests.py
@@ -1,3 +1,4 @@
+from .autolink import autolink
from .markdown import markdown, SyntaxRenderer
from .mention import Mention as mention
from .plaintext import plaintext
@@ -0,0 +1,8 @@
+# from lxml.html import fromstring, tostring
+from lxml.html.clean import autolink_html
+
+def autolink(context={}):
+ def render(content):
+ return autolink_html(content)
+
+ return render
@@ -0,0 +1,74 @@
+from lxml.html import fromstring
+
+from pypeline.filters import autolink
+from pypeline.utils import unittest
+
+class AutoLinkTests(unittest.TestCase):
+ def setUp(self):
+ self.filter = autolink()
+
+ def test_render_http(self):
+ """Tests to see if http links are converted to `<a></a>` elements"""
+ html = """\
+<div>
+ <p>http://test.com</p>
+</div>
+"""
+ filtered = fromstring(self.filter(html))
+
+ anchors = filtered.findall("..//a")
+
+ self.assertEqual(len(anchors), 1)
+ self.assertEqual(anchors[0].attrib['href'], "http://test.com")
+ self.assertEqual(anchors[0].text, "http://test.com")
+
+ def test_render_https(self):
+ """Tests to see if http links are converted to `<a></a>` elements"""
+ html = """\
+<div>
+ <p>https://test.com</p>
+</div>
+"""
+ filtered = fromstring(self.filter(html))
+
+ anchors = filtered.findall("..//a")
+
+ self.assertEqual(len(anchors), 1)
+ self.assertEqual(anchors[0].attrib['href'], "https://test.com")
+ self.assertEqual(anchors[0].text, "https://test.com")
+
+ def test_render_mailto(self):
+ """Tests to see if mailto links are converted to `<a></a>` elements"""
+ html = """\
+<div>
+ <p>mailto:test@test.com</p>
+</div>
+"""
+ filtered = fromstring(self.filter(html))
+
+ anchors = filtered.findall("..//a")
+
+ self.assertEqual(len(anchors), 1)
+ self.assertEqual(anchors[0].attrib['href'], "mailto:test@test.com")
+ self.assertEqual(anchors[0].text, "test@test.com")
+
+ def test_render_multiple(self):
+ """Tests to see if all links are converted to `<a></a>` elements"""
+ html = """\
+<div>
+ <p>https://test.com</p>
+ <p>http://test.com</p>
+ <p>mailto:test@test.com</p>
+</div>
+"""
+ filtered = fromstring(self.filter(html))
+
+ anchors = filtered.findall("..//a")
+
+ self.assertEqual(len(anchors), 3)
+ self.assertEqual(anchors[0].attrib['href'], "https://test.com")
+ self.assertEqual(anchors[0].text, "https://test.com")
+ self.assertEqual(anchors[1].attrib['href'], "http://test.com")
+ self.assertEqual(anchors[1].text, "http://test.com")
+ self.assertEqual(anchors[2].attrib['href'], "mailto:test@test.com")
+ self.assertEqual(anchors[2].text, "test@test.com")

0 comments on commit fe7eae8

Please sign in to comment.