#include <string.h>
#include <errno.h>
#include <limits.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
-#endif
#include <syslog.h>
#include "citadel.h"
#include "server.h"
#include "tools.h"
#include "room_ops.h"
#include "html.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 screenwidth) {
+char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
char inbuf[256];
char outbuf[256];
char tag[1024];
int done_reading = 0;
- char *readptr, *outputbuf;
- size_t input_length, this_read, output_length;
- int i, ch, did_out, rb;
+ char *inptr;
+ char *outptr;
+ size_t outlen;
+ int i, j, ch, did_out, rb;
int nest = 0; /* Bracket nesting level */
- input_length = strlen(inputmsg);
- readptr = inputmsg;
- output_length = strlen(inputmsg);
- outputbuf = mallok(output_length);
- if (outputbuf==NULL) return NULL;
+ inptr = inputmsg;
strcpy(inbuf, "");
strcpy(outbuf, "");
- lprintf(9, "Decoding %d bytes of HTML\n", input_length);
+ outptr = mallok(strlen(inptr) + 256);
+ if (outptr == NULL) return NULL;
+ strcpy(outptr, "");
+ outlen = 0;
do {
/* Fill the input buffer */
if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
- /* copy from the input buffer */
- lprintf(9, "input loop\n");
- this_read = strlen(readptr);
- if (this_read > 127) this_read = 127;
- lprintf(9, "%d bytes\n", this_read);
- for (i=0; i<this_read; ++i) {
+ /* FIX ... genericize this */
+ ch = *inputmsg++;
+ if (ch > 0) {
inbuf[strlen(inbuf)+1] = 0;
- inbuf[strlen(inbuf)] = readptr[0];
- ++readptr;
+ inbuf[strlen(inbuf)] = ch;
+ }
+ else {
+ done_reading = 1;
}
- if (strlen(readptr)==0) done_reading = 1;
- }
- else {
- lprintf(9, "skipped input loop\n");
}
/* Do some parsing */
- lprintf(9, "parse loop\n");
if (strlen(inbuf)>0) {
/* Fold in all the spacing */
- lprintf(9, "spacing loop\n");
for (i=0; i<strlen(inbuf); ++i) {
if (inbuf[i]==10) inbuf[i]=32;
if (inbuf[i]==13) inbuf[i]=32;
strcpy(&inbuf[i], &inbuf[i+1]);
}
- lprintf(9, "foo loop\n");
for (i=0; i<strlen(inbuf); ++i) {
ch = inbuf[i];
if (ch == '<') {
- lprintf(9, "bar loop\n");
++nest;
strcpy(tag, "");
}
else if (ch == '>') {
- lprintf(9, "baz loop\n");
if (nest > 0) --nest;
if (!strcasecmp(tag, "P")) {
strcat(outbuf, "\n\n");
}
+ if (!strcasecmp(tag, "/DIV")) {
+ strcat(outbuf, "\n\n");
+ }
+
else if (!strcasecmp(tag, "H1")) {
strcat(outbuf, "\n\n");
}
else if (!strcasecmp(tag, "HR")) {
strcat(outbuf, "\n ");
- for (i=0; i<screenwidth-2; ++i)
+ for (j=0; j<screenwidth-2; ++j)
strcat(outbuf, "-");
strcat(outbuf, "\n");
}
strcpy(inbuf, &inbuf[i]);
}
- lprintf(9, "checkquepoynte\n");
-
/* Convert &; tags to the forbidden characters */
if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
- lprintf(9, "eek loop\n");
if (!strncasecmp(&outbuf[i], " ", 6)) {
outbuf[i] = ' ';
strcpy(&outbuf[i+1], &outbuf[i+6]);
}
- else if (!strncasecmp(&outbuf[i], "&lb;", 4)) {
+ else if (!strncasecmp(&outbuf[i], "<", 4)) {
outbuf[i] = '<';
strcpy(&outbuf[i+1], &outbuf[i+4]);
}
- else if (!strncasecmp(&outbuf[i], "&rb;", 4)) {
+ else if (!strncasecmp(&outbuf[i], ">", 4)) {
outbuf[i] = '>';
strcpy(&outbuf[i+1], &outbuf[i+4]);
}
strcpy(&outbuf[i+1], &outbuf[i+6]);
}
+ else if (!strncasecmp(&outbuf[i], "©", 6)) {
+ outbuf[i] = '(';
+ outbuf[i+1] = 'c';
+ outbuf[i+2] = ')';
+ strcpy(&outbuf[i+3], &outbuf[i+6]);
+ }
+
+ else if (!strncasecmp(&outbuf[i], "®", 5)) {
+ outbuf[i] = '(';
+ outbuf[i+1] = 'r';
+ outbuf[i+2] = ')';
+ strcpy(&outbuf[i+3], &outbuf[i+5]);
+ }
+
}
/* Make sure the output buffer is big enough */
- if ((strlen(outputbuf) + strlen(outbuf) + 2) > output_length) {
- lprintf(9, "realloc loop\n");
- output_length = output_length + strlen(outbuf) + 2;
- outputbuf = reallok(outputbuf, output_length);
+ if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
+ outlen += 128;
+ outptr = realloc(outptr, outlen);
}
/* Output any lines terminated with hard line breaks */
- lprintf(9, "output loop 1\n");
do {
did_out = 0;
if (strlen(outbuf)>0)
for (i = 0; i<strlen(outbuf); ++i) {
if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
- strncat(outputbuf, outbuf, i+1);
+ strncat(outptr, outbuf, i+1);
+ strcat(outptr, "\n");
+ if (do_citaformat)
+ strcat(outptr, " ");
strcpy(outbuf, &outbuf[i+1]);
i = 0;
did_out = 1;
} while (did_out);
/* Add soft line breaks */
- lprintf(9, "output loop 2\n");
if (strlen(outbuf) > (screenwidth - 2)) {
rb = (-1);
for (i=0; i<(screenwidth-2); ++i) {
if (outbuf[i]==32) rb = i;
}
if (rb>=0) {
- strncat(outputbuf, outbuf, rb);
- strcat(outputbuf, "\n");
+ strncat(outptr, outbuf, rb);
+ strcat(outptr, "\n");
+ if (do_citaformat)
+ strcat(outptr, " ");
strcpy(outbuf, &outbuf[rb+1]);
} else {
- strncat(outputbuf, outbuf, screenwidth-2);
- strcat(outputbuf, "\n");
+
+ strncat(outptr, outbuf, screenwidth-2);
+ strcat(outptr, "\n");
+ if (do_citaformat)
+ strcat(outptr, " ");
strcpy(outbuf, &outbuf[screenwidth-2]);
}
}
} while (done_reading == 0);
- lprintf(9, "output loop 3\n");
- strncat(outputbuf, outbuf, strlen(outbuf));
- strcat(outputbuf, "\n");
- return outputbuf;
+ strcat(outptr, outbuf);
+ strcat(outptr, "\n");
+
+ return outptr;
+
}
+