2 * Functions which handle translation between HTML and plain text
3 * Copyright (c) 2000-2010 by the citadel.org team
5 * This program is free 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;
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 (!strcasecmp(tag, "B"))
202 || (!strcasecmp(tag, "/B"))
203 || (!strcasecmp(tag, "STRONG"))
204 || (!strcasecmp(tag, "/STRONG"))
211 (!strcasecmp(tag, "I"))
212 || (!strcasecmp(tag, "/I"))
213 || (!strcasecmp(tag, "EM"))
214 || (!strcasecmp(tag, "/EM"))
221 (!strcasecmp(tag, "U"))
222 || (!strcasecmp(tag, "/U"))
228 else if (!strcasecmp(tag, "BR")) {
232 else if (!strcasecmp(tag, "TR")) {
236 else if (!strcasecmp(tag, "/TABLE")) {
240 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
243 for (j=0; j<blockquote; ++j) strcat(nl, ">");
247 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
248 strcat(outbuf, "\n");
251 for (j=0; j<blockquote; ++j) strcat(nl, ">");
255 else if (!strcasecmp(tag, "STYLE")) {
258 styletag_start = strlen(outbuf);
262 else if (!strcasecmp(tag, "/STYLE")) {
265 outbuf[styletag_start] = 0;
271 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
272 tag[strlen(tag)+1] = 0;
273 tag[strlen(tag)] = ch;
277 outbuf[strlen(outbuf)+1] = 0;
278 outbuf[strlen(outbuf)] = ch;
281 strcpy(inbuf, &inbuf[i]);
284 /* Convert &; tags to the forbidden characters */
285 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
287 /* Character entity references */
288 if (!strncasecmp(&outbuf[i], " ", 6)) {
290 strcpy(&outbuf[i+1], &outbuf[i+6]);
293 if (!strncasecmp(&outbuf[i], " ", 6)) {
295 strcpy(&outbuf[i+1], &outbuf[i+6]);
298 if (!strncasecmp(&outbuf[i], " ", 6)) {
300 strcpy(&outbuf[i+1], &outbuf[i+6]);
303 if (!strncasecmp(&outbuf[i], " ", 8)) {
305 strcpy(&outbuf[i+1], &outbuf[i+8]);
308 else if (!strncasecmp(&outbuf[i], "<", 4)) {
310 strcpy(&outbuf[i+1], &outbuf[i+4]);
313 else if (!strncasecmp(&outbuf[i], ">", 4)) {
315 strcpy(&outbuf[i+1], &outbuf[i+4]);
318 else if (!strncasecmp(&outbuf[i], "&", 5)) {
319 strcpy(&outbuf[i+1], &outbuf[i+5]);
322 else if (!strncasecmp(&outbuf[i], """, 6)) {
324 strcpy(&outbuf[i+1], &outbuf[i+6]);
327 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
329 strcpy(&outbuf[i+1], &outbuf[i+7]);
332 else if (!strncasecmp(&outbuf[i], "’", 7)) {
334 strcpy(&outbuf[i+1], &outbuf[i+7]);
337 else if (!strncasecmp(&outbuf[i], "©", 6)) {
341 strcpy(&outbuf[i+3], &outbuf[i+6]);
344 else if (!strncasecmp(&outbuf[i], "•", 6)) {
348 strcpy(&outbuf[i+3], &outbuf[i+6]);
351 else if (!strncasecmp(&outbuf[i], "…", 8)) {
355 strcpy(&outbuf[i+3], &outbuf[i+8]);
358 else if (!strncasecmp(&outbuf[i], "™", 7)) {
363 strcpy(&outbuf[i+4], &outbuf[i+7]);
366 else if (!strncasecmp(&outbuf[i], "®", 5)) {
370 strcpy(&outbuf[i+3], &outbuf[i+5]);
373 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
377 strcpy(&outbuf[i+3], &outbuf[i+8]);
380 else if (!strncasecmp(&outbuf[i], "½", 8)) {
384 strcpy(&outbuf[i+3], &outbuf[i+8]);
387 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
391 strcpy(&outbuf[i+3], &outbuf[i+8]);
394 else if (!strncasecmp(&outbuf[i], "–", 7)) {
397 strcpy(&outbuf[i+2], &outbuf[i+7]);
400 else if (!strncasecmp(&outbuf[i], "—", 7)) {
404 strcpy(&outbuf[i+3], &outbuf[i+7]);
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], "È", 8)) {
419 strcpy(&outbuf[i+1], &outbuf[i+8]);
422 else if (!strncasecmp(&outbuf[i], "è", 8)) {
424 strcpy(&outbuf[i+1], &outbuf[i+8]);
427 else if (!strncmp(&outbuf[i], "Ê", 7)) {
429 strcpy(&outbuf[i+1], &outbuf[i+7]);
432 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
434 strcpy(&outbuf[i+1], &outbuf[i+7]);
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 (!strncmp(&outbuf[i], "À", 8)) {
449 strcpy(&outbuf[i+1], &outbuf[i+8]);
452 else if (!strncasecmp(&outbuf[i], "à", 8)) {
454 strcpy(&outbuf[i+1], &outbuf[i+8]);
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 ((!strncmp(&outbuf[i], "&#", 2))
474 && (outbuf[i+4] == ';') ) {
476 sscanf(&outbuf[i+2], "%02d", &scanch);
478 strcpy(&outbuf[i+1], &outbuf[i+5]);
481 /* three-digit decimal equivalents */
482 else if ((!strncmp(&outbuf[i], "&#", 2))
483 && (outbuf[i+5] == ';') ) {
485 sscanf(&outbuf[i+2], "%03d", &scanch);
487 strcpy(&outbuf[i+1], &outbuf[i+6]);
492 /* Make sure the output buffer is big enough */
493 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
494 outptr_buffer_size += SIZ;
495 outptr = realloc(outptr, outptr_buffer_size);
496 if (outptr == NULL) {
501 /* Output any lines terminated with hard line breaks */
504 if (strlen(outbuf) > 0) {
505 for (i = 0; i<strlen(outbuf); ++i) {
506 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
508 strncpy(&outptr[output_len], outbuf, i+1);
512 strcpy(&outptr[output_len], " ");
516 strcpy(outbuf, &outbuf[i+1]);
524 /* Add soft line breaks */
525 if (strlen(outbuf) > (screenwidth - 2 )) {
527 for (i=0; i<(screenwidth-2); ++i) {
528 if (outbuf[i]==32) rb = i;
531 strncpy(&outptr[output_len], outbuf, rb);
533 strcpy(&outptr[output_len], nl);
534 output_len += strlen(nl);
536 strcpy(&outptr[output_len], " ");
539 strcpy(outbuf, &outbuf[rb+1]);
541 strncpy(&outptr[output_len], outbuf,
543 output_len += (screenwidth-2);
544 strcpy(&outptr[output_len], nl);
545 output_len += strlen(nl);
547 strcpy(&outptr[output_len], " ");
550 strcpy(outbuf, &outbuf[screenwidth-2]);
554 } while (done_reading == 0);
556 strcpy(&outptr[output_len], outbuf);
557 output_len += strlen(outbuf);
559 /* Strip leading/trailing whitespace. We can't do this with
560 * striplt() because it uses too many strlen()'s
562 while ((output_len > 0) && (isspace(outptr[0]))) {
563 strcpy(outptr, &outptr[1]);
566 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
567 outptr[output_len-1] = 0;
571 if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
572 strcat(outptr, "\n");