diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-10-27 19:12:46 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-10-27 19:51:30 (GMT) |
commit | 5f6543647862b5ae1d5abdddd0612e6cf9f23c8f (patch) | |
tree | a46da0aae9787c52963e1df59123a9326a055d02 /SwifTools/Linkify.cpp | |
parent | 6810a2896f27e7ee07aee847f5e8dbccd1f6ec89 (diff) | |
download | swift-5f6543647862b5ae1d5abdddd0612e6cf9f23c8f.zip swift-5f6543647862b5ae1d5abdddd0612e6cf9f23c8f.tar.bz2 |
Changed linkifcation algorithm.
Diffstat (limited to 'SwifTools/Linkify.cpp')
-rw-r--r-- | SwifTools/Linkify.cpp | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/SwifTools/Linkify.cpp b/SwifTools/Linkify.cpp index 0ebe832..0dfa7b5 100644 --- a/SwifTools/Linkify.cpp +++ b/SwifTools/Linkify.cpp @@ -7,17 +7,46 @@ #include "SwifTools/Linkify.h" #include <boost/regex.hpp> +#include <sstream> +#include <iostream> namespace Swift { -static const boost::regex linkifyRegexp("(https?://([@:\\-\\w\\.]+)+(:\\d+)?(/([=%~\\-\\w/_#@\\.\\+]*(\\?\\S+)?)?)?)"); +static boost::regex linkifyRegexp("^https?://.*"); 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)); + std::ostringstream result; + std::vector<char> currentURL; + bool inURL = false; + for (size_t i = 0; i < input.getUTF8Size(); ++i) { + char c = input[i]; + if (inURL) { + if (c >= 33 && c != '\\' && c != '"') { + currentURL.push_back(c); + } + else { + String url(¤tURL[0], currentURL.size()); + result << "<a href=\"" << url << "\">" << url << "</a>"; + currentURL.clear(); + inURL = false; + result << c; + } + } + else { + if (boost::regex_match(input.getSubstring(i, 8).getUTF8String(), linkifyRegexp)) { + currentURL.push_back(c); + inURL = true; + } + else { + result << c; + } + } + } + if (currentURL.size() > 0) { + String url(¤tURL[0], currentURL.size()); + result << "<a href=\"" << url << "\">" << url << "</a>"; + } + return String(result.str()); } } |