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; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
42 #include "libcitadel.h"
46 * Convert HTML to plain text.
48 * inputmsg = pointer to raw HTML message
49 * screenwidth = desired output screenwidth
51 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth) {
59 size_t outptr_buffer_size;
60 size_t output_len = 0;
61 int i, j, ch, did_out, rb, scanch;
62 int nest = 0; /* Bracket nesting level */
63 int blockquote = 0; /* BLOCKQUOTE nesting level */
64 int styletag = 0; /* STYLE tag nesting level */
65 int styletag_start = 0;
66 int bytes_processed = 0;
74 if (msglen == 0) msglen = strlen(inputmsg);
76 outptr_buffer_size = strlen(inptr) + SIZ;
77 outptr = malloc(outptr_buffer_size);
78 if (outptr == NULL) return NULL;
83 /* Fill the input buffer */
84 inbuf_len = strlen(inbuf);
85 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
89 inbuf[inbuf_len++] = ch;
97 if (bytes_processed > msglen) {
103 /* Do some parsing */
104 if (!IsEmptyStr(inbuf)) {
107 /* Fold in all the spacing */
108 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
109 if (inbuf[i]==10) inbuf[i]=32;
110 if (inbuf[i]==13) inbuf[i]=32;
111 if (inbuf[i]==9) inbuf[i]=32;
113 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
114 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
115 strcpy(&inbuf[i], &inbuf[i+1]);
119 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
128 else if (ch == '>') { /* We have a tag. */
129 if (nest > 0) --nest;
131 /* Unqualify the tag (truncate at first space) */
132 if (strchr(tag, ' ') != NULL) {
133 strcpy(strchr(tag, ' '), "");
136 if (!strcasecmp(tag, "P")) {
141 if (!strcasecmp(tag, "/DIV")) {
146 if (!strcasecmp(tag, "LI")) {
148 strcat(outbuf, " * ");
151 else if (!strcasecmp(tag, "/UL")) {
156 else if (!strcasecmp(tag, "H1")) {
161 else if (!strcasecmp(tag, "H2")) {
166 else if (!strcasecmp(tag, "H3")) {
171 else if (!strcasecmp(tag, "H4")) {
176 else if (!strcasecmp(tag, "/H1")) {
180 else if (!strcasecmp(tag, "/H2")) {
184 else if (!strcasecmp(tag, "/H3")) {
188 else if (!strcasecmp(tag, "/H4")) {
192 else if (!strcasecmp(tag, "HR")) {
195 for (j=0; j<screenwidth-2; ++j)
201 These seemed like a good idea at the time, but it just makes a mess.
204 (!strcasecmp(tag, "B"))
205 || (!strcasecmp(tag, "/B"))
206 || (!strcasecmp(tag, "STRONG"))
207 || (!strcasecmp(tag, "/STRONG"))
213 (!strcasecmp(tag, "I"))
214 || (!strcasecmp(tag, "/I"))
215 || (!strcasecmp(tag, "EM"))
216 || (!strcasecmp(tag, "/EM"))
222 (!strcasecmp(tag, "U"))
223 || (!strcasecmp(tag, "/U"))
229 else if (!strcasecmp(tag, "BR")) {
233 else if (!strcasecmp(tag, "TR")) {
237 else if (!strcasecmp(tag, "/TABLE")) {
241 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
244 for (j=0; j<blockquote; ++j) strcat(nl, ">");
248 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
249 strcat(outbuf, "\n");
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]);
567 strncpy(&outptr[output_len], outbuf,
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. We can't do this with
582 * striplt() because it uses too many strlen()'s
584 while ((output_len > 0) && (isspace(outptr[0]))) {
585 strcpy(outptr, &outptr[1]);
588 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
589 outptr[output_len-1] = 0;
593 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
594 strcat(outptr, "\n");