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.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
33 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
45 * Convert HTML to plain text.
47 * inputmsg = pointer to raw HTML message
48 * screenwidth = desired output screenwidth
49 * do_citaformat = set to 1 to indent newlines with spaces
51 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
58 size_t outptr_buffer_size;
59 size_t output_len = 0;
60 int i, j, ch, did_out, rb, scanch;
61 int nest = 0; /* Bracket nesting level */
62 int blockquote = 0; /* BLOCKQUOTE nesting level */
63 int styletag = 0; /* STYLE tag nesting level */
64 int styletag_start = 0;
65 int bytes_processed = 0;
72 if (msglen == 0) msglen = strlen(inputmsg);
74 outptr_buffer_size = strlen(inptr) + SIZ;
75 outptr = malloc(outptr_buffer_size);
76 if (outptr == NULL) return NULL;
81 /* Fill the input buffer */
82 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
86 inbuf[strlen(inbuf)+1] = 0;
87 inbuf[strlen(inbuf)] = ch;
94 if (bytes_processed > msglen) {
100 /* Do some parsing */
101 if (strlen(inbuf)>0) {
103 /* Fold in all the spacing */
104 for (i=0; i<strlen(inbuf); ++i) {
105 if (inbuf[i]==10) inbuf[i]=32;
106 if (inbuf[i]==13) inbuf[i]=32;
107 if (inbuf[i]==9) inbuf[i]=32;
108 /*** we like foreign characters now.
109 if ((inbuf[i]<32) || (inbuf[i]>126)) {
113 for (i=0; i<strlen(inbuf); ++i) {
114 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
115 strcpy(&inbuf[i], &inbuf[i+1]);
118 for (i=0; i<strlen(inbuf); ++i) {
127 else if (ch == '>') { /* We have a tag. */
128 if (nest > 0) --nest;
130 /* Unqualify the tag (truncate at first space) */
131 if (strchr(tag, ' ') != NULL) {
132 strcpy(strchr(tag, ' '), "");
135 if (!strcasecmp(tag, "P")) {
140 if (!strcasecmp(tag, "/DIV")) {
145 if (!strcasecmp(tag, "LI")) {
147 strcat(outbuf, " * ");
150 else if (!strcasecmp(tag, "/UL")) {
155 else if (!strcasecmp(tag, "H1")) {
160 else if (!strcasecmp(tag, "H2")) {
165 else if (!strcasecmp(tag, "H3")) {
170 else if (!strcasecmp(tag, "H4")) {
175 else if (!strcasecmp(tag, "/H1")) {
179 else if (!strcasecmp(tag, "/H2")) {
183 else if (!strcasecmp(tag, "/H3")) {
187 else if (!strcasecmp(tag, "/H4")) {
191 else if (!strcasecmp(tag, "HR")) {
194 for (j=0; j<screenwidth-2; ++j)
199 else if (!strcasecmp(tag, "BR")) {
203 else if (!strcasecmp(tag, "TR")) {
207 else if (!strcasecmp(tag, "/TABLE")) {
211 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
214 for (j=0; j<blockquote; ++j) strcat(nl, ">");
218 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
219 strcat(outbuf, "\n");
222 for (j=0; j<blockquote; ++j) strcat(nl, ">");
226 else if (!strcasecmp(tag, "STYLE")) {
229 styletag_start = strlen(outbuf);
233 else if (!strcasecmp(tag, "/STYLE")) {
236 outbuf[styletag_start] = 0;
242 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
243 tag[strlen(tag)+1] = 0;
244 tag[strlen(tag)] = ch;
248 outbuf[strlen(outbuf)+1] = 0;
249 outbuf[strlen(outbuf)] = ch;
252 strcpy(inbuf, &inbuf[i]);
255 /* Convert &; tags to the forbidden characters */
256 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
258 /* Character entity references */
259 if (!strncasecmp(&outbuf[i], " ", 6)) {
261 strcpy(&outbuf[i+1], &outbuf[i+6]);
264 if (!strncasecmp(&outbuf[i], " ", 6)) {
266 strcpy(&outbuf[i+1], &outbuf[i+6]);
269 if (!strncasecmp(&outbuf[i], " ", 6)) {
271 strcpy(&outbuf[i+1], &outbuf[i+6]);
274 if (!strncasecmp(&outbuf[i], " ", 8)) {
276 strcpy(&outbuf[i+1], &outbuf[i+8]);
279 else if (!strncasecmp(&outbuf[i], "<", 4)) {
281 strcpy(&outbuf[i+1], &outbuf[i+4]);
284 else if (!strncasecmp(&outbuf[i], ">", 4)) {
286 strcpy(&outbuf[i+1], &outbuf[i+4]);
289 else if (!strncasecmp(&outbuf[i], "&", 5)) {
290 strcpy(&outbuf[i+1], &outbuf[i+5]);
293 else if (!strncasecmp(&outbuf[i], """, 6)) {
295 strcpy(&outbuf[i+1], &outbuf[i+6]);
298 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
300 strcpy(&outbuf[i+1], &outbuf[i+7]);
303 else if (!strncasecmp(&outbuf[i], "’", 7)) {
305 strcpy(&outbuf[i+1], &outbuf[i+7]);
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 /* two-digit decimal equivalents */
432 else if ((!strncmp(&outbuf[i], "&#", 2))
433 && (outbuf[i+4] == ';') ) {
435 sscanf(&outbuf[i+2], "%02d", &scanch);
437 strcpy(&outbuf[i+1], &outbuf[i+5]);
440 /* three-digit decimal equivalents */
441 else if ((!strncmp(&outbuf[i], "&#", 2))
442 && (outbuf[i+5] == ';') ) {
444 sscanf(&outbuf[i+2], "%03d", &scanch);
446 strcpy(&outbuf[i+1], &outbuf[i+6]);
451 /* Make sure the output buffer is big enough */
452 if ((output_len + strlen(outbuf) + SIZ)
453 > outptr_buffer_size) {
454 outptr_buffer_size += SIZ;
455 outptr = realloc(outptr, outptr_buffer_size);
458 /* Output any lines terminated with hard line breaks */
461 if (strlen(outbuf)>0) {
462 for (i = 0; i<strlen(outbuf); ++i) {
463 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
465 strncpy(&outptr[output_len],
470 strcpy(&outptr[output_len],
475 strcpy(outbuf, &outbuf[i+1]);
483 /* Add soft line breaks */
484 if (strlen(outbuf) > (screenwidth - 2 )) {
486 for (i=0; i<(screenwidth-2); ++i) {
487 if (outbuf[i]==32) rb = i;
490 strncpy(&outptr[output_len], outbuf, rb);
492 strcpy(&outptr[output_len], nl);
493 output_len += strlen(nl);
495 strcpy(&outptr[output_len], " ");
498 strcpy(outbuf, &outbuf[rb+1]);
500 strncpy(&outptr[output_len], outbuf,
502 output_len += (screenwidth-2);
503 strcpy(&outptr[output_len], nl);
504 output_len += strlen(nl);
506 strcpy(&outptr[output_len], " ");
509 strcpy(outbuf, &outbuf[screenwidth-2]);
513 } while (done_reading == 0);
515 strcpy(&outptr[output_len], outbuf);
516 output_len += strlen(outbuf);
518 /* Strip leading/trailing whitespace. We can't do this with
519 * striplt() because it uses too many strlen()'s
521 while ((output_len > 0) && (isspace(outptr[0]))) {
522 strcpy(outptr, &outptr[1]);
525 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
526 outptr[output_len-1] = 0;
530 if (outptr[output_len-1] != '\n') {
531 strcat(outptr, "\n");