1 // Functions which handle translation between HTML and plain text
2 // Copyright (c) 2000-2023 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 #include "libcitadel.h"
21 // Convert HTML to plain text.
23 // inputmsg = pointer to raw HTML message
24 // msglen = stop reading after this many bytes
25 // screenwidth = desired output screenwidth
26 // ansi = if nonzero, assume output is to a terminal that supports ANSI escape codes
28 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int ansi) {
36 size_t outptr_buffer_size;
37 size_t output_len = 0;
38 int i, j, ch, did_out, rb, scanch;
39 int nest = 0; // Bracket nesting level
40 int blockquote = 0; // BLOCKQUOTE nesting level
41 int styletag = 0; // STYLE tag nesting level
42 int styletag_start = 0;
43 int bytes_processed = 0;
51 if (msglen == 0) msglen = strlen(inputmsg);
53 outptr_buffer_size = strlen(inptr) + SIZ;
54 outptr = malloc(outptr_buffer_size);
55 if (outptr == NULL) return NULL;
60 // Fill the input buffer
61 inbuf_len = strlen(inbuf);
62 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
66 inbuf[inbuf_len++] = ch;
74 if (bytes_processed > msglen) {
81 if (!IsEmptyStr(inbuf)) {
83 // Fold in all the spacing
84 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
85 if (inbuf[i]==10) inbuf[i]=32;
86 if (inbuf[i]==13) inbuf[i]=32;
87 if (inbuf[i]==9) inbuf[i]=32;
89 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
90 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
91 strcpy(&inbuf[i], &inbuf[i+1]);
95 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
104 else if (ch == '>') { // We have a tag.
105 if (nest > 0) --nest;
107 // Unqualify the tag (truncate at first space)
108 if (strchr(tag, ' ') != NULL) {
109 strcpy(strchr(tag, ' '), "");
112 if (!strcasecmp(tag, "P")) {
117 if (!strcasecmp(tag, "/DIV")) {
122 if (!strcasecmp(tag, "LI")) {
124 strcat(outbuf, " * ");
127 else if (!strcasecmp(tag, "/UL")) {
132 else if (!strcasecmp(tag, "H1")) {
137 else if (!strcasecmp(tag, "H2")) {
142 else if (!strcasecmp(tag, "H3")) {
147 else if (!strcasecmp(tag, "H4")) {
152 else if (!strcasecmp(tag, "/H1")) {
156 else if (!strcasecmp(tag, "/H2")) {
160 else if (!strcasecmp(tag, "/H3")) {
164 else if (!strcasecmp(tag, "/H4")) {
168 else if (!strcasecmp(tag, "HR")) {
171 for (j=0; j<screenwidth-2; ++j)
177 (!strcasecmp(tag, "B"))
178 || (!strcasecmp(tag, "STRONG"))
181 strcat(outbuf, "\033[1m");
185 (!strcasecmp(tag, "/B"))
186 || (!strcasecmp(tag, "/STRONG"))
189 strcat(outbuf, "\033[22m");
194 (!strcasecmp(tag, "I"))
195 || (!strcasecmp(tag, "EM"))
198 strcat(outbuf, "\033[3m");
203 (!strcasecmp(tag, "/I"))
204 || (!strcasecmp(tag, "/EM"))
207 strcat(outbuf, "\033[23m");
211 else if (!strcasecmp(tag, "U")) {
213 strcat(outbuf, "\033[4m");
217 else if (!strcasecmp(tag, "/U")) {
219 strcat(outbuf, "\033[24m");
223 else if (!strcasecmp(tag, "BR")) {
227 else if (!strcasecmp(tag, "TR")) {
231 else if (!strcasecmp(tag, "/TABLE")) {
235 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
238 if ( (blockquote == 1) && (ansi) ) {
239 strcat(nl, "\033[2m\033[2m");
241 for (j=0; j<blockquote; ++j) strcat(nl, ">");
245 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
246 strcat(outbuf, "\n");
248 if ( (blockquote == 0) && (ansi) ) {
249 strcat(outbuf, "\033[22m\033[22m");
252 for (j=0; j<blockquote; ++j) strcat(nl, ">");
256 else if (!strcasecmp(tag, "STYLE")) {
259 styletag_start = strlen(outbuf);
263 else if (!strcasecmp(tag, "/STYLE")) {
266 outbuf[styletag_start] = 0;
272 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
273 tag[strlen(tag)+1] = 0;
274 tag[strlen(tag)] = ch;
277 else if ((!nest) && (styletag == 0)) {
278 outbuf[strlen(outbuf)+1] = 0;
279 outbuf[strlen(outbuf)] = ch;
282 strcpy(inbuf, &inbuf[i]);
285 // Convert &; tags to the forbidden characters
286 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
288 // Character entity references
289 if (!strncasecmp(&outbuf[i], " ", 6)) {
291 strcpy(&outbuf[i+1], &outbuf[i+6]);
294 if (!strncasecmp(&outbuf[i], " ", 6)) {
296 strcpy(&outbuf[i+1], &outbuf[i+6]);
299 if (!strncasecmp(&outbuf[i], " ", 6)) {
301 strcpy(&outbuf[i+1], &outbuf[i+6]);
304 if (!strncasecmp(&outbuf[i], " ", 8)) {
306 strcpy(&outbuf[i+1], &outbuf[i+8]);
309 else if (!strncasecmp(&outbuf[i], "<", 4)) {
311 strcpy(&outbuf[i+1], &outbuf[i+4]);
314 else if (!strncasecmp(&outbuf[i], ">", 4)) {
316 strcpy(&outbuf[i+1], &outbuf[i+4]);
319 else if (!strncasecmp(&outbuf[i], "&", 5)) {
320 strcpy(&outbuf[i+1], &outbuf[i+5]);
323 else if (!strncasecmp(&outbuf[i], """, 6)) {
325 strcpy(&outbuf[i+1], &outbuf[i+6]);
328 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
330 strcpy(&outbuf[i+1], &outbuf[i+7]);
333 else if (!strncasecmp(&outbuf[i], "’", 7)) {
335 strcpy(&outbuf[i+1], &outbuf[i+7]);
338 else if (!strncasecmp(&outbuf[i], "©", 6)) {
342 strcpy(&outbuf[i+3], &outbuf[i+6]);
345 else if (!strncasecmp(&outbuf[i], "•", 6)) {
349 strcpy(&outbuf[i+3], &outbuf[i+6]);
352 else if (!strncasecmp(&outbuf[i], "…", 8)) {
356 strcpy(&outbuf[i+3], &outbuf[i+8]);
359 else if (!strncasecmp(&outbuf[i], "™", 7)) {
364 strcpy(&outbuf[i+4], &outbuf[i+7]);
367 else if (!strncasecmp(&outbuf[i], "®", 5)) {
371 strcpy(&outbuf[i+3], &outbuf[i+5]);
374 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
378 strcpy(&outbuf[i+3], &outbuf[i+8]);
381 else if (!strncasecmp(&outbuf[i], "½", 8)) {
385 strcpy(&outbuf[i+3], &outbuf[i+8]);
388 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
392 strcpy(&outbuf[i+3], &outbuf[i+8]);
395 else if (!strncasecmp(&outbuf[i], "–", 7)) {
398 strcpy(&outbuf[i+2], &outbuf[i+7]);
401 else if (!strncasecmp(&outbuf[i], "—", 7)) {
405 strcpy(&outbuf[i+3], &outbuf[i+7]);
408 else if (!strncmp(&outbuf[i], "Ç", 8)) {
410 strcpy(&outbuf[i+1], &outbuf[i+8]);
413 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
415 strcpy(&outbuf[i+1], &outbuf[i+8]);
418 else if (!strncmp(&outbuf[i], "È", 8)) {
420 strcpy(&outbuf[i+1], &outbuf[i+8]);
423 else if (!strncasecmp(&outbuf[i], "è", 8)) {
425 strcpy(&outbuf[i+1], &outbuf[i+8]);
428 else if (!strncmp(&outbuf[i], "Ê", 7)) {
430 strcpy(&outbuf[i+1], &outbuf[i+7]);
433 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
435 strcpy(&outbuf[i+1], &outbuf[i+7]);
438 else if (!strncmp(&outbuf[i], "É", 8)) {
440 strcpy(&outbuf[i+1], &outbuf[i+8]);
443 else if (!strncasecmp(&outbuf[i], "é", 8)) {
445 strcpy(&outbuf[i+1], &outbuf[i+8]);
448 else if (!strncmp(&outbuf[i], "À", 8)) {
450 strcpy(&outbuf[i+1], &outbuf[i+8]);
453 else if (!strncasecmp(&outbuf[i], "à", 8)) {
455 strcpy(&outbuf[i+1], &outbuf[i+8]);
458 else if (!strncasecmp(&outbuf[i], "“", 7)) {
460 strcpy(&outbuf[i+1], &outbuf[i+7]);
463 else if (!strncasecmp(&outbuf[i], "”", 7)) {
465 strcpy(&outbuf[i+1], &outbuf[i+7]);
468 else if (!strncasecmp(&outbuf[i], "´", 7)) {
470 strcpy(&outbuf[i+1], &outbuf[i+7]);
473 else if (!strncasecmp(&outbuf[i], "’", 7)) {
475 strcpy(&outbuf[i+1], &outbuf[i+7]);
478 else if (!strncasecmp(&outbuf[i], "–", 7)) {
480 strcpy(&outbuf[i+1], &outbuf[i+7]);
483 // two-digit decimal equivalents
484 else if (outbuf[i] == '&' &&
485 outbuf[i + 1] == '#' &&
486 isdigit(outbuf[i + 2]) &&
487 isdigit(outbuf[i + 3]) &&
488 (outbuf[i+4] == ';') )
491 sscanf(&outbuf[i+2], "%02d", &scanch);
493 strcpy(&outbuf[i+1], &outbuf[i+5]);
496 // three-digit decimal equivalents
497 else if (outbuf[i] == '&' &&
498 outbuf[i + 1] == '#' &&
499 isdigit(outbuf[i + 2]) &&
500 isdigit(outbuf[i + 3]) &&
501 isdigit(outbuf[i + 4]) &&
502 (outbuf[i + 5] == ';') )
505 sscanf(&outbuf[i+2], "%03d", &scanch);
507 strcpy(&outbuf[i+1], &outbuf[i+6]);
510 // four-digit decimal equivalents
511 else if (outbuf[i] == '&' &&
512 outbuf[i + 1] == '#' &&
513 isdigit(outbuf[i + 2]) &&
514 isdigit(outbuf[i + 3]) &&
515 isdigit(outbuf[i + 4]) &&
516 isdigit(outbuf[i + 5]) &&
517 (outbuf[i + 6] == ';') )
520 sscanf(&outbuf[i+2], "%04d", &scanch);
522 strcpy(&outbuf[i+1], &outbuf[i+7]);
527 // Make sure the output buffer is big enough
528 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
529 outptr_buffer_size += SIZ;
530 outptr = realloc(outptr, outptr_buffer_size);
531 if (outptr == NULL) {
536 // Output any lines terminated with hard line breaks
539 if (strlen(outbuf) > 0) {
540 for (i = 0; i<strlen(outbuf); ++i) {
541 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
543 strncpy(&outptr[output_len], outbuf, i+1);
546 strcpy(outbuf, &outbuf[i+1]);
554 // Add soft line breaks
555 if (strlen(outbuf) > (screenwidth - 2 )) {
557 for (i=0; i<(screenwidth-2); ++i) {
558 if (outbuf[i]==32) rb = i;
561 strncpy(&outptr[output_len], outbuf, rb);
563 strcpy(&outptr[output_len], nl);
564 output_len += strlen(nl);
565 strcpy(outbuf, &outbuf[rb+1]);
568 strncpy(&outptr[output_len], outbuf, screenwidth-2);
569 output_len += (screenwidth-2);
570 strcpy(&outptr[output_len], nl);
571 output_len += strlen(nl);
572 strcpy(outbuf, &outbuf[screenwidth-2]);
576 } while (done_reading == 0);
578 strcpy(&outptr[output_len], outbuf);
579 output_len += strlen(outbuf);
581 // Strip leading/trailing whitespace.
582 while ((output_len > 0) && (isspace(outptr[0]))) {
583 strcpy(outptr, &outptr[1]);
586 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
587 outptr[output_len-1] = 0;
591 // Make sure the final line ends with a newline character.
592 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
593 strcat(outptr, "\n");