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>
31 #include <libcitadel.h>
35 #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) {
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 inbuf_len = strlen(inbuf);
83 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
87 inbuf[inbuf_len++] = ch;
95 if (bytes_processed > msglen) {
101 /* Do some parsing */
102 if (!IsEmptyStr(inbuf)) {
105 /* Fold in all the spacing */
106 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
107 if (inbuf[i]==10) inbuf[i]=32;
108 if (inbuf[i]==13) inbuf[i]=32;
109 if (inbuf[i]==9) inbuf[i]=32;
110 /*** we like foreign characters now.
111 if ((inbuf[i]<32) || (inbuf[i]>126)) {
115 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
116 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
117 strcpy(&inbuf[i], &inbuf[i+1]);
120 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
129 else if (ch == '>') { /* We have a tag. */
130 if (nest > 0) --nest;
132 /* Unqualify the tag (truncate at first space) */
133 if (strchr(tag, ' ') != NULL) {
134 strcpy(strchr(tag, ' '), "");
137 if (!strcasecmp(tag, "P")) {
142 if (!strcasecmp(tag, "/DIV")) {
147 if (!strcasecmp(tag, "LI")) {
149 strcat(outbuf, " * ");
152 else if (!strcasecmp(tag, "/UL")) {
157 else if (!strcasecmp(tag, "H1")) {
162 else if (!strcasecmp(tag, "H2")) {
167 else if (!strcasecmp(tag, "H3")) {
172 else if (!strcasecmp(tag, "H4")) {
177 else if (!strcasecmp(tag, "/H1")) {
181 else if (!strcasecmp(tag, "/H2")) {
185 else if (!strcasecmp(tag, "/H3")) {
189 else if (!strcasecmp(tag, "/H4")) {
193 else if (!strcasecmp(tag, "HR")) {
196 for (j=0; j<screenwidth-2; ++j)
201 else if (!strcasecmp(tag, "BR")) {
205 else if (!strcasecmp(tag, "TR")) {
209 else if (!strcasecmp(tag, "/TABLE")) {
213 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
216 for (j=0; j<blockquote; ++j) strcat(nl, ">");
220 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
221 strcat(outbuf, "\n");
224 for (j=0; j<blockquote; ++j) strcat(nl, ">");
228 else if (!strcasecmp(tag, "STYLE")) {
231 styletag_start = strlen(outbuf);
235 else if (!strcasecmp(tag, "/STYLE")) {
238 outbuf[styletag_start] = 0;
244 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
245 tag[strlen(tag)+1] = 0;
246 tag[strlen(tag)] = ch;
250 outbuf[strlen(outbuf)+1] = 0;
251 outbuf[strlen(outbuf)] = ch;
254 strcpy(inbuf, &inbuf[i]);
257 /* Convert &; tags to the forbidden characters */
258 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
260 /* Character entity references */
261 if (!strncasecmp(&outbuf[i], " ", 6)) {
263 strcpy(&outbuf[i+1], &outbuf[i+6]);
266 if (!strncasecmp(&outbuf[i], " ", 6)) {
268 strcpy(&outbuf[i+1], &outbuf[i+6]);
271 if (!strncasecmp(&outbuf[i], " ", 6)) {
273 strcpy(&outbuf[i+1], &outbuf[i+6]);
276 if (!strncasecmp(&outbuf[i], " ", 8)) {
278 strcpy(&outbuf[i+1], &outbuf[i+8]);
281 else if (!strncasecmp(&outbuf[i], "<", 4)) {
283 strcpy(&outbuf[i+1], &outbuf[i+4]);
286 else if (!strncasecmp(&outbuf[i], ">", 4)) {
288 strcpy(&outbuf[i+1], &outbuf[i+4]);
291 else if (!strncasecmp(&outbuf[i], "&", 5)) {
292 strcpy(&outbuf[i+1], &outbuf[i+5]);
295 else if (!strncasecmp(&outbuf[i], """, 6)) {
297 strcpy(&outbuf[i+1], &outbuf[i+6]);
300 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
302 strcpy(&outbuf[i+1], &outbuf[i+7]);
305 else if (!strncasecmp(&outbuf[i], "’", 7)) {
307 strcpy(&outbuf[i+1], &outbuf[i+7]);
310 else if (!strncasecmp(&outbuf[i], "©", 6)) {
314 strcpy(&outbuf[i+3], &outbuf[i+6]);
317 else if (!strncasecmp(&outbuf[i], "•", 6)) {
321 strcpy(&outbuf[i+3], &outbuf[i+6]);
324 else if (!strncasecmp(&outbuf[i], "…", 8)) {
328 strcpy(&outbuf[i+3], &outbuf[i+8]);
331 else if (!strncasecmp(&outbuf[i], "™", 7)) {
336 strcpy(&outbuf[i+4], &outbuf[i+7]);
339 else if (!strncasecmp(&outbuf[i], "®", 5)) {
343 strcpy(&outbuf[i+3], &outbuf[i+5]);
346 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
350 strcpy(&outbuf[i+3], &outbuf[i+8]);
353 else if (!strncasecmp(&outbuf[i], "½", 8)) {
357 strcpy(&outbuf[i+3], &outbuf[i+8]);
360 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
364 strcpy(&outbuf[i+3], &outbuf[i+8]);
367 else if (!strncasecmp(&outbuf[i], "–", 7)) {
370 strcpy(&outbuf[i+2], &outbuf[i+7]);
373 else if (!strncasecmp(&outbuf[i], "—", 7)) {
377 strcpy(&outbuf[i+3], &outbuf[i+7]);
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], "È", 8)) {
392 strcpy(&outbuf[i+1], &outbuf[i+8]);
395 else if (!strncasecmp(&outbuf[i], "è", 8)) {
397 strcpy(&outbuf[i+1], &outbuf[i+8]);
400 else if (!strncmp(&outbuf[i], "Ê", 7)) {
402 strcpy(&outbuf[i+1], &outbuf[i+7]);
405 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
407 strcpy(&outbuf[i+1], &outbuf[i+7]);
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 (!strncmp(&outbuf[i], "À", 8)) {
422 strcpy(&outbuf[i+1], &outbuf[i+8]);
425 else if (!strncasecmp(&outbuf[i], "à", 8)) {
427 strcpy(&outbuf[i+1], &outbuf[i+8]);
430 else if (!strncasecmp(&outbuf[i], "“", 7)) {
432 strcpy(&outbuf[i+1], &outbuf[i+7]);
435 else if (!strncasecmp(&outbuf[i], "”", 7)) {
437 strcpy(&outbuf[i+1], &outbuf[i+7]);
440 else if (!strncasecmp(&outbuf[i], "´", 7)) {
442 strcpy(&outbuf[i+1], &outbuf[i+7]);
445 /* two-digit decimal equivalents */
446 else if ((!strncmp(&outbuf[i], "&#", 2))
447 && (outbuf[i+4] == ';') ) {
449 sscanf(&outbuf[i+2], "%02d", &scanch);
451 strcpy(&outbuf[i+1], &outbuf[i+5]);
454 /* three-digit decimal equivalents */
455 else if ((!strncmp(&outbuf[i], "&#", 2))
456 && (outbuf[i+5] == ';') ) {
458 sscanf(&outbuf[i+2], "%03d", &scanch);
460 strcpy(&outbuf[i+1], &outbuf[i+6]);
465 /* Make sure the output buffer is big enough */
466 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
467 outptr_buffer_size += SIZ;
468 outptr = realloc(outptr, outptr_buffer_size);
469 if (outptr == NULL) {
474 /* Output any lines terminated with hard line breaks */
477 if (strlen(outbuf) > 0) {
478 for (i = 0; i<strlen(outbuf); ++i) {
479 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
481 strncpy(&outptr[output_len], outbuf, i+1);
485 strcpy(&outptr[output_len], " ");
489 strcpy(outbuf, &outbuf[i+1]);
497 /* Add soft line breaks */
498 if (strlen(outbuf) > (screenwidth - 2 )) {
500 for (i=0; i<(screenwidth-2); ++i) {
501 if (outbuf[i]==32) rb = i;
504 strncpy(&outptr[output_len], outbuf, rb);
506 strcpy(&outptr[output_len], nl);
507 output_len += strlen(nl);
509 strcpy(&outptr[output_len], " ");
512 strcpy(outbuf, &outbuf[rb+1]);
514 strncpy(&outptr[output_len], outbuf,
516 output_len += (screenwidth-2);
517 strcpy(&outptr[output_len], nl);
518 output_len += strlen(nl);
520 strcpy(&outptr[output_len], " ");
523 strcpy(outbuf, &outbuf[screenwidth-2]);
527 } while (done_reading == 0);
529 strcpy(&outptr[output_len], outbuf);
530 output_len += strlen(outbuf);
532 /* Strip leading/trailing whitespace. We can't do this with
533 * striplt() because it uses too many strlen()'s
535 while ((output_len > 0) && (isspace(outptr[0]))) {
536 strcpy(outptr, &outptr[1]);
539 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
540 outptr[output_len-1] = 0;
544 if (outptr[output_len-1] != '\n') {
545 strcat(outptr, "\n");