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")) {
212 for (j=0; j<blockquote; ++j) strcat(nl, ">");
216 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
217 strcat(outbuf, "\n");
220 for (j=0; j<blockquote; ++j) strcat(nl, ">");
226 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
227 tag[strlen(tag)+1] = 0;
228 tag[strlen(tag)] = ch;
232 outbuf[strlen(outbuf)+1] = 0;
233 outbuf[strlen(outbuf)] = ch;
236 strcpy(inbuf, &inbuf[i]);
239 /* Convert &; tags to the forbidden characters */
240 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
242 /* Character entity references */
243 if (!strncasecmp(&outbuf[i], " ", 6)) {
245 strcpy(&outbuf[i+1], &outbuf[i+6]);
248 if (!strncasecmp(&outbuf[i], " ", 6)) {
250 strcpy(&outbuf[i+1], &outbuf[i+6]);
253 if (!strncasecmp(&outbuf[i], " ", 6)) {
255 strcpy(&outbuf[i+1], &outbuf[i+6]);
258 if (!strncasecmp(&outbuf[i], " ", 8)) {
260 strcpy(&outbuf[i+1], &outbuf[i+8]);
263 else if (!strncasecmp(&outbuf[i], "<", 4)) {
265 strcpy(&outbuf[i+1], &outbuf[i+4]);
268 else if (!strncasecmp(&outbuf[i], ">", 4)) {
270 strcpy(&outbuf[i+1], &outbuf[i+4]);
273 else if (!strncasecmp(&outbuf[i], "&", 5)) {
274 strcpy(&outbuf[i+1], &outbuf[i+5]);
277 else if (!strncasecmp(&outbuf[i], """, 6)) {
279 strcpy(&outbuf[i+1], &outbuf[i+6]);
282 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
284 strcpy(&outbuf[i+1], &outbuf[i+7]);
287 else if (!strncasecmp(&outbuf[i], "’", 7)) {
289 strcpy(&outbuf[i+1], &outbuf[i+7]);
292 else if (!strncasecmp(&outbuf[i], "©", 6)) {
296 strcpy(&outbuf[i+3], &outbuf[i+6]);
299 else if (!strncasecmp(&outbuf[i], "…", 8)) {
303 strcpy(&outbuf[i+3], &outbuf[i+8]);
306 else if (!strncasecmp(&outbuf[i], "™", 7)) {
311 strcpy(&outbuf[i+4], &outbuf[i+7]);
314 else if (!strncasecmp(&outbuf[i], "®", 5)) {
318 strcpy(&outbuf[i+3], &outbuf[i+5]);
321 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
325 strcpy(&outbuf[i+3], &outbuf[i+8]);
328 else if (!strncasecmp(&outbuf[i], "½", 8)) {
332 strcpy(&outbuf[i+3], &outbuf[i+8]);
335 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
339 strcpy(&outbuf[i+3], &outbuf[i+8]);
342 else if (!strncasecmp(&outbuf[i], "–", 7)) {
345 strcpy(&outbuf[i+2], &outbuf[i+7]);
348 else if (!strncasecmp(&outbuf[i], "—", 7)) {
352 strcpy(&outbuf[i+3], &outbuf[i+7]);
355 /* two-digit decimal equivalents */
356 else if ((!strncmp(&outbuf[i], "&#", 2))
357 && (outbuf[i+4] == ';') ) {
359 sscanf(&outbuf[i+2], "%02d", &scanch);
361 strcpy(&outbuf[i+1], &outbuf[i+5]);
364 /* three-digit decimal equivalents */
365 else if ((!strncmp(&outbuf[i], "&#", 2))
366 && (outbuf[i+5] == ';') ) {
368 sscanf(&outbuf[i+2], "%03d", &scanch);
370 strcpy(&outbuf[i+1], &outbuf[i+6]);
375 /* Make sure the output buffer is big enough */
376 if ((output_len + strlen(outbuf) + SIZ)
377 > outptr_buffer_size) {
378 outptr_buffer_size += SIZ;
379 outptr = realloc(outptr, outptr_buffer_size);
382 /* Output any lines terminated with hard line breaks */
385 if (strlen(outbuf)>0) {
386 for (i = 0; i<strlen(outbuf); ++i) {
387 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
389 strncpy(&outptr[output_len],
394 strcpy(&outptr[output_len],
399 strcpy(outbuf, &outbuf[i+1]);
407 /* Add soft line breaks */
408 if (strlen(outbuf) > (screenwidth - 2 )) {
410 for (i=0; i<(screenwidth-2); ++i) {
411 if (outbuf[i]==32) rb = i;
414 strncpy(&outptr[output_len], outbuf, rb);
416 strcpy(&outptr[output_len], nl);
417 output_len += strlen(nl);
419 strcpy(&outptr[output_len], " ");
422 strcpy(outbuf, &outbuf[rb+1]);
424 strncpy(&outptr[output_len], outbuf,
426 output_len += (screenwidth-2);
427 strcpy(&outptr[output_len], nl);
428 output_len += strlen(nl);
430 strcpy(&outptr[output_len], " ");
433 strcpy(outbuf, &outbuf[screenwidth-2]);
437 } while (done_reading == 0);
439 strcpy(&outptr[output_len], outbuf);
440 output_len += strlen(outbuf);
442 /* Strip leading/trailing whitespace. We can't do this with
443 * striplt() because it uses too many strlen()'s
445 while ((output_len > 0) && (isspace(outptr[0]))) {
446 strcpy(outptr, &outptr[1]);
449 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
450 outptr[output_len-1] = 0;
454 if (outptr[output_len-1] != '\n') {
455 strcat(outptr, "\n");