125
QString replaceIRCMarkups(const QString& text)
133
QString tagUrls(const QString& text, const QString& fromNick, bool useCustomColor)
129
line.replace('\x02', "%B"); // replace bold char with %B
130
line.replace('\x03', "%C"); // replace color char with %C
131
line.replace('\x07', "%G"); // replace ASCII BEL 0x07 with %G
132
line.replace('\x1d', "%I"); // replace italics char with %I
133
line.replace('\x0f', "%O"); // replace reset to default char with %O
134
line.replace('\x13', "%S"); // replace strikethru char with %S
135
line.replace('\x16', "%R"); // replace reverse char with %R
136
// underline char send by kvirc
137
line.replace('\x1f', "%U"); // replace underline char with %U
138
// underline char send by mirc
139
line.replace('\x15', "%U"); // replace underline char with %U
135
TextUrlData data = extractUrlData(text, fromNick, false, true, useCustomColor);
137
return data.htmlText;
144
140
QList<QPair<int, int> > getUrlRanges(const QString& text)
146
TextUrlData data = extractUrlData(text, false);
142
TextUrlData data = extractUrlData(text, QString(), true, false, false);
148
144
return data.urlRanges;
151
QList< QPair< int, int > > getChannelRanges(const QString& text)
153
TextChannelData data = extractChannelData(text, false);
155
return data.channelRanges;
158
TextUrlData extractUrlData(const QString& text, bool doUrlFixup)
147
TextUrlData extractUrlData(const QString& text, const QString& fromNick, bool doUrlRanges,
148
bool doHyperlinks, bool useCustomHyperlinkColor)
160
153
TextUrlData data;
161
QString htmlText(text);
162
urlPattern.setCaseSensitivity(Qt::CaseInsensitive);
154
data.htmlText = text;
167
161
QString protocol;
170
while ((pos = urlPattern.indexIn(htmlText, pos)) >= 0)
165
urlPattern.setCaseSensitivity(Qt::CaseInsensitive);
169
QString linkColor = Preferences::self()->color(Preferences::Hyperlink).name();
171
if (useCustomHyperlinkColor)
172
link = "<a href=\"#%1\" style=\"color:" + linkColor + "\">%2</a>";
174
link = "<a href=\"#%1\">%2</a>";
176
if (data.htmlText.contains("#"))
178
QRegExp chanExp("(^|\\s|^\"|\\s\"|,|'|\\(|\\:|!|@|%|\\+)(#[^,\\s;\\)\\:\\/\\(\\<\\>]*[^.,\\s;\\)\\:\\/\\(\"\''\\<\\>])");
180
while ((pos = chanExp.indexIn(data.htmlText, pos)) >= 0)
182
href = chanExp.cap(2);
183
urlLen = href.length();
184
pos += chanExp.cap(1).length();
186
insertText = link.arg(href, href);
187
data.htmlText.replace(pos, urlLen, insertText);
188
pos += insertText.length();
192
if (useCustomHyperlinkColor)
193
link = "<a href=\"%1%2\" style=\"color:" + linkColor + "\">%3</a>";
195
link = "<a href=\"%1%2\">%3</a>";
201
while ((pos = urlPattern.indexIn(data.htmlText, pos)) >= 0)
172
203
urlLen = urlPattern.matchedLength();
173
href = htmlText.mid(pos, urlLen);
175
data.urlRanges << QPair<int, int>(pos, href.length());
176
pos += href.length();
181
if (urlPattern.cap(2).isEmpty())
205
// check if the matched text is already replaced as a channel
206
if (doHyperlinks && data.htmlText.lastIndexOf("<a", pos ) > data.htmlText.lastIndexOf("</a>", pos))
213
href = data.htmlText.mid(pos, urlLen);
216
// Don't consider trailing comma part of link.
217
if (href.right(1) == ",")
219
href.truncate(href.length()-1);
223
// Don't consider trailing semicolon part of link.
224
if (href.right(1) == ";")
226
href.truncate(href.length()-1);
230
// Don't consider trailing closing parenthesis part of link when
231
// there's an opening parenthesis preceding the beginning of the
232
// URL or there is no opening parenthesis in the URL at all.
233
if (href.right(1) == ")" && (data.htmlText.mid(pos-1, 1) == "(" || !href.contains("(")))
235
href.truncate(href.length()-1);
241
// Qt doesn't support (?<=pattern) so we do it here
242
if ((pos > 0) && data.htmlText[pos-1].isLetterOrNumber())
183
QString urlPatternCap1(urlPattern.cap(1));
184
if (urlPatternCap1.contains('@'))
185
protocol = "mailto:";
186
else if (urlPatternCap1.startsWith(QLatin1String("ftp."), Qt::CaseInsensitive))
189
protocol = "http://";
192
href = protocol + removeIrcMarkup(href);
193
data.fixedUrls.append(href);
248
if (urlPattern.cap(1).startsWith(QLatin1String("www."), Qt::CaseInsensitive))
249
protocol = "http://";
250
else if (urlPattern.cap(1).isEmpty())
251
protocol = "mailto:";
253
// Use \x0b as a placeholder for & so we can read them after changing all & in the normal text to &
254
insertText = link.arg(protocol, QString(href).replace('&', "\x0b"), href) + append;
256
data.htmlText.replace(pos, urlLen, insertText);
258
Application::instance()->storeUrl(fromNick, href, QDateTime::currentDateTime());
261
insertText = href + append;
264
data.urlRanges << QPair<int, int>(pos, href.length());
266
pos += insertText.length();
199
TextChannelData extractChannelData(const QString& text, bool doChannelFixup)
201
TextChannelData data;
202
QString ircText(text);
208
while ((pos = chanExp.indexIn(ircText, pos)) >= 0)
210
channel = chanExp.cap(2);
211
chanLen = channel.length();
213
// we want the pos where #channel starts
214
// indexIn gives us the first match and the first match may be
215
// "#test", " #test" or " \"test", so the first Index is off by some chars
216
pos = chanExp.pos(2);
218
data.channelRanges << QPair<int, int>(pos, chanLen);
223
channel = removeIrcMarkup(channel);
224
data.fixedChannels.append(channel);
271
// Change & to & to prevent html entities to do strange things to the text
272
data.htmlText.replace('&', "&");
273
data.htmlText.replace("\x0b", "&");
276
// kDebug() << "Took (msecs) : " << timer.elapsed() << " for " << data.htmlText;