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>
34 #include "sysdep_decls.h"
44 * Convert HTML to plain text.
46 * inputmsg = pointer to raw HTML message
47 * screenwidth = desired output screenwidth
48 * do_citaformat = set to 1 to indent newlines with spaces
50 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
57 size_t outptr_buffer_size;
58 size_t output_len = 0;
59 int i, j, ch, did_out, rb, scanch;
60 int nest = 0; /* Bracket nesting level */
61 int blockquote = 0; /* BLOCKQUOTE nesting level */
62 int styletag = 0; /* STYLE tag nesting level */
63 int styletag_start = 0;
64 int bytes_processed = 0;
71 if (msglen == 0) msglen = strlen(inputmsg);
73 outptr_buffer_size = strlen(inptr) + SIZ;
74 outptr = malloc(outptr_buffer_size);
75 if (outptr == NULL) return NULL;
80 /* Fill the input buffer */
81 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
85 inbuf[strlen(inbuf)+1] = 0;
86 inbuf[strlen(inbuf)] = ch;
93 if (bytes_processed > msglen) {
100 if (!IsEmptyStr(inbuf)) {
102 /* Fold in all the spacing */
103 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
104 if (inbuf[i]==10) inbuf[i]=32;
105 if (inbuf[i]==13) inbuf[i]=32;
106 if (inbuf[i]==9) inbuf[i]=32;
107 /*** we like foreign characters now.
108 if ((inbuf[i]<32) || (inbuf[i]>126)) {
112 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
113 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
114 strcpy(&inbuf[i], &inbuf[i+1]);
117 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
126 else if (ch == '>') { /* We have a tag. */
127 if (nest > 0) --nest;
129 /* Unqualify the tag (truncate at first space) */
130 if (strchr(tag, ' ') != NULL) {
131 strcpy(strchr(tag, ' '), "");
134 if (!strcasecmp(tag, "P")) {
139 if (!strcasecmp(tag, "/DIV")) {
144 if (!strcasecmp(tag, "LI")) {
146 strcat(outbuf, " * ");
149 else if (!strcasecmp(tag, "/UL")) {
154 else if (!strcasecmp(tag, "H1")) {
159 else if (!strcasecmp(tag, "H2")) {
164 else if (!strcasecmp(tag, "H3")) {
169 else if (!strcasecmp(tag, "H4")) {
174 else if (!strcasecmp(tag, "/H1")) {
178 else if (!strcasecmp(tag, "/H2")) {
182 else if (!strcasecmp(tag, "/H3")) {
186 else if (!strcasecmp(tag, "/H4")) {
190 else if (!strcasecmp(tag, "HR")) {
193 for (j=0; j<screenwidth-2; ++j)
198 else if (!strcasecmp(tag, "BR")) {
202 else if (!strcasecmp(tag, "TR")) {
206 else if (!strcasecmp(tag, "/TABLE")) {
210 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
213 for (j=0; j<blockquote; ++j) strcat(nl, ">");
217 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
218 strcat(outbuf, "\n");
221 for (j=0; j<blockquote; ++j) strcat(nl, ">");
225 else if (!strcasecmp(tag, "STYLE")) {
228 styletag_start = strlen(outbuf);
232 else if (!strcasecmp(tag, "/STYLE")) {
235 outbuf[styletag_start] = 0;
241 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
242 tag[strlen(tag)+1] = 0;
243 tag[strlen(tag)] = ch;
247 outbuf[strlen(outbuf)+1] = 0;
248 outbuf[strlen(outbuf)] = ch;
251 strcpy(inbuf, &inbuf[i]);
254 /* Convert &; tags to the forbidden characters */
255 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
257 /* Character entity references */
258 if (!strncasecmp(&outbuf[i], " ", 6)) {
260 strcpy(&outbuf[i+1], &outbuf[i+6]);
263 if (!strncasecmp(&outbuf[i], " ", 6)) {
265 strcpy(&outbuf[i+1], &outbuf[i+6]);
268 if (!strncasecmp(&outbuf[i], " ", 6)) {
270 strcpy(&outbuf[i+1], &outbuf[i+6]);
273 if (!strncasecmp(&outbuf[i], " ", 8)) {
275 strcpy(&outbuf[i+1], &outbuf[i+8]);
278 else if (!strncasecmp(&outbuf[i], "<", 4)) {
280 strcpy(&outbuf[i+1], &outbuf[i+4]);
283 else if (!strncasecmp(&outbuf[i], ">", 4)) {
285 strcpy(&outbuf[i+1], &outbuf[i+4]);
288 else if (!strncasecmp(&outbuf[i], "&", 5)) {
289 strcpy(&outbuf[i+1], &outbuf[i+5]);
292 else if (!strncasecmp(&outbuf[i], """, 6)) {
294 strcpy(&outbuf[i+1], &outbuf[i+6]);
297 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
299 strcpy(&outbuf[i+1], &outbuf[i+7]);
302 else if (!strncasecmp(&outbuf[i], "’", 7)) {
304 strcpy(&outbuf[i+1], &outbuf[i+7]);
307 else if (!strncasecmp(&outbuf[i], "©", 6)) {
311 strcpy(&outbuf[i+3], &outbuf[i+6]);
314 else if (!strncasecmp(&outbuf[i], "•", 6)) {
318 strcpy(&outbuf[i+3], &outbuf[i+6]);
321 else if (!strncasecmp(&outbuf[i], "…", 8)) {
325 strcpy(&outbuf[i+3], &outbuf[i+8]);
328 else if (!strncasecmp(&outbuf[i], "™", 7)) {
333 strcpy(&outbuf[i+4], &outbuf[i+7]);
336 else if (!strncasecmp(&outbuf[i], "®", 5)) {
340 strcpy(&outbuf[i+3], &outbuf[i+5]);
343 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
347 strcpy(&outbuf[i+3], &outbuf[i+8]);
350 else if (!strncasecmp(&outbuf[i], "½", 8)) {
354 strcpy(&outbuf[i+3], &outbuf[i+8]);
357 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
361 strcpy(&outbuf[i+3], &outbuf[i+8]);
364 else if (!strncasecmp(&outbuf[i], "–", 7)) {
367 strcpy(&outbuf[i+2], &outbuf[i+7]);
370 else if (!strncasecmp(&outbuf[i], "—", 7)) {
374 strcpy(&outbuf[i+3], &outbuf[i+7]);
377 else if (!strncmp(&outbuf[i], "Ç", 8)) {
379 strcpy(&outbuf[i+1], &outbuf[i+8]);
382 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
384 strcpy(&outbuf[i+1], &outbuf[i+8]);
387 else if (!strncmp(&outbuf[i], "È", 8)) {
389 strcpy(&outbuf[i+1], &outbuf[i+8]);
392 else if (!strncasecmp(&outbuf[i], "è", 8)) {
394 strcpy(&outbuf[i+1], &outbuf[i+8]);
397 else if (!strncmp(&outbuf[i], "Ê", 7)) {
399 strcpy(&outbuf[i+1], &outbuf[i+7]);
402 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
404 strcpy(&outbuf[i+1], &outbuf[i+7]);
407 else if (!strncmp(&outbuf[i], "É", 8)) {
409 strcpy(&outbuf[i+1], &outbuf[i+8]);
412 else if (!strncasecmp(&outbuf[i], "é", 8)) {
414 strcpy(&outbuf[i+1], &outbuf[i+8]);
417 else if (!strncmp(&outbuf[i], "À", 8)) {
419 strcpy(&outbuf[i+1], &outbuf[i+8]);
422 else if (!strncasecmp(&outbuf[i], "à", 8)) {
424 strcpy(&outbuf[i+1], &outbuf[i+8]);
427 else if (!strncasecmp(&outbuf[i], "“", 7)) {
429 strcpy(&outbuf[i+1], &outbuf[i+7]);
432 else if (!strncasecmp(&outbuf[i], "”", 7)) {
434 strcpy(&outbuf[i+1], &outbuf[i+7]);
437 /* two-digit decimal equivalents */
438 else if ((!strncmp(&outbuf[i], "&#", 2))
439 && (outbuf[i+4] == ';') ) {
441 sscanf(&outbuf[i+2], "%02d", &scanch);
443 strcpy(&outbuf[i+1], &outbuf[i+5]);
446 /* three-digit decimal equivalents */
447 else if ((!strncmp(&outbuf[i], "&#", 2))
448 && (outbuf[i+5] == ';') ) {
450 sscanf(&outbuf[i+2], "%03d", &scanch);
452 strcpy(&outbuf[i+1], &outbuf[i+6]);
457 /* Make sure the output buffer is big enough */
458 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
459 outptr_buffer_size += SIZ;
460 outptr = realloc(outptr, outptr_buffer_size);
461 if (outptr == NULL) {
466 /* Output any lines terminated with hard line breaks */
469 if (strlen(outbuf) > 0) {
470 for (i = 0; i<strlen(outbuf); ++i) {
471 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
473 strncpy(&outptr[output_len], outbuf, i+1);
477 strcpy(&outptr[output_len], " ");
481 strcpy(outbuf, &outbuf[i+1]);
489 /* Add soft line breaks */
490 if (strlen(outbuf) > (screenwidth - 2 )) {
492 for (i=0; i<(screenwidth-2); ++i) {
493 if (outbuf[i]==32) rb = i;
496 strncpy(&outptr[output_len], outbuf, rb);
498 strcpy(&outptr[output_len], nl);
499 output_len += strlen(nl);
501 strcpy(&outptr[output_len], " ");
504 strcpy(outbuf, &outbuf[rb+1]);
506 strncpy(&outptr[output_len], outbuf,
508 output_len += (screenwidth-2);
509 strcpy(&outptr[output_len], nl);
510 output_len += strlen(nl);
512 strcpy(&outptr[output_len], " ");
515 strcpy(outbuf, &outbuf[screenwidth-2]);
519 } while (done_reading == 0);
521 strcpy(&outptr[output_len], outbuf);
522 output_len += strlen(outbuf);
524 /* Strip leading/trailing whitespace. We can't do this with
525 * striplt() because it uses too many strlen()'s
527 while ((output_len > 0) && (isspace(outptr[0]))) {
528 strcpy(outptr, &outptr[1]);
531 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
532 outptr[output_len-1] = 0;
536 if (outptr[output_len-1] != '\n') {
537 strcat(outptr, "\n");