diff options
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()); } } |