]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/html_to_ascii.c
Added a new parameter to html_to_ascii() to let it know when it's rendering to a...
[citadel.git] / libcitadel / lib / html_to_ascii.c
index 6e4600ab19169317949538f7295e0404a4807268..2d604ac4fa3ab250519e022fca2d988f8bf3aaaf 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 open source 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-2022 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 "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(const 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];
@@ -60,13 +47,14 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
        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, "");
@@ -197,6 +185,9 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                        strcat(outbuf, nl);
                                }
 
+#if 0
+       These seemed like a good idea at the time, but it just makes a mess.
+
                                else if (
                                        (!strcasecmp(tag, "B"))
                                        || (!strcasecmp(tag, "/B"))
@@ -204,7 +195,6 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                        || (!strcasecmp(tag, "/STRONG"))
                                ) {
                                        strcat(outbuf, "*");
-                                       
                                }
 
                                else if (
@@ -214,7 +204,6 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                        || (!strcasecmp(tag, "/EM"))
                                ) {
                                        strcat(outbuf, "/");
-                                       
                                }
 
                                else if (
@@ -222,8 +211,8 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                        || (!strcasecmp(tag, "/U"))
                                ) {
                                        strcat(outbuf, "_");
-                                       
                                }
+#endif
 
                                else if (!strcasecmp(tag, "BR")) {
                                        strcat(outbuf, nl);
@@ -240,6 +229,9 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                else if (!strcasecmp(tag, "BLOCKQUOTE")) {
                                        ++blockquote;
                                        strcpy(nl, "\n");
+                                       if ( (blockquote == 1) && (ansi) ) {
+                                               strcat(nl, "\033[2m\033[3m");
+                                       }
                                        for (j=0; j<blockquote; ++j) strcat(nl, ">");
                                        strcat(outbuf, nl);
                                }
@@ -247,6 +239,9 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
                                        strcat(outbuf, "\n");
                                        --blockquote;
+                                       if ( (blockquote == 0) && (ansi) ) {
+                                               strcat(outbuf, "\033[22m\033[23m");
+                                       }
                                        strcpy(nl, "\n");
                                        for (j=0; j<blockquote; ++j) strcat(nl, ">");
                                        strcat(outbuf, nl);
@@ -273,7 +268,7 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                tag[strlen(tag)] = ch;
                        }
                                
-                       else if (!nest) {
+                       else if ((!nest) && (styletag == 0)) {
                                outbuf[strlen(outbuf)+1] = 0;
                                outbuf[strlen(outbuf)] = ch;
                        }
@@ -542,11 +537,6 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                        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;
@@ -566,10 +556,6 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                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,
@@ -577,10 +563,6 @@ char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int do_ci
                                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]);
                        }
                }