2 * Copyright (C) 2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include "empathy-string-parser.h"
26 #include "empathy-smiley-manager.h"
27 #include "empathy-ui-utils.h"
29 #define SCHEMES "([a-zA-Z\\+]+)"
30 #define INVALID_CHARS "\\s\"<>"
31 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\](){},;:"
32 #define INVALID_CHARS_FULL INVALID_CHARS_EXT "?'"
33 #define BODY "([^"INVALID_CHARS_FULL"])([^"INVALID_CHARS_EXT"]*)"
34 #define BODY_END "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_FULL".]"
35 #define URI_REGEX "("SCHEMES"://"BODY_END")" \
36 "|((www|ftp)\\."BODY_END")" \
37 "|((mailto:)?"BODY"@"BODY"\\."BODY_END")"
40 uri_regex_dup_singleton (void)
42 static GRegex *uri_regex = NULL;
44 /* We intentionally leak the regex so it's not recomputed */
48 uri_regex = g_regex_new (URI_REGEX, 0, 0, &error);
49 if (uri_regex == NULL) {
50 g_warning ("Failed to create reg exp: %s", error->message);
56 return g_regex_ref (uri_regex);
60 empathy_string_parser_substr (const gchar *text,
62 EmpathyStringParser *parsers,
65 if (parsers != NULL && parsers[0].match_func != NULL) {
66 parsers[0].match_func (text, len,
67 parsers[0].replace_func, parsers + 1,
73 empathy_string_match_link (const gchar *text,
75 EmpathyStringReplace replace_func,
76 EmpathyStringParser *sub_parsers,
80 GMatchInfo *match_info;
84 uri_regex = uri_regex_dup_singleton ();
85 if (uri_regex == NULL) {
86 empathy_string_parser_substr (text, len, sub_parsers, user_data);
90 match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
95 g_match_info_fetch_pos (match_info, 0, &s, &e);
98 /* Append the text between last link (or the
99 * start of the message) and this link */
100 empathy_string_parser_substr (text + last,
106 replace_func (text + s, e - s, NULL, user_data);
109 } while (g_match_info_next (match_info, NULL));
112 empathy_string_parser_substr (text + last, len - last,
113 sub_parsers, user_data);
115 g_match_info_free (match_info);
116 g_regex_unref (uri_regex);
120 empathy_string_match_smiley (const gchar *text,
122 EmpathyStringReplace replace_func,
123 EmpathyStringParser *sub_parsers,
127 EmpathySmileyManager *smiley_manager;
130 smiley_manager = empathy_smiley_manager_dup_singleton ();
131 hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
133 for (l = hits; l; l = l->next) {
134 EmpathySmileyHit *hit = l->data;
136 if (hit->start > last) {
137 /* Append the text between last smiley (or the
138 * start of the message) and this smiley */
139 empathy_string_parser_substr (text + last,
141 sub_parsers, user_data);
144 replace_func (text + hit->start, hit->end - hit->start,
149 empathy_smiley_hit_free (hit);
152 g_object_unref (smiley_manager);
154 empathy_string_parser_substr (text + last, len - last,
155 sub_parsers, user_data);
159 empathy_string_match_all (const gchar *text,
161 EmpathyStringReplace replace_func,
162 EmpathyStringParser *sub_parsers,
165 replace_func (text, len, NULL, user_data);
169 empathy_string_replace_link (const gchar *text,
174 GString *string = user_data;
179 real_url = empathy_make_absolute_url_len (text, len);
181 /* Need to copy manually, because g_markup_printf_escaped does not work
182 * with string precision pitfalls. */
183 title = g_strndup (text, len);
185 /* Append the link inside <a href=""></a> tag */
186 markup = g_markup_printf_escaped ("<a href=\"%s\">%s</a>",
189 g_string_append (string, markup);
197 empathy_string_replace_escaped (const gchar *text,
202 GString *string = user_data;
205 gsize escaped_len, old_len;
207 escaped = g_markup_escape_text (text, len);
208 escaped_len = strlen (escaped);
210 /* Allocate more space to string (we really need a g_string_extend...) */
211 old_len = string->len;
212 g_string_set_size (string, old_len + escaped_len);
213 g_string_truncate (string, old_len);
216 for (i = 0; i < escaped_len; i++) {
217 if (escaped[i] != '\r')
218 g_string_append_c (string, escaped[i]);
225 empathy_add_link_markup (const gchar *text)
227 EmpathyStringParser parsers[] = {
228 {empathy_string_match_link, empathy_string_replace_link},
229 {empathy_string_match_all, empathy_string_replace_escaped},
234 g_return_val_if_fail (text != NULL, NULL);
236 string = g_string_sized_new (strlen (text));
237 empathy_string_parser_substr (text, -1, parsers, string);
239 return g_string_free (string, FALSE);