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; i<strlen(inbuf); ++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; i<strlen(inbuf); ++i) {
113 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
114 strcpy(&inbuf[i], &inbuf[i+1]);
117 for (i=0; i<strlen(inbuf); ++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; i<strlen(outbuf); ++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], "…", 8)) {
318 strcpy(&outbuf[i+3], &outbuf[i+8]);
321 else if (!strncasecmp(&outbuf[i], "™", 7)) {
326 strcpy(&outbuf[i+4], &outbuf[i+7]);
329 else if (!strncasecmp(&outbuf[i], "®", 5)) {
333 strcpy(&outbuf[i+3], &outbuf[i+5]);
336 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
340 strcpy(&outbuf[i+3], &outbuf[i+8]);
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], "–", 7)) {
360 strcpy(&outbuf[i+2], &outbuf[i+7]);
363 else if (!strncasecmp(&outbuf[i], "—", 7)) {
367 strcpy(&outbuf[i+3], &outbuf[i+7]);
370 else if (!strncmp(&outbuf[i], "Ç", 8)) {
372 strcpy(&outbuf[i+1], &outbuf[i+8]);
375 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
377 strcpy(&outbuf[i+1], &outbuf[i+8]);
380 else if (!strncmp(&outbuf[i], "È", 8)) {
382 strcpy(&outbuf[i+1], &outbuf[i+8]);
385 else if (!strncasecmp(&outbuf[i], "è", 8)) {
387 strcpy(&outbuf[i+1], &outbuf[i+8]);
390 else if (!strncmp(&outbuf[i], "Ê", 7)) {
392 strcpy(&outbuf[i+1], &outbuf[i+7]);
395 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
397 strcpy(&outbuf[i+1], &outbuf[i+7]);
400 else if (!strncmp(&outbuf[i], "É", 8)) {
402 strcpy(&outbuf[i+1], &outbuf[i+8]);
405 else if (!strncasecmp(&outbuf[i], "é", 8)) {
407 strcpy(&outbuf[i+1], &outbuf[i+8]);
410 else if (!strncmp(&outbuf[i], "À", 8)) {
412 strcpy(&outbuf[i+1], &outbuf[i+8]);
415 else if (!strncasecmp(&outbuf[i], "à", 8)) {
417 strcpy(&outbuf[i+1], &outbuf[i+8]);
420 else if (!strncasecmp(&outbuf[i], "“", 7)) {
422 strcpy(&outbuf[i+1], &outbuf[i+7]);
425 else if (!strncasecmp(&outbuf[i], "”", 7)) {
427 strcpy(&outbuf[i+1], &outbuf[i+7]);
430 /* two-digit decimal equivalents */
431 else if ((!strncmp(&outbuf[i], "&#", 2))
432 && (outbuf[i+4] == ';') ) {
434 sscanf(&outbuf[i+2], "%02d", &scanch);
436 strcpy(&outbuf[i+1], &outbuf[i+5]);
439 /* three-digit decimal equivalents */
440 else if ((!strncmp(&outbuf[i], "&#", 2))
441 && (outbuf[i+5] == ';') ) {
443 sscanf(&outbuf[i+2], "%03d", &scanch);
445 strcpy(&outbuf[i+1], &outbuf[i+6]);
450 /* Make sure the output buffer is big enough */
451 if ((output_len + strlen(outbuf) + SIZ)
452 > outptr_buffer_size) {
453 outptr_buffer_size += SIZ;
454 outptr = realloc(outptr, outptr_buffer_size);
457 /* Output any lines terminated with hard line breaks */
460 if (!IsEmptyStr(outbuf)) {
461 for (i = 0; i<strlen(outbuf); ++i) {
462 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
464 strncpy(&outptr[output_len],
469 strcpy(&outptr[output_len],
474 strcpy(outbuf, &outbuf[i+1]);
482 /* Add soft line breaks */
483 if (strlen(outbuf) > (screenwidth - 2 )) {
485 for (i=0; i<(screenwidth-2); ++i) {
486 if (outbuf[i]==32) rb = i;
489 strncpy(&outptr[output_len], outbuf, rb);
491 strcpy(&outptr[output_len], nl);
492 output_len += strlen(nl);
494 strcpy(&outptr[output_len], " ");
497 strcpy(outbuf, &outbuf[rb+1]);
499 strncpy(&outptr[output_len], outbuf,
501 output_len += (screenwidth-2);
502 strcpy(&outptr[output_len], nl);
503 output_len += strlen(nl);
505 strcpy(&outptr[output_len], " ");
508 strcpy(outbuf, &outbuf[screenwidth-2]);
512 } while (done_reading == 0);
514 strcpy(&outptr[output_len], outbuf);
515 output_len += strlen(outbuf);
517 /* Strip leading/trailing whitespace. We can't do this with
518 * striplt() because it uses too many strlen()'s
520 while ((output_len > 0) && (isspace(outptr[0]))) {
521 strcpy(outptr, &outptr[1]);
524 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
525 outptr[output_len-1] = 0;
529 if (outptr[output_len-1] != '\n') {
530 strcat(outptr, "\n");