summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-15 13:03:05 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-15 13:33:17 (GMT)
commitbd4e48adb3f23f80abc8441bf359166fbe9b621c (patch)
tree9141e6e7c68e28e14961491459d5996a1d957067 /SwifTools
parent22973faaced00f3307efbb8b8fc4e914de7023e8 (diff)
downloadswift-bd4e48adb3f23f80abc8441bf359166fbe9b621c.zip
swift-bd4e48adb3f23f80abc8441bf359166fbe9b621c.tar.bz2
Linkify URLs.
Diffstat (limited to 'SwifTools')
-rw-r--r--SwifTools/Linkify.cpp17
-rw-r--r--SwifTools/Linkify.h9
-rw-r--r--SwifTools/SConscript15
-rw-r--r--SwifTools/UnitTest/LinkifyTest.cpp60
-rw-r--r--SwifTools/UnitTest/SConscript5
5 files changed, 106 insertions, 0 deletions
diff --git a/SwifTools/Linkify.cpp b/SwifTools/Linkify.cpp
new file mode 100644
index 0000000..8654307
--- /dev/null
+++ b/SwifTools/Linkify.cpp
@@ -0,0 +1,17 @@
+#include "SwifTools/Linkify.h"
+
+#include <boost/regex.hpp>
+
+namespace Swift {
+
+static const boost::regex linkifyRegexp("(https?://([-\\w\\.]+)+(:\\d+)?(/([%-\\w/_\\.]*(\\?\\S+)?)?)?)");
+
+String Linkify::linkify(const String& input) {
+ return String(boost::regex_replace(
+ input.getUTF8String(),
+ linkifyRegexp,
+ "<a href=\"\\1\">\\1</a>",
+ boost::match_default|boost::format_all));
+}
+
+}
diff --git a/SwifTools/Linkify.h b/SwifTools/Linkify.h
new file mode 100644
index 0000000..04182f9
--- /dev/null
+++ b/SwifTools/Linkify.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+ namespace Linkify {
+ String linkify(const String&);
+ }
+}
diff --git a/SwifTools/SConscript b/SwifTools/SConscript
new file mode 100644
index 0000000..2caff5f
--- /dev/null
+++ b/SwifTools/SConscript
@@ -0,0 +1,15 @@
+Import("env")
+
+env["SWIFTOOLS_FLAGS"] = {
+ "LIBPATH": [Dir(".")],
+ "LIBS": ["SwifTools"]
+ }
+
+myenv = env.Clone()
+
+myenv.MergeFlags(myenv["BOOST_FLAGS"])
+myenv.StaticLibrary("SwifTools", [
+ "Linkify.cpp"
+ ])
+
+SConscript(dirs = ["UnitTest"])
diff --git a/SwifTools/UnitTest/LinkifyTest.cpp b/SwifTools/UnitTest/LinkifyTest.cpp
new file mode 100644
index 0000000..9b66614
--- /dev/null
+++ b/SwifTools/UnitTest/LinkifyTest.cpp
@@ -0,0 +1,60 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "SwifTools/Linkify.h"
+
+using namespace Swift;
+
+class LinkifyTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(LinkifyTest);
+ CPPUNIT_TEST(testLinkify_URLWithResource);
+ CPPUNIT_TEST(testLinkify_URLWithEmptyResource);
+ CPPUNIT_TEST(testLinkify_BareURL);
+ CPPUNIT_TEST(testLinkify_URLSurroundedByWhitespace);
+ CPPUNIT_TEST(testLinkify_MultipleURLs);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testLinkify_URLWithResource() {
+ String result = Linkify::linkify("http://swift.im/blog");
+
+ CPPUNIT_ASSERT_EQUAL(
+ String("<a href=\"http://swift.im/blog\">http://swift.im/blog</a>"),
+ result);
+ }
+
+ void testLinkify_URLWithEmptyResource() {
+ String result = Linkify::linkify("http://swift.im/");
+
+ CPPUNIT_ASSERT_EQUAL(
+ String("<a href=\"http://swift.im/\">http://swift.im/</a>"),
+ result);
+ }
+
+
+ void testLinkify_BareURL() {
+ String result = Linkify::linkify("http://swift.im");
+
+ CPPUNIT_ASSERT_EQUAL(
+ String("<a href=\"http://swift.im\">http://swift.im</a>"),
+ result);
+ }
+
+ void testLinkify_URLSurroundedByWhitespace() {
+ String result = Linkify::linkify("Foo http://swift.im/blog Bar");
+
+ CPPUNIT_ASSERT_EQUAL(
+ String("Foo <a href=\"http://swift.im/blog\">http://swift.im/blog</a> Bar"),
+ result);
+ }
+
+ void testLinkify_MultipleURLs() {
+ String result = Linkify::linkify("Foo http://swift.im/blog Bar http://el-tramo.be/about Baz");
+
+ CPPUNIT_ASSERT_EQUAL(
+ String("Foo <a href=\"http://swift.im/blog\">http://swift.im/blog</a> Bar <a href=\"http://el-tramo.be/about\">http://el-tramo.be/about</a> Baz"),
+ result);
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(LinkifyTest);
diff --git a/SwifTools/UnitTest/SConscript b/SwifTools/UnitTest/SConscript
new file mode 100644
index 0000000..2622f39
--- /dev/null
+++ b/SwifTools/UnitTest/SConscript
@@ -0,0 +1,5 @@
+Import("env")
+
+env.Append(UNITTEST_SOURCES = [
+ File("LinkifyTest.cpp")
+ ])