|
1 /* |
|
2 * nohtml.c -- (X)HTML helper functions |
|
3 * |
|
4 * Copyright (C) 2008 Mikael Berthe <mikael@lilotux.net> |
|
5 * Some portions come from the jabberd project, see below. |
|
6 * |
|
7 * This program is free software; you can redistribute it and/or modify |
|
8 * it under the terms of the GNU General Public License as published by |
|
9 * the Free Software Foundation; either version 2 of the License, or (at |
|
10 * your option) any later version. |
|
11 * |
|
12 * This program is distributed in the hope that it will be useful, but |
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 * General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License |
|
18 * along with this program; if not, write to the Free Software |
|
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
20 * USA |
|
21 * |
|
22 * |
|
23 * Some parts come from libjabber/str.c: |
|
24 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact |
|
25 * information for Jabber.com, Inc. is available at http://www.jabber.com/. |
|
26 * Portions Copyright (c) 1998-1999 Jeremie Miller. |
|
27 */ |
|
28 |
|
29 #include <string.h> |
|
30 #include <glib.h> |
|
31 #include <config.h> |
|
32 |
|
33 |
|
34 /* html_strip(htmlbuf) |
|
35 * Remove html entities from htmlbuf and try to convert it to plain text. |
|
36 * The caller must g_free the string after use. |
|
37 * Code mostly derived from strunescape(), in libjabber. |
|
38 */ |
|
39 char *html_strip(const char *htmlbuf) |
|
40 { |
|
41 int i, j=0; |
|
42 char *nohtml; |
|
43 |
|
44 if (!htmlbuf) return(NULL); |
|
45 |
|
46 nohtml = g_strdup(htmlbuf); |
|
47 |
|
48 if (!strchr(htmlbuf, '&') && !strchr(htmlbuf, '<')) |
|
49 return(nohtml); |
|
50 |
|
51 for (i = 0; i < (int)strlen(htmlbuf); i++) { |
|
52 if (htmlbuf[i] == '&') { |
|
53 if (!strncmp(&htmlbuf[i],"&",5)) { |
|
54 nohtml[j] = '&'; |
|
55 i += 4; |
|
56 } else if (!strncmp(&htmlbuf[i],""", 6)) { |
|
57 nohtml[j] = '\"'; |
|
58 i += 5; |
|
59 } else if (!strncmp(&htmlbuf[i],"'", 6)) { |
|
60 nohtml[j] = '\''; |
|
61 i += 5; |
|
62 } else if (!strncmp(&htmlbuf[i],"<", 4)) { |
|
63 nohtml[j] = '<'; |
|
64 i += 3; |
|
65 } else if (!strncmp(&htmlbuf[i],">", 4)) { |
|
66 nohtml[j] = '>'; |
|
67 i += 3; |
|
68 } |
|
69 } else if (!strncmp(&htmlbuf[i],"<br>", 4) || |
|
70 !strncmp(&htmlbuf[i],"<br/>", 5)) { |
|
71 nohtml[j] = '\n'; |
|
72 i += (htmlbuf[i+3] == '/' ? 4 : 3); |
|
73 } else if (htmlbuf[i] == '<') { |
|
74 /* Let's strip all unknown tags */ |
|
75 j--; |
|
76 while (htmlbuf[++i] != '>'); |
|
77 } else |
|
78 nohtml[j] = htmlbuf[i]; |
|
79 j++; |
|
80 } |
|
81 nohtml[j] = '\0'; |
|
82 return nohtml; |
|
83 } |
|
84 |
|
85 /* html_escape(text) |
|
86 * Add (x)html entities to the text. |
|
87 * The caller must g_free the string after use. |
|
88 * Code mostly derived from strescape(), in libjabber. |
|
89 */ |
|
90 char *html_escape(const char *text) |
|
91 { |
|
92 int i, j; |
|
93 int oldlen, newlen; |
|
94 char *html; |
|
95 |
|
96 if (!text) return(NULL); |
|
97 |
|
98 oldlen = newlen = strlen(text); |
|
99 |
|
100 for (i = 0; i < oldlen; i++) { |
|
101 switch(text[i]) |
|
102 { |
|
103 case '&': |
|
104 newlen += 5; |
|
105 break; |
|
106 case '\'': |
|
107 newlen += 6; |
|
108 break; |
|
109 case '\"': |
|
110 newlen += 6; |
|
111 break; |
|
112 case '<': |
|
113 newlen += 4; |
|
114 break; |
|
115 case '>': |
|
116 newlen += 4; |
|
117 break; |
|
118 case '\n': |
|
119 newlen += 5; |
|
120 } |
|
121 } |
|
122 |
|
123 if (oldlen == newlen) |
|
124 return g_strdup(text); |
|
125 |
|
126 html = g_new0(char, newlen+1); |
|
127 |
|
128 for (i = j = 0; i < oldlen; i++) { |
|
129 switch(text[i]) |
|
130 { |
|
131 case '&': |
|
132 memcpy(&html[j], "&", 5); |
|
133 j += 5; |
|
134 break; |
|
135 case '\'': |
|
136 memcpy(&html[j], "'", 6); |
|
137 j += 6; |
|
138 break; |
|
139 case '\"': |
|
140 memcpy(&html[j], """, 6); |
|
141 j += 6; |
|
142 break; |
|
143 case '<': |
|
144 memcpy(&html[j], "<", 4); |
|
145 j += 4; |
|
146 break; |
|
147 case '>': |
|
148 memcpy(&html[j], ">", 4); |
|
149 j += 4; |
|
150 break; |
|
151 case '\n': |
|
152 memcpy(&html[j], "<br/>", 5); |
|
153 j += 5; |
|
154 break; |
|
155 default: |
|
156 html[j++] = text[i]; |
|
157 } |
|
158 } |
|
159 return html; |
|
160 } |
|
161 |
|
162 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |