-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink.h
More file actions
156 lines (129 loc) · 4.1 KB
/
link.h
File metadata and controls
156 lines (129 loc) · 4.1 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef LINK_RECOGNIZER
#define LINK_RECOGNIZER
#include <regex>
#include <iostream>
using namespace std;
extern Index wikiIndex;
class LinkRecognizer
{
static string strip(const string &s)
{
string ret;
int firstIndex = -1, lastIndex = -1;
for(int i = 0; i < s.length(); i++)
{
if(firstIndex == -1 && s[i] != ' ')firstIndex = i;
if(s[i] != ' ')lastIndex = i;
}
for(int i = firstIndex; i <= lastIndex; i++)
ret += s[i];
return ret;
}
public:
static vector<string> findLinks(const string &s)
{
vector<string> result;
string link;
bool inLink = false;
int startLoc;
const string startTag = "[[";
const string endTag = "]]";
int nextMatching = 0;
for(int i = 0; i < s.length(); i++)
{
char c = s[i];
if (!inLink) {
if (c == startTag[nextMatching]) {
nextMatching++;
if (nextMatching == startTag.length()) {
inLink = true;
startLoc = i - 1;
nextMatching = 0;
continue;
}
} else
nextMatching = 0;
} else {
if (c == endTag[nextMatching]) {
nextMatching++;
if (nextMatching == endTag.length()) {
inLink = false;
nextMatching = 0;
link.pop_back();
if(!(startLoc - 1 >= 0 && s[startLoc - 1] == '='))
result.push_back(link);
link = "";
}
} else
nextMatching = 0;
}
if (inLink)
link += c;
}
return result;
}
static vector<string> parseLinks(const string &text) {
vector<string> links;
vector<string> possibleLinks = findLinks(text);
for(auto &linkText : possibleLinks)
{
transform(linkText.begin(), linkText.end(), linkText.begin(), ::tolower);
int delimiters = count(linkText.begin(), linkText.end(), '|');
if (delimiters < 2) {
string original = linkText;
// normal link
if (delimiters == 1)
linkText = linkText.substr(0, linkText.find('|'));
size_t hash = linkText.find('#');
// remove section linking
if (hash != string::npos)
linkText = linkText.substr(0, hash);
replace(linkText.begin(), linkText.end(), '_', ' ');
linkText = strip(linkText);
if (!wikiIndex.lookup(linkText)) {
size_t colon = linkText.find(':');
// ignore links with namespaces except from en(languge)
if (colon != string::npos) {
string colLeft = linkText.substr(0, colon);
if (colLeft != "en" && colLeft != "")
continue;
if (colLeft == "") {
linkText = linkText.substr(1,linkText.length());
}
else if (colLeft == "en")
linkText = linkText.substr(colon + 1, linkText.length() - colon - 1);
}
}
linkText = strip(linkText);
// skip template, help bug with :
if (linkText.find("category:") == 0 || linkText.find("wikipedia:") == 0 || linkText.find("template:") == 0
|| linkText.find("help:") == 0 || linkText.find("portal:") == 0 || linkText.find("mediawiki:") == 0 || linkText.find("file:") == 0 )
continue;
// handle "biology (kingdom)" for ex
if (!wikiIndex.lookup(linkText)) {
size_t paropen = linkText.find('(');
size_t parclose = linkText.find(')');
if (paropen != string::npos && parclose != string::npos &&
paropen < parclose)
linkText =
linkText.substr(0, paropen) +
linkText.substr(parclose + 1, linkText.length() - parclose - 1);
}
// handle "Seattle, Washington"
if (!wikiIndex.lookup(linkText)) {
size_t comma = linkText.find(',');
if (comma != string::npos)
linkText = linkText.substr(0, comma);
}
linkText = strip(linkText);
// if nothing's left => skip (most likely internal linking/sections)
if (linkText == "")
continue;
if(wikiIndex.lookup(linkText))
links.push_back(linkText);
}
}
return links;
}
};
#endif