File: parseurl.ll

package info (click to toggle)
sim 0.9.5~svn20080806-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 18,108 kB
  • ctags: 11,570
  • sloc: cpp: 119,605; sh: 9,986; ansic: 3,312; perl: 2,752; lex: 1,533; makefile: 839; xml: 206; python: 56
file content (112 lines) | stat: -rw-r--r-- 3,925 bytes parent folder | download
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
%{
/***************************************************************************
                          parse.ll  -  description
                             -------------------
    begin                : Sun Mar 10 2002
    copyright            : (C) 2002 by Vladimir Shutoff
    email                : vovan@shutoff.ru
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "unquot.h"

#include "navigate.h"

#define TXT			1
#define URL			2
#define MAIL_URL	3
#define HTTP_URL	4
#define FTP_URL		5

#define YY_NEVER_INTERACTIVE    1
#define YY_ALWAYS_INTERACTIVE   0
#define YY_MAIN         0

%}

%option nounput
%option nostack
%option prefix="parseurl"

%x x_tag
%x x_word
%x x_link
%%

(http|https|ftp)"://"[A-Za-z0-9/\,\.\?\@\&:\;\(\)\-_\+\'\%=~\#]+ { return URL; }
"file:///"[A-Za-z0-9/\,\.\?\@\&:\;\(\)\-_\+\'\%=\\~\#]+ { return URL; }
(mailto:)?[A-Za-z0-9\-_][A-Za-z0-9\-_\.]*\@([A-Za-z0-9\-]+\.)+[A-Za-z]+		{ return MAIL_URL; }
"www."[A-Za-z0-9/\,\.\?\&:\;\(\)\-_\+\%=~\#\']+			{ return HTTP_URL; }
"ftp."[A-Za-z0-9/\,\.:\;\-_\+~\']+				{ return FTP_URL; }
<INITIAL,x_word>"&quot;"					{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>"&amp;"						{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>"&lt;"						{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>"&gt;"						{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>"\t"						{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>" "							{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>[\:\.\,\ \(\)]					{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>"<a href=\""[^"]+"\">"			{ BEGIN(x_link); return TXT; }
<INITIAL,x_word>"<"						{ BEGIN(x_tag); return TXT; }
<x_tag>">"							{ BEGIN(INITIAL); return TXT; }
<x_tag>.							{ return TXT; }
<x_link>"</a>"						{ BEGIN(INITIAL); return TXT; }
<x_link>.							{ return TXT; }
<INITIAL,x_word>[\xC0-\xDF][\x80-\xBF]				{ BEGIN(x_word); return TXT; }
<INITIAL,x_word>[\xE0-\xEF][\x00-\xFF]{2}			{ BEGIN(x_word); return TXT; }
<INITIAL,x_word>[\xF0-\xF7][\x00-\xFF]{3}			{ BEGIN(x_word); return TXT; }
<INITIAL,x_word>[\xF8-\xFB][\x00-\xFF]{4}			{ BEGIN(x_word); return TXT; }
<INITIAL,x_word>[\xFC-\xFD][\x00-\xFF]{5}			{ BEGIN(x_word); return TXT; }
<INITIAL,x_word>"\n"						{ BEGIN(INITIAL); return TXT; }
<INITIAL,x_word>.						{ BEGIN(x_word); return TXT; }
%%

int yywrap() { return 1; }

QString NavigatePlugin::parseUrl(const QString &text)
{
    QCString str = text.utf8();
    YY_BUFFER_STATE yy_current_buffer = yy_scan_string(str);
    yy_start = 1;	/* == BEGIN(INITIAL) - go to initial state since yy_start
                       is static and can have an old invalid value */
    QString res;
    int r;
    while ((r = yylex())) {;
		if (r == TXT){
			res += QString::fromUtf8(yytext);
			continue;
		}
		QString url  = yytext;
		QString link = SIM::unquoteString(QString::fromUtf8(yytext));
        switch (r){
        case MAIL_URL:
            if (link.left(7) != "mailto:")
                link = QString("mailto:") + link;
            break;
        case HTTP_URL:
			link = QString("http://") + link;
			break;
        case FTP_URL:
			link = QString("ftp://") + link;
			break;
		}
		res += "<a href=\"";
		res += link;
		res += "\"><u>";
		res += url;
		res += "</u></a>";
    };
    yy_delete_buffer(yy_current_buffer);
    return res;
}