2 * Functions which handle translation between HTML and plain text
3 * Copyright (c) 2000-2018 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
13 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
31 #include "libcitadel.h"
35 * Convert HTML to plain text.
37 * inputmsg = pointer to raw HTML message
38 * screenwidth = desired output screenwidth
40 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth) {
48 size_t outptr_buffer_size;
49 size_t output_len = 0;
50 int i, j, ch, did_out, rb, scanch;
51 int nest = 0; /* Bracket nesting level */
52 int blockquote = 0; /* BLOCKQUOTE nesting level */
53 int styletag = 0; /* STYLE tag nesting level */
54 int styletag_start = 0;
55 int bytes_processed = 0;
63 if (msglen == 0) msglen = strlen(inputmsg);
65 outptr_buffer_size = strlen(inptr) + SIZ;
66 outptr = malloc(outptr_buffer_size);
67 if (outptr == NULL) return NULL;
72 /* Fill the input buffer */
73 inbuf_len = strlen(inbuf);
74 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
78 inbuf[inbuf_len++] = ch;
86 if (bytes_processed > msglen) {
93 if (!IsEmptyStr(inbuf)) {
96 /* Fold in all the spacing */
97 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
98 if (inbuf[i]==10) inbuf[i]=32;
99 if (inbuf[i]==13) inbuf[i]=32;
100 if (inbuf[i]==9) inbuf[i]=32;
102 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
103 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
104 strcpy(&inbuf[i], &inbuf[i+1]);
108 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
117 else if (ch == '>') { /* We have a tag. */
118 if (nest > 0) --nest;
120 /* Unqualify the tag (truncate at first space) */
121 if (strchr(tag, ' ') != NULL) {
122 strcpy(strchr(tag, ' '), "");
125 if (!strcasecmp(tag, "P")) {
130 if (!strcasecmp(tag, "/DIV")) {
135 if (!strcasecmp(tag, "LI")) {
137 strcat(outbuf, " * ");
140 else if (!strcasecmp(tag, "/UL")) {
145 else if (!strcasecmp(tag, "H1")) {
150 else if (!strcasecmp(tag, "H2")) {
155 else if (!strcasecmp(tag, "H3")) {
160 else if (!strcasecmp(tag, "H4")) {
165 else if (!strcasecmp(tag, "/H1")) {
169 else if (!strcasecmp(tag, "/H2")) {
173 else if (!strcasecmp(tag, "/H3")) {
177 else if (!strcasecmp(tag, "/H4")) {
181 else if (!strcasecmp(tag, "HR")) {
184 for (j=0; j<screenwidth-2; ++j)
190 These seemed like a good idea at the time, but it just makes a mess.
193 (!strcasecmp(tag, "B"))
194 || (!strcasecmp(tag, "/B"))
195 || (!strcasecmp(tag, "STRONG"))
196 || (!strcasecmp(tag, "/STRONG"))
202 (!strcasecmp(tag, "I"))
203 || (!strcasecmp(tag, "/I"))
204 || (!strcasecmp(tag, "EM"))
205 || (!strcasecmp(tag, "/EM"))
211 (!strcasecmp(tag, "U"))
212 || (!strcasecmp(tag, "/U"))
218 else if (!strcasecmp(tag, "BR")) {
222 else if (!strcasecmp(tag, "TR")) {
226 else if (!strcasecmp(tag, "/TABLE")) {
230 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
233 for (j=0; j<blockquote; ++j) strcat(nl, ">");
237 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
238 strcat(outbuf, "\n");
241 for (j=0; j<blockquote; ++j) strcat(nl, ">");
245 else if (!strcasecmp(tag, "STYLE")) {
248 styletag_start = strlen(outbuf);
252 else if (!strcasecmp(tag, "/STYLE")) {
255 outbuf[styletag_start] = 0;
261 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
262 tag[strlen(tag)+1] = 0;
263 tag[strlen(tag)] = ch;
266 else if ((!nest) && (styletag == 0)) {
267 outbuf[strlen(outbuf)+1] = 0;
268 outbuf[strlen(outbuf)] = ch;
271 strcpy(inbuf, &inbuf[i]);
274 /* Convert &; tags to the forbidden characters */
275 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
277 /* Character entity references */
278 if (!strncasecmp(&outbuf[i], " ", 6)) {
280 strcpy(&outbuf[i+1], &outbuf[i+6]);
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], " ", 8)) {
295 strcpy(&outbuf[i+1], &outbuf[i+8]);
298 else if (!strncasecmp(&outbuf[i], "<", 4)) {
300 strcpy(&outbuf[i+1], &outbuf[i+4]);
303 else if (!strncasecmp(&outbuf[i], ">", 4)) {
305 strcpy(&outbuf[i+1], &outbuf[i+4]);
308 else if (!strncasecmp(&outbuf[i], "&", 5)) {
309 strcpy(&outbuf[i+1], &outbuf[i+5]);
312 else if (!strncasecmp(&outbuf[i], """, 6)) {
314 strcpy(&outbuf[i+1], &outbuf[i+6]);
317 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
319 strcpy(&outbuf[i+1], &outbuf[i+7]);
322 else if (!strncasecmp(&outbuf[i], "’", 7)) {
324 strcpy(&outbuf[i+1], &outbuf[i+7]);
327 else if (!strncasecmp(&outbuf[i], "©", 6)) {
331 strcpy(&outbuf[i+3], &outbuf[i+6]);
334 else if (!strncasecmp(&outbuf[i], "•", 6)) {
338 strcpy(&outbuf[i+3], &outbuf[i+6]);
341 else if (!strncasecmp(&outbuf[i], "…", 8)) {
345 strcpy(&outbuf[i+3], &outbuf[i+8]);
348 else if (!strncasecmp(&outbuf[i], "™", 7)) {
353 strcpy(&outbuf[i+4], &outbuf[i+7]);
356 else if (!strncasecmp(&outbuf[i], "®", 5)) {
360 strcpy(&outbuf[i+3], &outbuf[i+5]);
363 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
367 strcpy(&outbuf[i+3], &outbuf[i+8]);
370 else if (!strncasecmp(&outbuf[i], "½", 8)) {
374 strcpy(&outbuf[i+3], &outbuf[i+8]);
377 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
381 strcpy(&outbuf[i+3], &outbuf[i+8]);
384 else if (!strncasecmp(&outbuf[i], "–", 7)) {
387 strcpy(&outbuf[i+2], &outbuf[i+7]);
390 else if (!strncasecmp(&outbuf[i], "—", 7)) {
394 strcpy(&outbuf[i+3], &outbuf[i+7]);
397 else if (!strncmp(&outbuf[i], "Ç", 8)) {
399 strcpy(&outbuf[i+1], &outbuf[i+8]);
402 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
404 strcpy(&outbuf[i+1], &outbuf[i+8]);
407 else if (!strncmp(&outbuf[i], "È", 8)) {
409 strcpy(&outbuf[i+1], &outbuf[i+8]);
412 else if (!strncasecmp(&outbuf[i], "è", 8)) {
414 strcpy(&outbuf[i+1], &outbuf[i+8]);
417 else if (!strncmp(&outbuf[i], "Ê", 7)) {
419 strcpy(&outbuf[i+1], &outbuf[i+7]);
422 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
424 strcpy(&outbuf[i+1], &outbuf[i+7]);
427 else if (!strncmp(&outbuf[i], "É", 8)) {
429 strcpy(&outbuf[i+1], &outbuf[i+8]);
432 else if (!strncasecmp(&outbuf[i], "é", 8)) {
434 strcpy(&outbuf[i+1], &outbuf[i+8]);
437 else if (!strncmp(&outbuf[i], "À", 8)) {
439 strcpy(&outbuf[i+1], &outbuf[i+8]);
442 else if (!strncasecmp(&outbuf[i], "à", 8)) {
444 strcpy(&outbuf[i+1], &outbuf[i+8]);
447 else if (!strncasecmp(&outbuf[i], "“", 7)) {
449 strcpy(&outbuf[i+1], &outbuf[i+7]);
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 /* two-digit decimal equivalents */
473 else if (outbuf[i] == '&' &&
474 outbuf[i + 1] == '#' &&
475 isdigit(outbuf[i + 2]) &&
476 isdigit(outbuf[i + 3]) &&
477 (outbuf[i+4] == ';') )
480 sscanf(&outbuf[i+2], "%02d", &scanch);
482 strcpy(&outbuf[i+1], &outbuf[i+5]);
485 /* three-digit decimal equivalents */
486 else if (outbuf[i] == '&' &&
487 outbuf[i + 1] == '#' &&
488 isdigit(outbuf[i + 2]) &&
489 isdigit(outbuf[i + 3]) &&
490 isdigit(outbuf[i + 4]) &&
491 (outbuf[i + 5] == ';') )
494 sscanf(&outbuf[i+2], "%03d", &scanch);
496 strcpy(&outbuf[i+1], &outbuf[i+6]);
499 /* four-digit decimal equivalents */
500 else if (outbuf[i] == '&' &&
501 outbuf[i + 1] == '#' &&
502 isdigit(outbuf[i + 2]) &&
503 isdigit(outbuf[i + 3]) &&
504 isdigit(outbuf[i + 4]) &&
505 isdigit(outbuf[i + 5]) &&
506 (outbuf[i + 6] == ';') )
509 sscanf(&outbuf[i+2], "%04d", &scanch);
511 strcpy(&outbuf[i+1], &outbuf[i+7]);
516 /* Make sure the output buffer is big enough */
517 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
518 outptr_buffer_size += SIZ;
519 outptr = realloc(outptr, outptr_buffer_size);
520 if (outptr == NULL) {
525 /* Output any lines terminated with hard line breaks */
528 if (strlen(outbuf) > 0) {
529 for (i = 0; i<strlen(outbuf); ++i) {
530 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
532 strncpy(&outptr[output_len], outbuf, i+1);
535 strcpy(outbuf, &outbuf[i+1]);
543 /* Add soft line breaks */
544 if (strlen(outbuf) > (screenwidth - 2 )) {
546 for (i=0; i<(screenwidth-2); ++i) {
547 if (outbuf[i]==32) rb = i;
550 strncpy(&outptr[output_len], outbuf, rb);
552 strcpy(&outptr[output_len], nl);
553 output_len += strlen(nl);
554 strcpy(outbuf, &outbuf[rb+1]);
556 strncpy(&outptr[output_len], outbuf,
558 output_len += (screenwidth-2);
559 strcpy(&outptr[output_len], nl);
560 output_len += strlen(nl);
561 strcpy(outbuf, &outbuf[screenwidth-2]);
565 } while (done_reading == 0);
567 strcpy(&outptr[output_len], outbuf);
568 output_len += strlen(outbuf);
570 /* Strip leading/trailing whitespace. We can't do this with
571 * striplt() because it uses too many strlen()'s
573 while ((output_len > 0) && (isspace(outptr[0]))) {
574 strcpy(outptr, &outptr[1]);
577 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
578 outptr[output_len-1] = 0;
582 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
583 strcat(outptr, "\n");