Permalink
Browse files

Add autolink filter.

  • Loading branch information...
2 parents 8e9716e + 98e1ff4 commit 60e31b2bb666cebfdadc3350cf8832f1618a3230 @rsenk330 committed Dec 20, 2012
Showing with 95 additions and 3 deletions.
  1. +5 −3 README.md
  2. +1 −0 pypeline/filters/__init__.py
  3. +15 −0 pypeline/filters/autolink.py
  4. +74 −0 pypeline/filters/tests/autolink_tests.py
View
@@ -32,14 +32,16 @@ Most of the filters provided by [HTML::Pipeline](https://github.com/jch/html-pip
Filters that are currently completed:
-* **MarkdownFilter**: Markdown -> HTML
-* **PlainTextInputFilter**: Escapes HTML tags and wraps in a div
+* **MarkdownFilter***: Markdown -> HTML
+* **PlainTextInputFilter***: Escapes HTML tags and wraps in a div
+* **AutolinkFilter**: Automatic linking of URLs
* **MentionFilter**: Replace `@mentions` with a URL
* **SyntaxHighlightFilter**: Syntax highlighting
+*Only one of these can be used at a time, and they must be the _first_ filter in the pipeline.
+
Filters that are being worked on:
-* **AutolinkFilter**: Automatic linking of URLs
* **EmojiFilter**: Replace [emoji](http://www.emoji-cheat-sheet.com/) tags with images
* **HttpsFilter**: Replace HTTP URLs with HTTPS
* **ImageMaxWidthFilter**: Link to the full size image when creating image previews
@@ -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,15 @@
+# from lxml.html import fromstring, tostring
+from lxml.html.clean import autolink_html
+
+def autolink(context={}):
+ """The autolink filter automatically add links.
+
+ It does this by looking for things that look like links, which includes
+ anything starting with `http`, `https`, and `mailto` and replaces it
+ with an anchor element.
+
+ """
+ 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 60e31b2

Please sign in to comment.