2 * $Id: html.c 6014 2008-02-04 18:38:35Z ajc $
4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2005 by Art Cancro and others. This program is
6 * released under the terms of the GNU General Public License.
13 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
31 #include "libcitadel.h"
35 * Convert HTML to plain text.
37 * inputmsg = pointer to raw HTML message
38 * screenwidth = desired output screenwidth
39 * do_citaformat = set to 1 to indent newlines with spaces
41 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
49 size_t outptr_buffer_size;
50 size_t output_len = 0;
51 int i, j, ch, did_out, rb, scanch;
52 int nest = 0; /* Bracket nesting level */
53 int blockquote = 0; /* BLOCKQUOTE nesting level */
54 int styletag = 0; /* STYLE tag nesting level */
55 int styletag_start = 0;
56 int bytes_processed = 0;
63 if (msglen == 0) msglen = strlen(inputmsg);
65 outptr_buffer_size = strlen(inptr) + SIZ;
66 outptr = malloc(outptr_buffer_size);
67 if (outptr == NULL) return NULL;
72 /* Fill the input buffer */
73 inbuf_len = strlen(inbuf);
74 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
78 inbuf[inbuf_len++] = ch;
86 if (bytes_processed > msglen) {
93 if (!IsEmptyStr(inbuf)) {
96 /* Fold in all the spacing */
97 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
98 if (inbuf[i]==10) inbuf[i]=32;
99 if (inbuf[i]==13) inbuf[i]=32;
100 if (inbuf[i]==9) inbuf[i]=32;
101 /*** we like foreign characters now.
102 if ((inbuf[i]<32) || (inbuf[i]>126)) {
106 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
107 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
108 strcpy(&inbuf[i], &inbuf[i+1]);
111 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
120 else if (ch == '>') { /* We have a tag. */
121 if (nest > 0) --nest;
123 /* Unqualify the tag (truncate at first space) */
124 if (strchr(tag, ' ') != NULL) {
125 strcpy(strchr(tag, ' '), "");
128 if (!strcasecmp(tag, "P")) {
133 if (!strcasecmp(tag, "/DIV")) {
138 if (!strcasecmp(tag, "LI")) {
140 strcat(outbuf, " * ");
143 else if (!strcasecmp(tag, "/UL")) {
148 else if (!strcasecmp(tag, "H1")) {
153 else if (!strcasecmp(tag, "H2")) {
158 else if (!strcasecmp(tag, "H3")) {
163 else if (!strcasecmp(tag, "H4")) {
168 else if (!strcasecmp(tag, "/H1")) {
172 else if (!strcasecmp(tag, "/H2")) {
176 else if (!strcasecmp(tag, "/H3")) {
180 else if (!strcasecmp(tag, "/H4")) {
184 else if (!strcasecmp(tag, "HR")) {
187 for (j=0; j<screenwidth-2; ++j)
192 else if (!strcasecmp(tag, "BR")) {
196 else if (!strcasecmp(tag, "TR")) {
200 else if (!strcasecmp(tag, "/TABLE")) {
204 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
207 for (j=0; j<blockquote; ++j) strcat(nl, ">");
211 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
212 strcat(outbuf, "\n");
215 for (j=0; j<blockquote; ++j) strcat(nl, ">");
219 else if (!strcasecmp(tag, "STYLE")) {
222 styletag_start = strlen(outbuf);
226 else if (!strcasecmp(tag, "/STYLE")) {
229 outbuf[styletag_start] = 0;
235 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
236 tag[strlen(tag)+1] = 0;
237 tag[strlen(tag)] = ch;
241 outbuf[strlen(outbuf)+1] = 0;
242 outbuf[strlen(outbuf)] = ch;
245 strcpy(inbuf, &inbuf[i]);
248 /* Convert &; tags to the forbidden characters */
249 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
251 /* Character entity references */
252 if (!strncasecmp(&outbuf[i], " ", 6)) {
254 strcpy(&outbuf[i+1], &outbuf[i+6]);
257 if (!strncasecmp(&outbuf[i], " ", 6)) {
259 strcpy(&outbuf[i+1], &outbuf[i+6]);
262 if (!strncasecmp(&outbuf[i], " ", 6)) {
264 strcpy(&outbuf[i+1], &outbuf[i+6]);
267 if (!strncasecmp(&outbuf[i], " ", 8)) {
269 strcpy(&outbuf[i+1], &outbuf[i+8]);
272 else if (!strncasecmp(&outbuf[i], "<", 4)) {
274 strcpy(&outbuf[i+1], &outbuf[i+4]);
277 else if (!strncasecmp(&outbuf[i], ">", 4)) {
279 strcpy(&outbuf[i+1], &outbuf[i+4]);
282 else if (!strncasecmp(&outbuf[i], "&", 5)) {
283 strcpy(&outbuf[i+1], &outbuf[i+5]);
286 else if (!strncasecmp(&outbuf[i], """, 6)) {
288 strcpy(&outbuf[i+1], &outbuf[i+6]);
291 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
293 strcpy(&outbuf[i+1], &outbuf[i+7]);
296 else if (!strncasecmp(&outbuf[i], "’", 7)) {
298 strcpy(&outbuf[i+1], &outbuf[i+7]);
301 else if (!strncasecmp(&outbuf[i], "©", 6)) {
305 strcpy(&outbuf[i+3], &outbuf[i+6]);
308 else if (!strncasecmp(&outbuf[i], "•", 6)) {
312 strcpy(&outbuf[i+3], &outbuf[i+6]);
315 else if (!strncasecmp(&outbuf[i], "…", 8)) {
319 strcpy(&outbuf[i+3], &outbuf[i+8]);
322 else if (!strncasecmp(&outbuf[i], "™", 7)) {
327 strcpy(&outbuf[i+4], &outbuf[i+7]);
330 else if (!strncasecmp(&outbuf[i], "®", 5)) {
334 strcpy(&outbuf[i+3], &outbuf[i+5]);
337 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
341 strcpy(&outbuf[i+3], &outbuf[i+8]);
344 else if (!strncasecmp(&outbuf[i], "½", 8)) {
348 strcpy(&outbuf[i+3], &outbuf[i+8]);
351 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
355 strcpy(&outbuf[i+3], &outbuf[i+8]);
358 else if (!strncasecmp(&outbuf[i], "–", 7)) {
361 strcpy(&outbuf[i+2], &outbuf[i+7]);
364 else if (!strncasecmp(&outbuf[i], "—", 7)) {
368 strcpy(&outbuf[i+3], &outbuf[i+7]);
371 else if (!strncmp(&outbuf[i], "Ç", 8)) {
373 strcpy(&outbuf[i+1], &outbuf[i+8]);
376 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
378 strcpy(&outbuf[i+1], &outbuf[i+8]);
381 else if (!strncmp(&outbuf[i], "È", 8)) {
383 strcpy(&outbuf[i+1], &outbuf[i+8]);
386 else if (!strncasecmp(&outbuf[i], "è", 8)) {
388 strcpy(&outbuf[i+1], &outbuf[i+8]);
391 else if (!strncmp(&outbuf[i], "Ê", 7)) {
393 strcpy(&outbuf[i+1], &outbuf[i+7]);
396 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
398 strcpy(&outbuf[i+1], &outbuf[i+7]);
401 else if (!strncmp(&outbuf[i], "É", 8)) {
403 strcpy(&outbuf[i+1], &outbuf[i+8]);
406 else if (!strncasecmp(&outbuf[i], "é", 8)) {
408 strcpy(&outbuf[i+1], &outbuf[i+8]);
411 else if (!strncmp(&outbuf[i], "À", 8)) {
413 strcpy(&outbuf[i+1], &outbuf[i+8]);
416 else if (!strncasecmp(&outbuf[i], "à", 8)) {
418 strcpy(&outbuf[i+1], &outbuf[i+8]);
421 else if (!strncasecmp(&outbuf[i], "“", 7)) {
423 strcpy(&outbuf[i+1], &outbuf[i+7]);
426 else if (!strncasecmp(&outbuf[i], "”", 7)) {
428 strcpy(&outbuf[i+1], &outbuf[i+7]);
431 else if (!strncasecmp(&outbuf[i], "´", 7)) {
433 strcpy(&outbuf[i+1], &outbuf[i+7]);
436 /* two-digit decimal equivalents */
437 else if ((!strncmp(&outbuf[i], "&#", 2))
438 && (outbuf[i+4] == ';') ) {
440 sscanf(&outbuf[i+2], "%02d", &scanch);
442 strcpy(&outbuf[i+1], &outbuf[i+5]);
445 /* three-digit decimal equivalents */
446 else if ((!strncmp(&outbuf[i], "&#", 2))
447 && (outbuf[i+5] == ';') ) {
449 sscanf(&outbuf[i+2], "%03d", &scanch);
451 strcpy(&outbuf[i+1], &outbuf[i+6]);
456 /* Make sure the output buffer is big enough */
457 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
458 outptr_buffer_size += SIZ;
459 outptr = realloc(outptr, outptr_buffer_size);
460 if (outptr == NULL) {
465 /* Output any lines terminated with hard line breaks */
468 if (strlen(outbuf) > 0) {
469 for (i = 0; i<strlen(outbuf); ++i) {
470 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
472 strncpy(&outptr[output_len], outbuf, i+1);
476 strcpy(&outptr[output_len], " ");
480 strcpy(outbuf, &outbuf[i+1]);
488 /* Add soft line breaks */
489 if (strlen(outbuf) > (screenwidth - 2 )) {
491 for (i=0; i<(screenwidth-2); ++i) {
492 if (outbuf[i]==32) rb = i;
495 strncpy(&outptr[output_len], outbuf, rb);
497 strcpy(&outptr[output_len], nl);
498 output_len += strlen(nl);
500 strcpy(&outptr[output_len], " ");
503 strcpy(outbuf, &outbuf[rb+1]);
505 strncpy(&outptr[output_len], outbuf,
507 output_len += (screenwidth-2);
508 strcpy(&outptr[output_len], nl);
509 output_len += strlen(nl);
511 strcpy(&outptr[output_len], " ");
514 strcpy(outbuf, &outbuf[screenwidth-2]);
518 } while (done_reading == 0);
520 strcpy(&outptr[output_len], outbuf);
521 output_len += strlen(outbuf);
523 /* Strip leading/trailing whitespace. We can't do this with
524 * striplt() because it uses too many strlen()'s
526 while ((output_len > 0) && (isspace(outptr[0]))) {
527 strcpy(outptr, &outptr[1]);
530 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
531 outptr[output_len-1] = 0;
535 if (outptr[output_len-1] != '\n') {
536 strcat(outptr, "\n");