fix dlen
[citadel.git] / libcitadel / lib / html_to_ascii.c
index ccf074a48574e8dbb4b15e71cb38da1d15eae401..ca2de8df8d9e7b0eb756ca1a7186a16d5f571641 100644 (file)
@@ -1,21 +1,8 @@
-/*
- * Functions which handle translation between HTML and plain text
- * Copyright (c) 2000-2010 by the citadel.org team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
+// Functions which handle translation between HTML and plain text
+// Copyright (c) 2000-2023 by the citadel.org team
+//
+// This program is open source software.  Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
 
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <limits.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include "libcitadel.h"
  
 
-/*
- * Convert HTML to plain text.
- *
- * inputmsg      = pointer to raw HTML message
- * screenwidth   = desired output screenwidth
- * do_citaformat = set to 1 to indent newlines with spaces
- */
-char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
+// Convert HTML to plain text.
+//
+// inputmsg    = pointer to raw HTML message
+// msglen      = stop reading after this many bytes
+// screenwidth = desired output screenwidth
+// ansi                = if nonzero, assume output is to a terminal that supports ANSI escape codes
+//
+char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int ansi) {
        char inbuf[SIZ];
        int inbuf_len = 0;
        char outbuf[SIZ];
        char tag[1024];
        int done_reading = 0;
-       char *inptr;
+       const char *inptr;
        char *outptr;
        size_t outptr_buffer_size;
        size_t output_len = 0;
        int i, j, ch, did_out, rb, scanch;
-       int nest = 0;           /* Bracket nesting level */
-       int blockquote = 0;     /* BLOCKQUOTE nesting level */
-       int styletag = 0;       /* STYLE tag nesting level */
+       int nest = 0;                           // Bracket nesting level
+       int blockquote = 0;                     // BLOCKQUOTE nesting level
+       int styletag = 0;                       // STYLE tag nesting level
        int styletag_start = 0;
        int bytes_processed = 0;
        char nl[128];
 
+       tag[0] = '\0';
        strcpy(nl, "\n");
        inptr = inputmsg;
        strcpy(inbuf, "");
@@ -80,7 +57,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
        output_len = 0;
 
        do {
-               /* Fill the input buffer */
+               // Fill the input buffer
                inbuf_len = strlen(inbuf);
                if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
 
@@ -100,11 +77,10 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
 
                }
 
-               /* Do some parsing */
+               // Do some parsing
                if (!IsEmptyStr(inbuf)) {
 
-
-                   /* Fold in all the spacing */
+                   // Fold in all the spacing
                    for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
                        if (inbuf[i]==10) inbuf[i]=32;
                        if (inbuf[i]==13) inbuf[i]=32;
@@ -125,10 +101,10 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                strcpy(tag, "");
                        }
 
-                       else if (ch == '>') {   /* We have a tag. */
+                       else if (ch == '>') {   // We have a tag.
                                if (nest > 0) --nest;
 
-                               /* Unqualify the tag (truncate at first space) */
+                               // Unqualify the tag (truncate at first space)
                                if (strchr(tag, ' ') != NULL) {
                                        strcpy(strchr(tag, ' '), "");
                                }
@@ -199,30 +175,49 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
 
                                else if (
                                        (!strcasecmp(tag, "B"))
-                                       || (!strcasecmp(tag, "/B"))
                                        || (!strcasecmp(tag, "STRONG"))
+                               ) {
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[1m");
+                                       }
+                               }
+                               else if (
+                                       (!strcasecmp(tag, "/B"))
                                        || (!strcasecmp(tag, "/STRONG"))
                                ) {
-                                       strcat(outbuf, "*");
-                                       
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[22m");
+                                       }
                                }
 
                                else if (
                                        (!strcasecmp(tag, "I"))
-                                       || (!strcasecmp(tag, "/I"))
                                        || (!strcasecmp(tag, "EM"))
-                                       || (!strcasecmp(tag, "/EM"))
                                ) {
-                                       strcat(outbuf, "/");
-                                       
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[3m");
+                                       }
                                }
 
                                else if (
-                                       (!strcasecmp(tag, "U"))
-                                       || (!strcasecmp(tag, "/U"))
+                                       (!strcasecmp(tag, "/I"))
+                                       || (!strcasecmp(tag, "/EM"))
                                ) {
-                                       strcat(outbuf, "_");
-                                       
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[23m");
+                                       }
+                               }
+
+                               else if (!strcasecmp(tag, "U")) {
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[4m");
+                                       }
+                               }
+
+                               else if (!strcasecmp(tag, "/U")) {
+                                       if (ansi) {
+                                               strcat(outbuf, "\033[24m");
+                                       }
                                }
 
                                else if (!strcasecmp(tag, "BR")) {
@@ -240,6 +235,9 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                else if (!strcasecmp(tag, "BLOCKQUOTE")) {
                                        ++blockquote;
                                        strcpy(nl, "\n");
+                                       if ( (blockquote == 1) && (ansi) ) {
+                                               strcat(nl, "\033[2m\033[2m");
+                                       }
                                        for (j=0; j<blockquote; ++j) strcat(nl, ">");
                                        strcat(outbuf, nl);
                                }
@@ -247,6 +245,9 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
                                        strcat(outbuf, "\n");
                                        --blockquote;
+                                       if ( (blockquote == 0) && (ansi) ) {
+                                               strcat(outbuf, "\033[22m\033[22m");
+                                       }
                                        strcpy(nl, "\n");
                                        for (j=0; j<blockquote; ++j) strcat(nl, ">");
                                        strcat(outbuf, nl);
@@ -273,7 +274,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                tag[strlen(tag)] = ch;
                        }
                                
-                       else if (!nest) {
+                       else if ((!nest) && (styletag == 0)) {
                                outbuf[strlen(outbuf)+1] = 0;
                                outbuf[strlen(outbuf)] = ch;
                        }
@@ -281,10 +282,10 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                    strcpy(inbuf, &inbuf[i]);
                }
 
-               /* Convert &; tags to the forbidden characters */
+               // Convert &; tags to the forbidden characters
                if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
 
-                       /* Character entity references */
+                       // Character entity references
                        if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
                                outbuf[i] = ' ';
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
@@ -469,27 +470,61 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                strcpy(&outbuf[i+1], &outbuf[i+7]);
                        }
 
-                       /* two-digit decimal equivalents */
-                       else if ((!strncmp(&outbuf[i], "&#", 2))
-                             && (outbuf[i+4] == ';') ) {
+                       else if (!strncasecmp(&outbuf[i], "&#8217;", 7)) {
+                               outbuf[i] = '\'';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&#8211;", 7)) {
+                               outbuf[i] = '-';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       // two-digit decimal equivalents
+                       else if (outbuf[i] == '&'       &&
+                                outbuf[i + 1] == '#'   &&
+                                isdigit(outbuf[i + 2]) && 
+                                isdigit(outbuf[i + 3]) &&
+                                (outbuf[i+4] == ';') ) 
+                       {
                                scanch = 0;
                                sscanf(&outbuf[i+2], "%02d", &scanch);
                                outbuf[i] = scanch;
                                strcpy(&outbuf[i+1], &outbuf[i+5]);
                        }
 
-                       /* three-digit decimal equivalents */
-                       else if ((!strncmp(&outbuf[i], "&#", 2))
-                             && (outbuf[i+5] == ';') ) {
+                       // three-digit decimal equivalents
+                       else if (outbuf[i] == '&'       &&
+                                outbuf[i + 1] == '#'   &&
+                                isdigit(outbuf[i + 2]) && 
+                                isdigit(outbuf[i + 3]) && 
+                                isdigit(outbuf[i + 4]) &&
+                                (outbuf[i + 5] == ';') ) 
+                       {
                                scanch = 0;
                                sscanf(&outbuf[i+2], "%03d", &scanch);
                                outbuf[i] = scanch;
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
                        }
 
+                       // four-digit decimal equivalents
+                       else if (outbuf[i] == '&'       &&
+                                outbuf[i + 1] == '#'   &&
+                                isdigit(outbuf[i + 2]) && 
+                                isdigit(outbuf[i + 3]) && 
+                                isdigit(outbuf[i + 4]) &&
+                                isdigit(outbuf[i + 5]) &&
+                                (outbuf[i + 6] == ';') ) 
+                       {
+                               scanch = 0;
+                               sscanf(&outbuf[i+2], "%04d", &scanch);
+                               outbuf[i] = scanch;
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
                }
 
-               /* Make sure the output buffer is big enough */
+               // Make sure the output buffer is big enough
                if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
                        outptr_buffer_size += SIZ;
                        outptr = realloc(outptr, outptr_buffer_size);
@@ -498,7 +533,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                        }
                }
 
-               /* Output any lines terminated with hard line breaks */
+               // Output any lines terminated with hard line breaks
                do {
                        did_out = 0;
                        if (strlen(outbuf) > 0) {
@@ -508,11 +543,6 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                        strncpy(&outptr[output_len], outbuf, i+1);
                                        output_len += (i+1);
 
-                                       if (do_citaformat) {
-                                               strcpy(&outptr[output_len], " ");
-                                               ++output_len;
-                                       }
-
                                        strcpy(outbuf, &outbuf[i+1]);
                                        i = 0;
                                        did_out = 1;
@@ -521,7 +551,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                    }
                } while (did_out);
 
-               /* Add soft line breaks */
+               // Add soft line breaks
                if (strlen(outbuf) > (screenwidth - 2 )) {
                        rb = (-1);
                        for (i=0; i<(screenwidth-2); ++i) {
@@ -532,21 +562,13 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                output_len += rb;
                                strcpy(&outptr[output_len], nl);
                                output_len += strlen(nl);
-                               if (do_citaformat) {
-                                       strcpy(&outptr[output_len], " ");
-                                       ++output_len;
-                               }
                                strcpy(outbuf, &outbuf[rb+1]);
-                       } else {
-                               strncpy(&outptr[output_len], outbuf,
-                                       screenwidth-2);
+                       }
+                       else {
+                               strncpy(&outptr[output_len], outbuf, screenwidth-2);
                                output_len += (screenwidth-2);
                                strcpy(&outptr[output_len], nl);
                                output_len += strlen(nl);
-                               if (do_citaformat) {
-                                       strcpy(&outptr[output_len], " ");
-                                       ++output_len;
-                               }
                                strcpy(outbuf, &outbuf[screenwidth-2]);
                        }
                }
@@ -556,9 +578,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
        strcpy(&outptr[output_len], outbuf);
        output_len += strlen(outbuf);
 
-       /* Strip leading/trailing whitespace.  We can't do this with
-        * striplt() because it uses too many strlen()'s
-        */
+       // Strip leading/trailing whitespace.
        while ((output_len > 0) && (isspace(outptr[0]))) {
                strcpy(outptr, &outptr[1]);
                --output_len;
@@ -568,6 +588,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                --output_len;
        }
 
+       // Make sure the final line ends with a newline character.
        if ((output_len > 0) && (outptr[output_len-1] != '\n')) {
                strcat(outptr, "\n");
                ++output_len;