2 * Functions which handle translation between HTML and plain text
3 * Copyright (c) 2000-2010 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
50 * do_citaformat = set to 1 to indent newlines with spaces
52 char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
60 size_t outptr_buffer_size;
61 size_t output_len = 0;
62 int i, j, ch, did_out, rb, scanch;
63 int nest = 0; /* Bracket nesting level */
64 int blockquote = 0; /* BLOCKQUOTE nesting level */
65 int styletag = 0; /* STYLE tag nesting level */
66 int styletag_start = 0;
67 int bytes_processed = 0;
75 if (msglen == 0) msglen = strlen(inputmsg);
77 outptr_buffer_size = strlen(inptr) + SIZ;
78 outptr = malloc(outptr_buffer_size);
79 if (outptr == NULL) return NULL;
84 /* Fill the input buffer */
85 inbuf_len = strlen(inbuf);
86 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
90 inbuf[inbuf_len++] = ch;
98 if (bytes_processed > msglen) {
104 /* Do some parsing */
105 if (!IsEmptyStr(inbuf)) {
108 /* Fold in all the spacing */
109 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
110 if (inbuf[i]==10) inbuf[i]=32;
111 if (inbuf[i]==13) inbuf[i]=32;
112 if (inbuf[i]==9) inbuf[i]=32;
114 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
115 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
116 strcpy(&inbuf[i], &inbuf[i+1]);
120 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
129 else if (ch == '>') { /* We have a tag. */
130 if (nest > 0) --nest;
132 /* Unqualify the tag (truncate at first space) */
133 if (strchr(tag, ' ') != NULL) {
134 strcpy(strchr(tag, ' '), "");
137 if (!strcasecmp(tag, "P")) {
142 if (!strcasecmp(tag, "/DIV")) {
147 if (!strcasecmp(tag, "LI")) {
149 strcat(outbuf, " * ");
152 else if (!strcasecmp(tag, "/UL")) {
157 else if (!strcasecmp(tag, "H1")) {
162 else if (!strcasecmp(tag, "H2")) {
167 else if (!strcasecmp(tag, "H3")) {
172 else if (!strcasecmp(tag, "H4")) {
177 else if (!strcasecmp(tag, "/H1")) {
181 else if (!strcasecmp(tag, "/H2")) {
185 else if (!strcasecmp(tag, "/H3")) {
189 else if (!strcasecmp(tag, "/H4")) {
193 else if (!strcasecmp(tag, "HR")) {
196 for (j=0; j<screenwidth-2; ++j)
202 (!strcasecmp(tag, "B"))
203 || (!strcasecmp(tag, "/B"))
204 || (!strcasecmp(tag, "STRONG"))
205 || (!strcasecmp(tag, "/STRONG"))
212 (!strcasecmp(tag, "I"))
213 || (!strcasecmp(tag, "/I"))
214 || (!strcasecmp(tag, "EM"))
215 || (!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;
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);
547 strcpy(&outptr[output_len], " ");
551 strcpy(outbuf, &outbuf[i+1]);
559 /* Add soft line breaks */
560 if (strlen(outbuf) > (screenwidth - 2 )) {
562 for (i=0; i<(screenwidth-2); ++i) {
563 if (outbuf[i]==32) rb = i;
566 strncpy(&outptr[output_len], outbuf, rb);
568 strcpy(&outptr[output_len], nl);
569 output_len += strlen(nl);
571 strcpy(&outptr[output_len], " ");
574 strcpy(outbuf, &outbuf[rb+1]);
576 strncpy(&outptr[output_len], outbuf,
578 output_len += (screenwidth-2);
579 strcpy(&outptr[output_len], nl);
580 output_len += strlen(nl);
582 strcpy(&outptr[output_len], " ");
585 strcpy(outbuf, &outbuf[screenwidth-2]);
589 } while (done_reading == 0);
591 strcpy(&outptr[output_len], outbuf);
592 output_len += strlen(outbuf);
594 /* Strip leading/trailing whitespace. We can't do this with
595 * striplt() because it uses too many strlen()'s
597 while ((output_len > 0) && (isspace(outptr[0]))) {
598 strcpy(outptr, &outptr[1]);
601 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
602 outptr[output_len-1] = 0;
606 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
607 strcat(outptr, "\n");