2
* Copyright (C) 2012 Lasath Fernando <kde@lasath.org>
3
* Copyright (C) 2012 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "format-filter.h"
24
#include <KPluginFactory>
27
typedef QPair<QRegExp, QString> FormatTag;
29
class FormatFilter::Private {
31
QList<FormatTag> tags;
33
QString allTagsPattern;
35
void addTag (const QString &markingCharacter, const QString &htmlTag);
36
QString filterString(QString string);
39
FormatFilter::FormatFilter (QObject* parent, const QVariantList&) :
40
KTp::AbstractMessageFilter (parent),
44
// 1. The beginning of the string or a white character [ (^|\\s) ]
45
// 2. Depending on the regexp the tag to be replaced or a pattern including
46
// all the tags in tagsMap [ %1 ]
47
// 3. One non-whitespace character, or by any string that starts and ends
48
// with a non-whitespace character [ (\\S|\\S.*\\S) ]
49
// 4. Same as 2. [ %1 ]
50
// 5. A white character or the end of the string [ (\\s|$) ]
51
d->mainPattern = QLatin1String("(^|\\s)%1(\\S|\\S.*\\S)%1(\\s|$)");
53
QMap<QString, QString> tagsMap;
55
tagsMap[QLatin1String("_")] = QLatin1Char('u');
56
tagsMap[QLatin1String("*")] = QLatin1Char('b');
57
tagsMap[QLatin1String("-")] = QLatin1Char('s');
58
tagsMap[QLatin1String("/")] = QLatin1Char('i');
60
d->allTagsPattern = QLatin1Char('(');
62
QMapIterator<QString, QString> i(tagsMap);
66
d->allTagsPattern += QRegExp::escape(i.key());
68
d->allTagsPattern += QLatin1Char('|');
70
d->addTag(i.key(), i.value());
73
d->allTagsPattern += QLatin1Char(')');
76
FormatFilter::~FormatFilter()
81
void FormatFilter::filterMessage (KTp::Message& message, const KTp::MessageContext &context)
84
message.setMainMessagePart(d->filterString(message.mainMessagePart()));
87
QString FormatFilter::Private::filterString(QString string)
89
QRegExp rx(mainPattern.arg(allTagsPattern));
93
while ((pos = string.indexOf(rx, pos)) != -1) {
94
string = string.replace(rx.cap(3), filterString(rx.cap(3)));
95
pos += rx.matchedLength();
98
Q_FOREACH(const FormatTag &tag, tags) {
99
string = string.replace(tag.first, tag.second);
105
void FormatFilter::Private::addTag (const QString &markingCharacter, const QString &htmlTag)
107
QString repl = QLatin1String("\\1<%1>%2\\2%2</%1>\\3");
108
repl = repl.arg(htmlTag).arg(markingCharacter);
110
QRegExp exp = QRegExp(mainPattern.arg(QRegExp::escape(markingCharacter)));
111
exp.setMinimal(true);
113
tags.append(FormatTag(exp, repl));
116
K_PLUGIN_FACTORY(MessageFilterFactory, registerPlugin<FormatFilter>();)
117
K_EXPORT_PLUGIN(MessageFilterFactory("ktptextui_message_filter_formatting"))