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 (!strcasecmp(tag, "B"))
190 || (!strcasecmp(tag, "STRONG"))
193 strcat(outbuf, "\033[1m");
197 (!strcasecmp(tag, "/B"))
198 || (!strcasecmp(tag, "/STRONG"))
201 strcat(outbuf, "\033[22m");
206 (!strcasecmp(tag, "I"))
207 || (!strcasecmp(tag, "EM"))
210 strcat(outbuf, "\033[3m");
215 (!strcasecmp(tag, "/I"))
216 || (!strcasecmp(tag, "/EM"))
219 strcat(outbuf, "\033[23m");
223 else if (!strcasecmp(tag, "U")) {
225 strcat(outbuf, "\033[4m");
229 else if (!strcasecmp(tag, "/U")) {
231 strcat(outbuf, "\033[24m");
235 else if (!strcasecmp(tag, "BR")) {
239 else if (!strcasecmp(tag, "TR")) {
243 else if (!strcasecmp(tag, "/TABLE")) {
247 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
250 if ( (blockquote == 1) && (ansi) ) {
251 strcat(nl, "\033[2m\033[3m");
253 for (j=0; j<blockquote; ++j) strcat(nl, ">");
257 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
258 strcat(outbuf, "\n");
260 if ( (blockquote == 0) && (ansi) ) {
261 strcat(outbuf, "\033[22m\033[23m");
264 for (j=0; j<blockquote; ++j) strcat(nl, ">");
268 else if (!strcasecmp(tag, "STYLE")) {
271 styletag_start = strlen(outbuf);
275 else if (!strcasecmp(tag, "/STYLE")) {
278 outbuf[styletag_start] = 0;
284 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
285 tag[strlen(tag)+1] = 0;
286 tag[strlen(tag)] = ch;
289 else if ((!nest) && (styletag == 0)) {
290 outbuf[strlen(outbuf)+1] = 0;
291 outbuf[strlen(outbuf)] = ch;
294 strcpy(inbuf, &inbuf[i]);
297 /* Convert &; tags to the forbidden characters */
298 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
300 /* Character entity references */
301 if (!strncasecmp(&outbuf[i], " ", 6)) {
303 strcpy(&outbuf[i+1], &outbuf[i+6]);
306 if (!strncasecmp(&outbuf[i], " ", 6)) {
308 strcpy(&outbuf[i+1], &outbuf[i+6]);
311 if (!strncasecmp(&outbuf[i], " ", 6)) {
313 strcpy(&outbuf[i+1], &outbuf[i+6]);
316 if (!strncasecmp(&outbuf[i], " ", 8)) {
318 strcpy(&outbuf[i+1], &outbuf[i+8]);
321 else if (!strncasecmp(&outbuf[i], "<", 4)) {
323 strcpy(&outbuf[i+1], &outbuf[i+4]);
326 else if (!strncasecmp(&outbuf[i], ">", 4)) {
328 strcpy(&outbuf[i+1], &outbuf[i+4]);
331 else if (!strncasecmp(&outbuf[i], "&", 5)) {
332 strcpy(&outbuf[i+1], &outbuf[i+5]);
335 else if (!strncasecmp(&outbuf[i], """, 6)) {
337 strcpy(&outbuf[i+1], &outbuf[i+6]);
340 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
342 strcpy(&outbuf[i+1], &outbuf[i+7]);
345 else if (!strncasecmp(&outbuf[i], "’", 7)) {
347 strcpy(&outbuf[i+1], &outbuf[i+7]);
350 else if (!strncasecmp(&outbuf[i], "©", 6)) {
354 strcpy(&outbuf[i+3], &outbuf[i+6]);
357 else if (!strncasecmp(&outbuf[i], "•", 6)) {
361 strcpy(&outbuf[i+3], &outbuf[i+6]);
364 else if (!strncasecmp(&outbuf[i], "…", 8)) {
368 strcpy(&outbuf[i+3], &outbuf[i+8]);
371 else if (!strncasecmp(&outbuf[i], "™", 7)) {
376 strcpy(&outbuf[i+4], &outbuf[i+7]);
379 else if (!strncasecmp(&outbuf[i], "®", 5)) {
383 strcpy(&outbuf[i+3], &outbuf[i+5]);
386 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
390 strcpy(&outbuf[i+3], &outbuf[i+8]);
393 else if (!strncasecmp(&outbuf[i], "½", 8)) {
397 strcpy(&outbuf[i+3], &outbuf[i+8]);
400 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
404 strcpy(&outbuf[i+3], &outbuf[i+8]);
407 else if (!strncasecmp(&outbuf[i], "–", 7)) {
410 strcpy(&outbuf[i+2], &outbuf[i+7]);
413 else if (!strncasecmp(&outbuf[i], "—", 7)) {
417 strcpy(&outbuf[i+3], &outbuf[i+7]);
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 (!strncmp(&outbuf[i], "È", 8)) {
432 strcpy(&outbuf[i+1], &outbuf[i+8]);
435 else if (!strncasecmp(&outbuf[i], "è", 8)) {
437 strcpy(&outbuf[i+1], &outbuf[i+8]);
440 else if (!strncmp(&outbuf[i], "Ê", 7)) {
442 strcpy(&outbuf[i+1], &outbuf[i+7]);
445 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
447 strcpy(&outbuf[i+1], &outbuf[i+7]);
450 else if (!strncmp(&outbuf[i], "É", 8)) {
452 strcpy(&outbuf[i+1], &outbuf[i+8]);
455 else if (!strncasecmp(&outbuf[i], "é", 8)) {
457 strcpy(&outbuf[i+1], &outbuf[i+8]);
460 else if (!strncmp(&outbuf[i], "À", 8)) {
462 strcpy(&outbuf[i+1], &outbuf[i+8]);
465 else if (!strncasecmp(&outbuf[i], "à", 8)) {
467 strcpy(&outbuf[i+1], &outbuf[i+8]);
470 else if (!strncasecmp(&outbuf[i], "“", 7)) {
472 strcpy(&outbuf[i+1], &outbuf[i+7]);
475 else if (!strncasecmp(&outbuf[i], "”", 7)) {
477 strcpy(&outbuf[i+1], &outbuf[i+7]);
480 else if (!strncasecmp(&outbuf[i], "´", 7)) {
482 strcpy(&outbuf[i+1], &outbuf[i+7]);
485 else if (!strncasecmp(&outbuf[i], "’", 7)) {
487 strcpy(&outbuf[i+1], &outbuf[i+7]);
490 else if (!strncasecmp(&outbuf[i], "–", 7)) {
492 strcpy(&outbuf[i+1], &outbuf[i+7]);
495 /* two-digit decimal equivalents */
496 else if (outbuf[i] == '&' &&
497 outbuf[i + 1] == '#' &&
498 isdigit(outbuf[i + 2]) &&
499 isdigit(outbuf[i + 3]) &&
500 (outbuf[i+4] == ';') )
503 sscanf(&outbuf[i+2], "%02d", &scanch);
505 strcpy(&outbuf[i+1], &outbuf[i+5]);
508 /* three-digit decimal equivalents */
509 else if (outbuf[i] == '&' &&
510 outbuf[i + 1] == '#' &&
511 isdigit(outbuf[i + 2]) &&
512 isdigit(outbuf[i + 3]) &&
513 isdigit(outbuf[i + 4]) &&
514 (outbuf[i + 5] == ';') )
517 sscanf(&outbuf[i+2], "%03d", &scanch);
519 strcpy(&outbuf[i+1], &outbuf[i+6]);
522 /* four-digit decimal equivalents */
523 else if (outbuf[i] == '&' &&
524 outbuf[i + 1] == '#' &&
525 isdigit(outbuf[i + 2]) &&
526 isdigit(outbuf[i + 3]) &&
527 isdigit(outbuf[i + 4]) &&
528 isdigit(outbuf[i + 5]) &&
529 (outbuf[i + 6] == ';') )
532 sscanf(&outbuf[i+2], "%04d", &scanch);
534 strcpy(&outbuf[i+1], &outbuf[i+7]);
539 /* Make sure the output buffer is big enough */
540 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
541 outptr_buffer_size += SIZ;
542 outptr = realloc(outptr, outptr_buffer_size);
543 if (outptr == NULL) {
548 /* Output any lines terminated with hard line breaks */
551 if (strlen(outbuf) > 0) {
552 for (i = 0; i<strlen(outbuf); ++i) {
553 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
555 strncpy(&outptr[output_len], outbuf, i+1);
558 strcpy(outbuf, &outbuf[i+1]);
566 /* Add soft line breaks */
567 if (strlen(outbuf) > (screenwidth - 2 )) {
569 for (i=0; i<(screenwidth-2); ++i) {
570 if (outbuf[i]==32) rb = i;
573 strncpy(&outptr[output_len], outbuf, rb);
575 strcpy(&outptr[output_len], nl);
576 output_len += strlen(nl);
577 strcpy(outbuf, &outbuf[rb+1]);
579 strncpy(&outptr[output_len], outbuf,
581 output_len += (screenwidth-2);
582 strcpy(&outptr[output_len], nl);
583 output_len += strlen(nl);
584 strcpy(outbuf, &outbuf[screenwidth-2]);
588 } while (done_reading == 0);
590 strcpy(&outptr[output_len], outbuf);
591 output_len += strlen(outbuf);
593 /* Strip leading/trailing whitespace. We can't do this with
594 * striplt() because it uses too many strlen()'s
596 while ((output_len > 0) && (isspace(outptr[0]))) {
597 strcpy(outptr, &outptr[1]);
600 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
601 outptr[output_len-1] = 0;
605 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
606 strcat(outptr, "\n");