~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/textutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#include "psiiconset.h"
6
6
#include "rtparse.h"
7
7
 
 
8
// Qt::escape() doesn't escape " to " -- it sucks
 
9
QString TextUtil::escape(const QString& plain)
 
10
{
 
11
        QString rich;
 
12
        rich.reserve(int(plain.length() * 1.1));
 
13
        for (int i = 0; i < plain.length(); ++i) {
 
14
                if (plain.at(i) == QLatin1Char('<'))
 
15
                        rich += QLatin1String("&lt;");
 
16
                else if (plain.at(i) == QLatin1Char('>'))
 
17
                        rich += QLatin1String("&gt;");
 
18
                else if (plain.at(i) == QLatin1Char('"'))
 
19
                        rich += QLatin1String("&quot;");
 
20
                else if (plain.at(i) == QLatin1Char('&'))
 
21
                        rich += QLatin1String("&amp;");
 
22
                else
 
23
                        rich += plain.at(i);
 
24
        }
 
25
        return rich;
 
26
}
 
27
 
 
28
QString TextUtil::unescape(const QString& escaped)
 
29
{
 
30
        QString plain = escaped;
 
31
        plain.replace("&lt;", "<");
 
32
        plain.replace("&gt;", ">");
 
33
        plain.replace("&quot;", "\"");
 
34
        plain.replace("&amp;", "&");
 
35
        return plain;
 
36
}
 
37
 
8
38
QString TextUtil::quote(const QString &toquote, int width, bool quoteEmpty)
9
39
{
10
40
        int ql = 0, col = 0, atstart = 1, ls=0;
148
178
                        if(in[i] == QChar::nbsp)
149
179
                                out += ' ';
150
180
                        else if(in[i] != '\n') {
151
 
                                if(i == 0)
 
181
                                if(i == 0 || out.length() == 0)
152
182
                                        out += ' ';
153
183
                                else {
154
184
                                        QChar last = out.at(out.length()-1);
271
301
        return TRUE;
272
302
}
273
303
 
 
304
/**
 
305
 * takes a richtext string and heuristically adds links for uris of common protocols
 
306
 * @return a richtext string with link markup added
 
307
 */
274
308
QString TextUtil::linkify(const QString &in)
275
309
{
276
310
        QString out = in;
349
383
                                continue;
350
384
                        }
351
385
                        href += link;
 
386
                        // attributes need to be encoded too.
 
387
                        href = Qt::escape(href);
352
388
                        href = linkify_htmlsafe(href);
353
389
                        //printf("link: [%s], href=[%s]\n", link.latin1(), href.latin1());
354
390
                        linked = QString("<a href=\"%1\">").arg(href) + Qt::escape(link) + "</a>" + Qt::escape(pre.mid(cutoff));
411
447
 
412
448
                        Q3PtrListIterator<Iconset> iconsets(PsiIconset::instance()->emoticons);
413
449
                        Iconset *iconset;
414
 
                        while ( (iconset = iconsets.current()) != 0 ) {
 
450
                        while ( (iconset = iconsets.current()) != 0 ) {
415
451
                                QListIterator<PsiIcon*> it = iconset->iterator();
416
452
                                while ( it.hasNext()) {
417
453
                                        PsiIcon *icon = it.next();
432
468
                                                        continue;
433
469
 
434
470
                                                if(ePos == -1 || n < ePos || (rx.matchedLength() > foundLen && n < ePos + foundLen)) {
 
471
                                                        bool leftSpace  = n == 0 || (n > 0 && str[n-1].isSpace());
 
472
                                                        bool rightSpace = (n+rx.matchedLength() == (int)str.length()) || (n+rx.matchedLength() < (int)str.length() && str[n+rx.matchedLength()].isSpace());
435
473
                                                        // there must be whitespace at least on one side of the emoticon
436
 
                                                        if ( ( n == 0 ) ||
437
 
                                                             ( n+rx.matchedLength() == (int)str.length() ) ||
438
 
                                                             ( n > 0 && str[n-1].isSpace() ) ||
439
 
                                                             ( n+rx.matchedLength() < (int)str.length() && str[n+rx.matchedLength()].isSpace() ) )
440
 
                                                        {
 
474
                                                        if (leftSpace || rightSpace) {
441
475
                                                                ePos = n;
442
476
                                                                closest = icon;
443
477
 
466
500
                        if ( !closest )
467
501
                                break;
468
502
 
469
 
                        p.putRich( QString("<icon name=\"%1\" text=\"%2\">").arg(Qt::escape(closest->name())).arg(Qt::escape(str.mid(foundPos, foundLen))) );
 
503
                        p.putRich( QString("<icon name=\"%1\" text=\"%2\">").arg(TextUtil::escape(closest->name())).arg(TextUtil::escape(str.mid(foundPos, foundLen))) );
470
504
                        i = foundPos + foundLen;
471
505
                }
472
506
        }
486
520
        //out=out.replace(QRegExp("(^[^<>\\s]*|\\s[^<>\\s]*)_(\\S+)_([^<>\\s]*\\s|[^<>\\s]*$)"),"\\1<u>_\\2_</u>\\3");
487
521
 
488
522
        QString out=in;
489
 
        out=out.replace(QRegExp("(^_|\\s_)(\\S+)(_\\s|_$)"),"\\1<u>\\2</u>\\3");
490
 
        out=out.replace(QRegExp("(^\\*|\\s\\*)(\\S+)(\\*\\s|\\*$)"),"\\1<b>\\2</b>\\3");
491
 
        out=out.replace(QRegExp("(^\\/|\\s\\/)(\\S+)(\\/\\s|\\/$)"),"\\1<i>\\2</i>\\3");
 
523
        out=out.replace(QRegExp("(^|\\s|>)_(\\S+)_(<|\\s|$)"),"\\1<u>_\\2_</u>\\3");
 
524
        out=out.replace(QRegExp("(^|\\s|>)\\*(\\S+)\\*(<|\\s|$)"),"\\1<b>*\\2*</b>\\3");
 
525
        out=out.replace(QRegExp("(^|\\s|>)\\/(\\S+)\\/(<|\\s|$)"),"\\1<i>/\\2/</i>\\3");
492
526
 
493
527
        return out;
494
528
}