2 * nohtml.c -- (X)HTML helper functions
4 * Copyright (C) 2008,2009 Mikael Berthe <mikael@lilotux.net>
5 * Some portions come from the jabberd project, see below.
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.
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.
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
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.
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.
39 char *html_strip(const char *htmlbuf
)
44 if (!htmlbuf
) return(NULL
);
46 nohtml
= g_strdup(htmlbuf
);
48 if (!strchr(htmlbuf
, '&') && !strchr(htmlbuf
, '<'))
51 for (i
= 0; i
< (int)strlen(htmlbuf
); i
++) {
52 if (htmlbuf
[i
] == '&') {
53 if (!strncmp(&htmlbuf
[i
],"&",5)) {
56 } else if (!strncmp(&htmlbuf
[i
],""", 6)) {
59 } else if (!strncmp(&htmlbuf
[i
],"'", 6)) {
62 } else if (!strncmp(&htmlbuf
[i
],"<", 4)) {
65 } else if (!strncmp(&htmlbuf
[i
],">", 4)) {
69 } else if (!strncmp(&htmlbuf
[i
],"<br>", 4) ||
70 !strncmp(&htmlbuf
[i
],"<br/>", 5)) {
72 i
+= (htmlbuf
[i
+3] == '/' ? 4 : 3);
73 } else if (htmlbuf
[i
] == '<') {
74 /* Let's strip all unknown tags */
76 while (htmlbuf
[++i
] != '>');
78 nohtml
[j
] = htmlbuf
[i
];
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.
90 char *html_escape(const char *text
)
96 if (!text
) return(NULL
);
98 oldlen
= newlen
= strlen(text
);
100 for (i
= 0; i
< oldlen
; i
++) {
123 if (oldlen
== newlen
)
124 return g_strdup(text
);
126 html
= g_new0(char, newlen
+1);
128 for (i
= j
= 0; i
< oldlen
; i
++) {
132 memcpy(&html
[j
], "&", 5);
136 memcpy(&html
[j
], "'", 6);
140 memcpy(&html
[j
], """, 6);
144 memcpy(&html
[j
], "<", 4);
148 memcpy(&html
[j
], ">", 4);
152 memcpy(&html
[j
], "<br/>", 5);
162 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */