summaryrefslogtreecommitdiffstats
blob: b1557e5649ffad0346d14abb475cc2cbee4bd91d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <SwifTools/Linkify.h>

#include <iostream>
#include <sstream>

#include <boost/regex.hpp>

namespace Swift {

static boost::regex linkifyRegexp("^(https?://|xmpp:).*");

std::string Linkify::linkify(const std::string& input) {
    std::ostringstream result;
    std::vector<char> currentURL;
    bool inURL = false;
    for (size_t i = 0; i < input.size(); ++i) {
        char c = input[i];
        if (inURL) {
            if (c != ' ' && c != '\t' && c != '\n' && !(c == '*' && i == input.size() - 1 && input[0] == '*')) {
                currentURL.push_back(c);
            }
            else {
                std::string url(&currentURL[0], currentURL.size());
                result << "<a href=\"" << url << "\">" <<  url << "</a>";
                currentURL.clear();
                inURL = false;
                result << c;
            }
        }
        else {
            if (boost::regex_match(input.substr(i, 8), linkifyRegexp)) {
                currentURL.push_back(c);
                inURL = true;
            }
            else {
                result << c;
            }
        }
    }
    if (!currentURL.empty()) {
        std::string url(&currentURL[0], currentURL.size());
        result << "<a href=\"" << url << "\">" <<  url << "</a>";
    }
    return std::string(result.str());
}

std::pair<std::vector<std::string>, size_t> Linkify::splitLink(const std::string& input) {
    std::vector<std::string> result;
    std::pair<std::vector<std::string>, size_t> pair;
    std::vector<char> currentURL;
    bool inURL = false;
    size_t urlStartsAt = 0;
    for (size_t i = 0; i < input.size(); ++i) {
        char c = input[i];
        if (inURL) {
            if (c != ' ' && c != '\t' && c != '\n' && !(c == '*' && i == input.size() - 1 && input[0] == '*')) {
                // Keep parsing
            }
            else {
                std::string url(input.substr(urlStartsAt, i - urlStartsAt));
                result.push_back(url);
                inURL = false;
                size_t remaining = input.size() - i;
                if (remaining > 0) {
                    result.push_back(input.substr(i, remaining));
                }
                pair.first = result;
                pair.second = urlStartsAt == 0 ? 0 : 1;
                return pair;
            }
        }
        else {
            if (boost::regex_match(input.substr(i, 8), linkifyRegexp)) {
                urlStartsAt = i;
                inURL = true;
                if (i > 0) {
                    result.push_back(input.substr(0, i));
                }
            }
            else {
                // Just keep swimming
            }
        }
    }
    if (urlStartsAt > 0 || inURL) {
        std::string url(input.substr(urlStartsAt, input.size() - urlStartsAt));
        result.push_back(url);
        pair.first = result;
        pair.second = urlStartsAt == 0 ? 0 : 1;
    }
    else {
        pair.first.push_back(input);
        pair.second = 1;
    }
    return pair;
}

}