4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2005 by Art Cancro and others. This program is
6 * released under the terms of the GNU General Public License.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
33 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
45 * Convert HTML to plain text.
47 * inputmsg = pointer to raw HTML message
48 * screenwidth = desired output screenwidth
49 * do_citaformat = set to 1 to indent newlines with spaces
51 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
58 size_t outptr_buffer_size;
59 size_t output_len = 0;
60 int i, j, ch, did_out, rb, scanch;
61 int nest = 0; /* Bracket nesting level */
62 int blockquote = 0; /* BLOCKQUOTE nesting level */
63 int bytes_processed = 0;
70 if (msglen == 0) msglen = strlen(inputmsg);
72 outptr_buffer_size = strlen(inptr) + SIZ;
73 outptr = malloc(outptr_buffer_size);
74 if (outptr == NULL) return NULL;
79 /* Fill the input buffer */
80 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
84 inbuf[strlen(inbuf)+1] = 0;
85 inbuf[strlen(inbuf)] = ch;
92 if (bytes_processed > msglen) {
99 if (strlen(inbuf)>0) {
101 /* Fold in all the spacing */
102 for (i=0; i<strlen(inbuf); ++i) {
103 if (inbuf[i]==10) inbuf[i]=32;
104 if (inbuf[i]==13) inbuf[i]=32;
105 if (inbuf[i]==9) inbuf[i]=32;
106 /*** we like foreign characters now.
107 if ((inbuf[i]<32) || (inbuf[i]>126)) {
111 for (i=0; i<strlen(inbuf); ++i) {
112 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
113 strcpy(&inbuf[i], &inbuf[i+1]);
116 for (i=0; i<strlen(inbuf); ++i) {
125 else if (ch == '>') { /* We have a tag. */
126 if (nest > 0) --nest;
128 /* Unqualify the tag (truncate at first space) */
129 if (strchr(tag, ' ') != NULL) {
130 strcpy(strchr(tag, ' '), "");
133 if (!strcasecmp(tag, "P")) {
138 if (!strcasecmp(tag, "/DIV")) {
143 if (!strcasecmp(tag, "LI")) {
145 strcat(outbuf, " * ");
148 else if (!strcasecmp(tag, "/UL")) {
153 else if (!strcasecmp(tag, "H1")) {
158 else if (!strcasecmp(tag, "H2")) {
163 else if (!strcasecmp(tag, "H3")) {
168 else if (!strcasecmp(tag, "H4")) {
173 else if (!strcasecmp(tag, "/H1")) {
177 else if (!strcasecmp(tag, "/H2")) {
181 else if (!strcasecmp(tag, "/H3")) {
185 else if (!strcasecmp(tag, "/H4")) {
189 else if (!strcasecmp(tag, "HR")) {
192 for (j=0; j<screenwidth-2; ++j)
197 else if (!strcasecmp(tag, "BR")) {
201 else if (!strcasecmp(tag, "TR")) {
205 else if (!strcasecmp(tag, "/TABLE")) {
209 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
213 for (j=0; j<blockquote; ++j) strcat(nl, ">");
217 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
218 strcat(outbuf, "\n");
221 for (j=0; j<blockquote; ++j) strcat(nl, ">");
228 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
229 tag[strlen(tag)+1] = 0;
230 tag[strlen(tag)] = ch;
234 outbuf[strlen(outbuf)+1] = 0;
235 outbuf[strlen(outbuf)] = ch;
238 strcpy(inbuf, &inbuf[i]);
241 /* Convert &; tags to the forbidden characters */
242 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
244 /* Character entity references */
245 if (!strncasecmp(&outbuf[i], " ", 6)) {
247 strcpy(&outbuf[i+1], &outbuf[i+6]);
250 if (!strncasecmp(&outbuf[i], " ", 6)) {
252 strcpy(&outbuf[i+1], &outbuf[i+6]);
255 if (!strncasecmp(&outbuf[i], " ", 6)) {
257 strcpy(&outbuf[i+1], &outbuf[i+6]);
260 if (!strncasecmp(&outbuf[i], " ", 8)) {
262 strcpy(&outbuf[i+1], &outbuf[i+8]);
265 else if (!strncasecmp(&outbuf[i], "<", 4)) {
267 strcpy(&outbuf[i+1], &outbuf[i+4]);
270 else if (!strncasecmp(&outbuf[i], ">", 4)) {
272 strcpy(&outbuf[i+1], &outbuf[i+4]);
275 else if (!strncasecmp(&outbuf[i], "&", 5)) {
276 strcpy(&outbuf[i+1], &outbuf[i+5]);
279 else if (!strncasecmp(&outbuf[i], """, 6)) {
281 strcpy(&outbuf[i+1], &outbuf[i+6]);
284 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
286 strcpy(&outbuf[i+1], &outbuf[i+7]);
289 else if (!strncasecmp(&outbuf[i], "’", 7)) {
291 strcpy(&outbuf[i+1], &outbuf[i+7]);
294 else if (!strncasecmp(&outbuf[i], "©", 6)) {
298 strcpy(&outbuf[i+3], &outbuf[i+6]);
301 else if (!strncasecmp(&outbuf[i], "…", 8)) {
305 strcpy(&outbuf[i+3], &outbuf[i+8]);
308 else if (!strncasecmp(&outbuf[i], "™", 7)) {
313 strcpy(&outbuf[i+4], &outbuf[i+7]);
316 else if (!strncasecmp(&outbuf[i], "®", 5)) {
320 strcpy(&outbuf[i+3], &outbuf[i+5]);
323 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
327 strcpy(&outbuf[i+3], &outbuf[i+8]);
330 else if (!strncasecmp(&outbuf[i], "½", 8)) {
334 strcpy(&outbuf[i+3], &outbuf[i+8]);
337 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
341 strcpy(&outbuf[i+3], &outbuf[i+8]);
344 else if (!strncasecmp(&outbuf[i], "–", 7)) {
347 strcpy(&outbuf[i+2], &outbuf[i+7]);
350 else if (!strncasecmp(&outbuf[i], "—", 7)) {
354 strcpy(&outbuf[i+3], &outbuf[i+7]);
357 /* two-digit decimal equivalents */
358 else if ((!strncmp(&outbuf[i], "&#", 2))
359 && (outbuf[i+4] == ';') ) {
361 sscanf(&outbuf[i+2], "%02d", &scanch);
363 strcpy(&outbuf[i+1], &outbuf[i+5]);
366 /* three-digit decimal equivalents */
367 else if ((!strncmp(&outbuf[i], "&#", 2))
368 && (outbuf[i+5] == ';') ) {
370 sscanf(&outbuf[i+2], "%03d", &scanch);
372 strcpy(&outbuf[i+1], &outbuf[i+6]);
377 /* Make sure the output buffer is big enough */
378 if ((output_len + strlen(outbuf) + SIZ)
379 > outptr_buffer_size) {
380 outptr_buffer_size += SIZ;
381 outptr = realloc(outptr, outptr_buffer_size);
384 /* Output any lines terminated with hard line breaks */
387 if (strlen(outbuf)>0) {
388 for (i = 0; i<strlen(outbuf); ++i) {
389 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
391 strncpy(&outptr[output_len],
396 strcpy(&outptr[output_len],
401 strcpy(outbuf, &outbuf[i+1]);
409 /* Add soft line breaks */
410 if (strlen(outbuf) > (screenwidth - 2 )) {
412 for (i=0; i<(screenwidth-2); ++i) {
413 if (outbuf[i]==32) rb = i;
416 strncpy(&outptr[output_len], outbuf, rb);
418 strcpy(&outptr[output_len], nl);
419 output_len += strlen(nl);
421 strcpy(&outptr[output_len], " ");
424 strcpy(outbuf, &outbuf[rb+1]);
426 strncpy(&outptr[output_len], outbuf,
428 output_len += (screenwidth-2);
429 strcpy(&outptr[output_len], nl);
430 output_len += strlen(nl);
432 strcpy(&outptr[output_len], " ");
435 strcpy(outbuf, &outbuf[screenwidth-2]);
439 } while (done_reading == 0);
441 strcpy(&outptr[output_len], outbuf);
442 output_len += strlen(outbuf);
444 /* Strip leading/trailing whitespace. We can't do this with
445 * striplt() because it uses too many strlen()'s
447 while ((output_len > 0) && (isspace(outptr[0]))) {
448 strcpy(outptr, &outptr[1]);
451 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
452 outptr[output_len-1] = 0;
456 if (outptr[output_len-1] != '\n') {
457 strcat(outptr, "\n");