// Functions which handle translation between HTML and plain text
-// Copyright (c) 2000-2022 by the citadel.org team
+// 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 <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"
output_len = 0;
do {
- /* Fill the input buffer */
+ // Fill the input buffer
inbuf_len = strlen(inbuf);
if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
}
- /* 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;
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, ' '), "");
}
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"))
|| (!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");
+ }
}
-#endif
else if (!strcasecmp(tag, "BR")) {
strcat(outbuf, nl);
++blockquote;
strcpy(nl, "\n");
if ( (blockquote == 1) && (ansi) ) {
- strcat(nl, "\033[2m\033[3m");
+ strcat(nl, "\033[2m\033[2m");
}
for (j=0; j<blockquote; ++j) strcat(nl, ">");
strcat(outbuf, nl);
strcat(outbuf, "\n");
--blockquote;
if ( (blockquote == 0) && (ansi) ) {
- strcat(outbuf, "\033[22m\033[23m");
+ strcat(outbuf, "\033[22m\033[22m");
}
strcpy(nl, "\n");
for (j=0; j<blockquote; ++j) strcat(nl, ">");
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], " ", 6)) {
outbuf[i] = ' ';
strcpy(&outbuf[i+1], &outbuf[i+6]);
strcpy(&outbuf[i+1], &outbuf[i+7]);
}
- /* two-digit decimal equivalents */
+ // two-digit decimal equivalents
else if (outbuf[i] == '&' &&
outbuf[i + 1] == '#' &&
isdigit(outbuf[i + 2]) &&
strcpy(&outbuf[i+1], &outbuf[i+5]);
}
- /* three-digit decimal equivalents */
+ // three-digit decimal equivalents
else if (outbuf[i] == '&' &&
outbuf[i + 1] == '#' &&
isdigit(outbuf[i + 2]) &&
strcpy(&outbuf[i+1], &outbuf[i+6]);
}
- /* four-digit decimal equivalents */
+ // four-digit decimal equivalents
else if (outbuf[i] == '&' &&
outbuf[i + 1] == '#' &&
isdigit(outbuf[i + 2]) &&
}
- /* 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);
}
}
- /* Output any lines terminated with hard line breaks */
+ // Output any lines terminated with hard line breaks
do {
did_out = 0;
if (strlen(outbuf) > 0) {
}
} 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) {
strcpy(&outptr[output_len], nl);
output_len += strlen(nl);
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);
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;
--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;