1 // Functions which handle translation between HTML and plain text
2 // Copyright (c) 2000-2022 by the citadel.org team
4 // This program is open source software. Use, duplication, or disclosure
5 // is subject to the terms of the GNU General Public License, version 3.
11 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
29 #include "libcitadel.h"
32 // Convert HTML to plain text.
34 // inputmsg = pointer to raw HTML message
35 // msglen = stop reading after this many bytes
36 // screenwidth = desired output screenwidth
37 // ansi = if nonzero, assume output is to a terminal that supports ANSI escape codes
39 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int ansi) {
47 size_t outptr_buffer_size;
48 size_t output_len = 0;
49 int i, j, ch, did_out, rb, scanch;
50 int nest = 0; // Bracket nesting level
51 int blockquote = 0; // BLOCKQUOTE nesting level
52 int styletag = 0; // STYLE tag nesting level
53 int styletag_start = 0;
54 int bytes_processed = 0;
62 if (msglen == 0) msglen = strlen(inputmsg);
64 outptr_buffer_size = strlen(inptr) + SIZ;
65 outptr = malloc(outptr_buffer_size);
66 if (outptr == NULL) return NULL;
71 /* Fill the input buffer */
72 inbuf_len = strlen(inbuf);
73 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
77 inbuf[inbuf_len++] = ch;
85 if (bytes_processed > msglen) {
92 if (!IsEmptyStr(inbuf)) {
95 /* Fold in all the spacing */
96 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
97 if (inbuf[i]==10) inbuf[i]=32;
98 if (inbuf[i]==13) inbuf[i]=32;
99 if (inbuf[i]==9) inbuf[i]=32;
101 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
102 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
103 strcpy(&inbuf[i], &inbuf[i+1]);
107 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
116 else if (ch == '>') { /* We have a tag. */
117 if (nest > 0) --nest;
119 /* Unqualify the tag (truncate at first space) */
120 if (strchr(tag, ' ') != NULL) {
121 strcpy(strchr(tag, ' '), "");
124 if (!strcasecmp(tag, "P")) {
129 if (!strcasecmp(tag, "/DIV")) {
134 if (!strcasecmp(tag, "LI")) {
136 strcat(outbuf, " * ");
139 else if (!strcasecmp(tag, "/UL")) {
144 else if (!strcasecmp(tag, "H1")) {
149 else if (!strcasecmp(tag, "H2")) {
154 else if (!strcasecmp(tag, "H3")) {
159 else if (!strcasecmp(tag, "H4")) {
164 else if (!strcasecmp(tag, "/H1")) {
168 else if (!strcasecmp(tag, "/H2")) {
172 else if (!strcasecmp(tag, "/H3")) {
176 else if (!strcasecmp(tag, "/H4")) {
180 else if (!strcasecmp(tag, "HR")) {
183 for (j=0; j<screenwidth-2; ++j)
189 These seemed like a good idea at the time, but it just makes a mess.
192 (!strcasecmp(tag, "B"))
193 || (!strcasecmp(tag, "/B"))
194 || (!strcasecmp(tag, "STRONG"))
195 || (!strcasecmp(tag, "/STRONG"))
201 (!strcasecmp(tag, "I"))
202 || (!strcasecmp(tag, "/I"))
203 || (!strcasecmp(tag, "EM"))
204 || (!strcasecmp(tag, "/EM"))
210 (!strcasecmp(tag, "U"))
211 || (!strcasecmp(tag, "/U"))
217 else if (!strcasecmp(tag, "BR")) {
221 else if (!strcasecmp(tag, "TR")) {
225 else if (!strcasecmp(tag, "/TABLE")) {
229 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
232 if ( (blockquote == 1) && (ansi) ) {
233 strcat(nl, "\033[2m\033[3m");
235 for (j=0; j<blockquote; ++j) strcat(nl, ">");
239 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
240 strcat(outbuf, "\n");
242 if ( (blockquote == 0) && (ansi) ) {
243 strcat(outbuf, "\033[22m\033[23m");
246 for (j=0; j<blockquote; ++j) strcat(nl, ">");
250 else if (!strcasecmp(tag, "STYLE")) {
253 styletag_start = strlen(outbuf);
257 else if (!strcasecmp(tag, "/STYLE")) {
260 outbuf[styletag_start] = 0;
266 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
267 tag[strlen(tag)+1] = 0;
268 tag[strlen(tag)] = ch;
271 else if ((!nest) && (styletag == 0)) {
272 outbuf[strlen(outbuf)+1] = 0;
273 outbuf[strlen(outbuf)] = ch;
276 strcpy(inbuf, &inbuf[i]);
279 /* Convert &; tags to the forbidden characters */
280 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
282 /* Character entity references */
283 if (!strncasecmp(&outbuf[i], " ", 6)) {
285 strcpy(&outbuf[i+1], &outbuf[i+6]);
288 if (!strncasecmp(&outbuf[i], " ", 6)) {
290 strcpy(&outbuf[i+1], &outbuf[i+6]);
293 if (!strncasecmp(&outbuf[i], " ", 6)) {
295 strcpy(&outbuf[i+1], &outbuf[i+6]);
298 if (!strncasecmp(&outbuf[i], " ", 8)) {
300 strcpy(&outbuf[i+1], &outbuf[i+8]);
303 else if (!strncasecmp(&outbuf[i], "<", 4)) {
305 strcpy(&outbuf[i+1], &outbuf[i+4]);
308 else if (!strncasecmp(&outbuf[i], ">", 4)) {
310 strcpy(&outbuf[i+1], &outbuf[i+4]);
313 else if (!strncasecmp(&outbuf[i], "&", 5)) {
314 strcpy(&outbuf[i+1], &outbuf[i+5]);
317 else if (!strncasecmp(&outbuf[i], """, 6)) {
319 strcpy(&outbuf[i+1], &outbuf[i+6]);
322 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
324 strcpy(&outbuf[i+1], &outbuf[i+7]);
327 else if (!strncasecmp(&outbuf[i], "’", 7)) {
329 strcpy(&outbuf[i+1], &outbuf[i+7]);
332 else if (!strncasecmp(&outbuf[i], "©", 6)) {
336 strcpy(&outbuf[i+3], &outbuf[i+6]);
339 else if (!strncasecmp(&outbuf[i], "•", 6)) {
343 strcpy(&outbuf[i+3], &outbuf[i+6]);
346 else if (!strncasecmp(&outbuf[i], "…", 8)) {
350 strcpy(&outbuf[i+3], &outbuf[i+8]);
353 else if (!strncasecmp(&outbuf[i], "™", 7)) {
358 strcpy(&outbuf[i+4], &outbuf[i+7]);
361 else if (!strncasecmp(&outbuf[i], "®", 5)) {
365 strcpy(&outbuf[i+3], &outbuf[i+5]);
368 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
372 strcpy(&outbuf[i+3], &outbuf[i+8]);
375 else if (!strncasecmp(&outbuf[i], "½", 8)) {
379 strcpy(&outbuf[i+3], &outbuf[i+8]);
382 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
386 strcpy(&outbuf[i+3], &outbuf[i+8]);
389 else if (!strncasecmp(&outbuf[i], "–", 7)) {
392 strcpy(&outbuf[i+2], &outbuf[i+7]);
395 else if (!strncasecmp(&outbuf[i], "—", 7)) {
399 strcpy(&outbuf[i+3], &outbuf[i+7]);
402 else if (!strncmp(&outbuf[i], "Ç", 8)) {
404 strcpy(&outbuf[i+1], &outbuf[i+8]);
407 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
409 strcpy(&outbuf[i+1], &outbuf[i+8]);
412 else if (!strncmp(&outbuf[i], "È", 8)) {
414 strcpy(&outbuf[i+1], &outbuf[i+8]);
417 else if (!strncasecmp(&outbuf[i], "è", 8)) {
419 strcpy(&outbuf[i+1], &outbuf[i+8]);
422 else if (!strncmp(&outbuf[i], "Ê", 7)) {
424 strcpy(&outbuf[i+1], &outbuf[i+7]);
427 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
429 strcpy(&outbuf[i+1], &outbuf[i+7]);
432 else if (!strncmp(&outbuf[i], "É", 8)) {
434 strcpy(&outbuf[i+1], &outbuf[i+8]);
437 else if (!strncasecmp(&outbuf[i], "é", 8)) {
439 strcpy(&outbuf[i+1], &outbuf[i+8]);
442 else if (!strncmp(&outbuf[i], "À", 8)) {
444 strcpy(&outbuf[i+1], &outbuf[i+8]);
447 else if (!strncasecmp(&outbuf[i], "à", 8)) {
449 strcpy(&outbuf[i+1], &outbuf[i+8]);
452 else if (!strncasecmp(&outbuf[i], "“", 7)) {
454 strcpy(&outbuf[i+1], &outbuf[i+7]);
457 else if (!strncasecmp(&outbuf[i], "”", 7)) {
459 strcpy(&outbuf[i+1], &outbuf[i+7]);
462 else if (!strncasecmp(&outbuf[i], "´", 7)) {
464 strcpy(&outbuf[i+1], &outbuf[i+7]);
467 else if (!strncasecmp(&outbuf[i], "’", 7)) {
469 strcpy(&outbuf[i+1], &outbuf[i+7]);
472 else if (!strncasecmp(&outbuf[i], "–", 7)) {
474 strcpy(&outbuf[i+1], &outbuf[i+7]);
477 /* two-digit decimal equivalents */
478 else if (outbuf[i] == '&' &&
479 outbuf[i + 1] == '#' &&
480 isdigit(outbuf[i + 2]) &&
481 isdigit(outbuf[i + 3]) &&
482 (outbuf[i+4] == ';') )
485 sscanf(&outbuf[i+2], "%02d", &scanch);
487 strcpy(&outbuf[i+1], &outbuf[i+5]);
490 /* three-digit decimal equivalents */
491 else if (outbuf[i] == '&' &&
492 outbuf[i + 1] == '#' &&
493 isdigit(outbuf[i + 2]) &&
494 isdigit(outbuf[i + 3]) &&
495 isdigit(outbuf[i + 4]) &&
496 (outbuf[i + 5] == ';') )
499 sscanf(&outbuf[i+2], "%03d", &scanch);
501 strcpy(&outbuf[i+1], &outbuf[i+6]);
504 /* four-digit decimal equivalents */
505 else if (outbuf[i] == '&' &&
506 outbuf[i + 1] == '#' &&
507 isdigit(outbuf[i + 2]) &&
508 isdigit(outbuf[i + 3]) &&
509 isdigit(outbuf[i + 4]) &&
510 isdigit(outbuf[i + 5]) &&
511 (outbuf[i + 6] == ';') )
514 sscanf(&outbuf[i+2], "%04d", &scanch);
516 strcpy(&outbuf[i+1], &outbuf[i+7]);
521 /* Make sure the output buffer is big enough */
522 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
523 outptr_buffer_size += SIZ;
524 outptr = realloc(outptr, outptr_buffer_size);
525 if (outptr == NULL) {
530 /* Output any lines terminated with hard line breaks */
533 if (strlen(outbuf) > 0) {
534 for (i = 0; i<strlen(outbuf); ++i) {
535 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
537 strncpy(&outptr[output_len], outbuf, i+1);
540 strcpy(outbuf, &outbuf[i+1]);
548 /* Add soft line breaks */
549 if (strlen(outbuf) > (screenwidth - 2 )) {
551 for (i=0; i<(screenwidth-2); ++i) {
552 if (outbuf[i]==32) rb = i;
555 strncpy(&outptr[output_len], outbuf, rb);
557 strcpy(&outptr[output_len], nl);
558 output_len += strlen(nl);
559 strcpy(outbuf, &outbuf[rb+1]);
561 strncpy(&outptr[output_len], outbuf,
563 output_len += (screenwidth-2);
564 strcpy(&outptr[output_len], nl);
565 output_len += strlen(nl);
566 strcpy(outbuf, &outbuf[screenwidth-2]);
570 } while (done_reading == 0);
572 strcpy(&outptr[output_len], outbuf);
573 output_len += strlen(outbuf);
575 /* Strip leading/trailing whitespace. We can't do this with
576 * striplt() because it uses too many strlen()'s
578 while ((output_len > 0) && (isspace(outptr[0]))) {
579 strcpy(outptr, &outptr[1]);
582 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
583 outptr[output_len-1] = 0;
587 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
588 strcat(outptr, "\n");