• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
text-parser.cpp
Go to the documentation of this file.
1 /*
2  * Text Parser common class
3  * Copyright (C) 2004 Peter Simonsson <psn@linux.se>
4  * Copyright (C) 2006-2008 Eike Hein <hein@kde.org>
5  * Copyright (C) 2011 Przemek Czekaj <xcojack@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "text-parser.h"
23 
24 #include <QtCore/QLatin1String>
25 
26 namespace KTp
27 {
28 
29 TextParser* TextParser::s_instance = NULL;
30 
34 static QRegExp s_urlPattern(QString::fromLatin1("\\b((?:(?:([a-z][\\w\\.-]+:/{1,3})|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|\\}\\]|[^\\s`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6])|[a-z0-9.\\-+_]+@[a-z0-9.\\-]+[.][a-z]{1,5}[^\\s/`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6]))")
35  .arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
36 
37 TextParser::TextParser(QObject* parent)
38  : QObject(parent)
39 {
40 }
41 
42 TextParser* TextParser::instance()
43 {
44  if (!s_instance) {
45  s_instance = new TextParser(0);
46  }
47 
48  return s_instance;
49 }
50 
51 TextUrlData TextParser::extractUrlData(const QString& text, bool doUrlFixup)
52 {
53  TextUrlData data;
54  QString htmlText(text);
55  s_urlPattern.setCaseSensitivity(Qt::CaseInsensitive);
56 
57  int pos = 0;
58  int urlLen = 0;
59 
60  QString protocol;
61  QString href;
62 
63  while ((pos = s_urlPattern.indexIn(htmlText, pos)) >= 0) {
64  urlLen = s_urlPattern.matchedLength();
65  href = htmlText.mid(pos, urlLen);
66 
67  data.urlRanges << QPair<int, int>(pos, href.length());
68  pos += href.length();
69 
70  if (doUrlFixup) {
71  protocol.clear();
72  if (s_urlPattern.cap(2).isEmpty()) {
73  QString urlPatternCap1(s_urlPattern.cap(1));
74  if (urlPatternCap1.contains(QLatin1Char('@'))) {
75  protocol = QLatin1String("mailto:");
76  } else if (urlPatternCap1.startsWith(QLatin1String("ftp."), Qt::CaseInsensitive)) {
77  protocol = QLatin1String("ftp://");
78  } else {
79  protocol = QLatin1String("http://");
80  }
81  }
82 
83  href = protocol + href;
84  data.fixedUrls.append(href);
85  }
86  }
87  return data;
88 }
89 
90 TextParser::~TextParser()
91 {
92 }
93 
94 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal