2 * $Id: html.c 6014 2008-02-04 18:38:35Z ajc $
4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2010 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
44 #include "libcitadel.h"
48 * Convert HTML to plain text.
50 * inputmsg = pointer to raw HTML message
51 * screenwidth = desired output screenwidth
52 * do_citaformat = set to 1 to indent newlines with spaces
54 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
62 size_t outptr_buffer_size;
63 size_t output_len = 0;
64 int i, j, ch, did_out, rb, scanch;
65 int nest = 0; /* Bracket nesting level */
66 int blockquote = 0; /* BLOCKQUOTE nesting level */
67 int styletag = 0; /* STYLE tag nesting level */
68 int styletag_start = 0;
69 int bytes_processed = 0;
76 if (msglen == 0) msglen = strlen(inputmsg);
78 outptr_buffer_size = strlen(inptr) + SIZ;
79 outptr = malloc(outptr_buffer_size);
80 if (outptr == NULL) return NULL;
85 /* Fill the input buffer */
86 inbuf_len = strlen(inbuf);
87 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
91 inbuf[inbuf_len++] = ch;
99 if (bytes_processed > msglen) {
105 /* Do some parsing */
106 if (!IsEmptyStr(inbuf)) {
109 /* Fold in all the spacing */
110 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
111 if (inbuf[i]==10) inbuf[i]=32;
112 if (inbuf[i]==13) inbuf[i]=32;
113 if (inbuf[i]==9) inbuf[i]=32;
115 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
116 while ((inbuf[i]==32)&&(inbuf[i+1]==32)) {
117 strcpy(&inbuf[i], &inbuf[i+1]);
121 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
130 else if (ch == '>') { /* We have a tag. */
131 if (nest > 0) --nest;
133 /* Unqualify the tag (truncate at first space) */
134 if (strchr(tag, ' ') != NULL) {
135 strcpy(strchr(tag, ' '), "");
138 if (!strcasecmp(tag, "P")) {
143 if (!strcasecmp(tag, "/DIV")) {
148 if (!strcasecmp(tag, "LI")) {
150 strcat(outbuf, " * ");
153 else if (!strcasecmp(tag, "/UL")) {
158 else if (!strcasecmp(tag, "H1")) {
163 else if (!strcasecmp(tag, "H2")) {
168 else if (!strcasecmp(tag, "H3")) {
173 else if (!strcasecmp(tag, "H4")) {
178 else if (!strcasecmp(tag, "/H1")) {
182 else if (!strcasecmp(tag, "/H2")) {
186 else if (!strcasecmp(tag, "/H3")) {
190 else if (!strcasecmp(tag, "/H4")) {
194 else if (!strcasecmp(tag, "HR")) {
197 for (j=0; j<screenwidth-2; ++j)
203 (!strcasecmp(tag, "B"))
204 || (!strcasecmp(tag, "/B"))
205 || (!strcasecmp(tag, "STRONG"))
206 || (!strcasecmp(tag, "/STRONG"))
213 (!strcasecmp(tag, "I"))
214 || (!strcasecmp(tag, "/I"))
215 || (!strcasecmp(tag, "EM"))
216 || (!strcasecmp(tag, "/EM"))
223 (!strcasecmp(tag, "U"))
224 || (!strcasecmp(tag, "/U"))
230 else if (!strcasecmp(tag, "BR")) {
234 else if (!strcasecmp(tag, "TR")) {
238 else if (!strcasecmp(tag, "/TABLE")) {
242 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
245 for (j=0; j<blockquote; ++j) strcat(nl, ">");
249 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
250 strcat(outbuf, "\n");
253 for (j=0; j<blockquote; ++j) strcat(nl, ">");
257 else if (!strcasecmp(tag, "STYLE")) {
260 styletag_start = strlen(outbuf);
264 else if (!strcasecmp(tag, "/STYLE")) {
267 outbuf[styletag_start] = 0;
273 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
274 tag[strlen(tag)+1] = 0;
275 tag[strlen(tag)] = ch;
279 outbuf[strlen(outbuf)+1] = 0;
280 outbuf[strlen(outbuf)] = ch;
283 strcpy(inbuf, &inbuf[i]);
286 /* Convert &; tags to the forbidden characters */
287 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
289 /* Character entity references */
290 if (!strncasecmp(&outbuf[i], " ", 6)) {
292 strcpy(&outbuf[i+1], &outbuf[i+6]);
295 if (!strncasecmp(&outbuf[i], " ", 6)) {
297 strcpy(&outbuf[i+1], &outbuf[i+6]);
300 if (!strncasecmp(&outbuf[i], " ", 6)) {
302 strcpy(&outbuf[i+1], &outbuf[i+6]);
305 if (!strncasecmp(&outbuf[i], " ", 8)) {
307 strcpy(&outbuf[i+1], &outbuf[i+8]);
310 else if (!strncasecmp(&outbuf[i], "<", 4)) {
312 strcpy(&outbuf[i+1], &outbuf[i+4]);
315 else if (!strncasecmp(&outbuf[i], ">", 4)) {
317 strcpy(&outbuf[i+1], &outbuf[i+4]);
320 else if (!strncasecmp(&outbuf[i], "&", 5)) {
321 strcpy(&outbuf[i+1], &outbuf[i+5]);
324 else if (!strncasecmp(&outbuf[i], """, 6)) {
326 strcpy(&outbuf[i+1], &outbuf[i+6]);
329 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
331 strcpy(&outbuf[i+1], &outbuf[i+7]);
334 else if (!strncasecmp(&outbuf[i], "’", 7)) {
336 strcpy(&outbuf[i+1], &outbuf[i+7]);
339 else if (!strncasecmp(&outbuf[i], "©", 6)) {
343 strcpy(&outbuf[i+3], &outbuf[i+6]);
346 else if (!strncasecmp(&outbuf[i], "•", 6)) {
350 strcpy(&outbuf[i+3], &outbuf[i+6]);
353 else if (!strncasecmp(&outbuf[i], "…", 8)) {
357 strcpy(&outbuf[i+3], &outbuf[i+8]);
360 else if (!strncasecmp(&outbuf[i], "™", 7)) {
365 strcpy(&outbuf[i+4], &outbuf[i+7]);
368 else if (!strncasecmp(&outbuf[i], "®", 5)) {
372 strcpy(&outbuf[i+3], &outbuf[i+5]);
375 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
379 strcpy(&outbuf[i+3], &outbuf[i+8]);
382 else if (!strncasecmp(&outbuf[i], "½", 8)) {
386 strcpy(&outbuf[i+3], &outbuf[i+8]);
389 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
393 strcpy(&outbuf[i+3], &outbuf[i+8]);
396 else if (!strncasecmp(&outbuf[i], "–", 7)) {
399 strcpy(&outbuf[i+2], &outbuf[i+7]);
402 else if (!strncasecmp(&outbuf[i], "—", 7)) {
406 strcpy(&outbuf[i+3], &outbuf[i+7]);
409 else if (!strncmp(&outbuf[i], "Ç", 8)) {
411 strcpy(&outbuf[i+1], &outbuf[i+8]);
414 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
416 strcpy(&outbuf[i+1], &outbuf[i+8]);
419 else if (!strncmp(&outbuf[i], "È", 8)) {
421 strcpy(&outbuf[i+1], &outbuf[i+8]);
424 else if (!strncasecmp(&outbuf[i], "è", 8)) {
426 strcpy(&outbuf[i+1], &outbuf[i+8]);
429 else if (!strncmp(&outbuf[i], "Ê", 7)) {
431 strcpy(&outbuf[i+1], &outbuf[i+7]);
434 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
436 strcpy(&outbuf[i+1], &outbuf[i+7]);
439 else if (!strncmp(&outbuf[i], "É", 8)) {
441 strcpy(&outbuf[i+1], &outbuf[i+8]);
444 else if (!strncasecmp(&outbuf[i], "é", 8)) {
446 strcpy(&outbuf[i+1], &outbuf[i+8]);
449 else if (!strncmp(&outbuf[i], "À", 8)) {
451 strcpy(&outbuf[i+1], &outbuf[i+8]);
454 else if (!strncasecmp(&outbuf[i], "à", 8)) {
456 strcpy(&outbuf[i+1], &outbuf[i+8]);
459 else if (!strncasecmp(&outbuf[i], "“", 7)) {
461 strcpy(&outbuf[i+1], &outbuf[i+7]);
464 else if (!strncasecmp(&outbuf[i], "”", 7)) {
466 strcpy(&outbuf[i+1], &outbuf[i+7]);
469 else if (!strncasecmp(&outbuf[i], "´", 7)) {
471 strcpy(&outbuf[i+1], &outbuf[i+7]);
474 /* two-digit decimal equivalents */
475 else if ((!strncmp(&outbuf[i], "&#", 2))
476 && (outbuf[i+4] == ';') ) {
478 sscanf(&outbuf[i+2], "%02d", &scanch);
480 strcpy(&outbuf[i+1], &outbuf[i+5]);
483 /* three-digit decimal equivalents */
484 else if ((!strncmp(&outbuf[i], "&#", 2))
485 && (outbuf[i+5] == ';') ) {
487 sscanf(&outbuf[i+2], "%03d", &scanch);
489 strcpy(&outbuf[i+1], &outbuf[i+6]);
494 /* Make sure the output buffer is big enough */
495 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
496 outptr_buffer_size += SIZ;
497 outptr = realloc(outptr, outptr_buffer_size);
498 if (outptr == NULL) {
503 /* Output any lines terminated with hard line breaks */
506 if (strlen(outbuf) > 0) {
507 for (i = 0; i<strlen(outbuf); ++i) {
508 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
510 strncpy(&outptr[output_len], outbuf, i+1);
514 strcpy(&outptr[output_len], " ");
518 strcpy(outbuf, &outbuf[i+1]);
526 /* Add soft line breaks */
527 if (strlen(outbuf) > (screenwidth - 2 )) {
529 for (i=0; i<(screenwidth-2); ++i) {
530 if (outbuf[i]==32) rb = i;
533 strncpy(&outptr[output_len], outbuf, rb);
535 strcpy(&outptr[output_len], nl);
536 output_len += strlen(nl);
538 strcpy(&outptr[output_len], " ");
541 strcpy(outbuf, &outbuf[rb+1]);
543 strncpy(&outptr[output_len], outbuf,
545 output_len += (screenwidth-2);
546 strcpy(&outptr[output_len], nl);
547 output_len += strlen(nl);
549 strcpy(&outptr[output_len], " ");
552 strcpy(outbuf, &outbuf[screenwidth-2]);
556 } while (done_reading == 0);
558 strcpy(&outptr[output_len], outbuf);
559 output_len += strlen(outbuf);
561 /* Strip leading/trailing whitespace. We can't do this with
562 * striplt() because it uses too many strlen()'s
564 while ((output_len > 0) && (isspace(outptr[0]))) {
565 strcpy(outptr, &outptr[1]);
568 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
569 outptr[output_len-1] = 0;
573 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
574 strcat(outptr, "\n");