4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2001 by Art Cancro and others. This program is
6 * released under the terms of the GNU General Public License.
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
37 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
49 * Convert HTML to plain text.
51 * inputmsg = pointer to raw HTML message
52 * screenwidth = desired output screenwidth
53 * do_citaformat = set to 1 to indent newlines with spaces
55 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
62 size_t outptr_buffer_size;
63 size_t output_len = 0;
64 int i, j, ch, did_out, rb, scanch;
65 int nest = 0; /* Bracket nesting level */
66 int blockquote = 0; /* BLOCKQUOTE nesting level */
67 int bytes_processed = 0;
72 if (msglen == 0) msglen = strlen(inputmsg);
74 outptr_buffer_size = strlen(inptr) + SIZ;
75 outptr = malloc(outptr_buffer_size);
76 if (outptr == NULL) return NULL;
81 /* Fill the input buffer */
82 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
86 inbuf[strlen(inbuf)+1] = 0;
87 inbuf[strlen(inbuf)] = ch;
94 if (bytes_processed > msglen) {
100 /* Do some parsing */
101 if (strlen(inbuf)>0) {
103 /* Fold in all the spacing */
104 for (i=0; i<strlen(inbuf); ++i) {
105 if (inbuf[i]==10) inbuf[i]=32;
106 if (inbuf[i]==13) inbuf[i]=32;
107 if (inbuf[i]==9) inbuf[i]=32;
108 /*** we like foreign characters now.
109 if ((inbuf[i]<32) || (inbuf[i]>126)) {
113 for (i=0; i<strlen(inbuf); ++i) {
114 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
115 strcpy(&inbuf[i], &inbuf[i+1]);
118 for (i=0; i<strlen(inbuf); ++i) {
127 else if (ch == '>') { /* We have a tag. */
128 if (nest > 0) --nest;
130 /* Unqualify the tag (truncate at first space) */
131 if (strchr(tag, ' ') != NULL) {
132 strcpy(strchr(tag, ' '), "");
135 if (!strcasecmp(tag, "P")) {
136 strcat(outbuf, "\n\n");
139 if (!strcasecmp(tag, "/DIV")) {
140 strcat(outbuf, "\n\n");
143 if (!strcasecmp(tag, "LI")) {
144 strcat(outbuf, "\n * ");
147 else if (!strcasecmp(tag, "/UL")) {
148 strcat(outbuf, "\n\n");
151 else if (!strcasecmp(tag, "H1")) {
152 strcat(outbuf, "\n\n");
155 else if (!strcasecmp(tag, "H2")) {
156 strcat(outbuf, "\n\n");
159 else if (!strcasecmp(tag, "H3")) {
160 strcat(outbuf, "\n\n");
163 else if (!strcasecmp(tag, "H4")) {
164 strcat(outbuf, "\n\n");
167 else if (!strcasecmp(tag, "/H1")) {
168 strcat(outbuf, "\n");
171 else if (!strcasecmp(tag, "/H2")) {
172 strcat(outbuf, "\n");
175 else if (!strcasecmp(tag, "/H3")) {
176 strcat(outbuf, "\n");
179 else if (!strcasecmp(tag, "/H4")) {
180 strcat(outbuf, "\n");
183 else if (!strcasecmp(tag, "HR")) {
184 strcat(outbuf, "\n ");
185 for (j=0; j<screenwidth-2; ++j)
187 strcat(outbuf, "\n");
190 else if (!strcasecmp(tag, "BR")) {
191 strcat(outbuf, "\n");
194 else if (!strcasecmp(tag, "TR")) {
195 strcat(outbuf, "\n");
198 else if (!strcasecmp(tag, "/TABLE")) {
199 strcat(outbuf, "\n");
202 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
203 strcat(outbuf, "\n\n <<\n");
207 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
208 strcat(outbuf, "\n >>\n\n");
214 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
215 tag[strlen(tag)+1] = 0;
216 tag[strlen(tag)] = ch;
220 outbuf[strlen(outbuf)+1] = 0;
221 outbuf[strlen(outbuf)] = ch;
224 strcpy(inbuf, &inbuf[i]);
227 /* Convert &; tags to the forbidden characters */
228 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
230 /* Character entity references */
231 if (!strncasecmp(&outbuf[i], " ", 6)) {
233 strcpy(&outbuf[i+1], &outbuf[i+6]);
236 if (!strncasecmp(&outbuf[i], " ", 6)) {
238 strcpy(&outbuf[i+1], &outbuf[i+6]);
241 if (!strncasecmp(&outbuf[i], " ", 6)) {
243 strcpy(&outbuf[i+1], &outbuf[i+6]);
246 if (!strncasecmp(&outbuf[i], " ", 8)) {
248 strcpy(&outbuf[i+1], &outbuf[i+8]);
251 else if (!strncasecmp(&outbuf[i], "<", 4)) {
253 strcpy(&outbuf[i+1], &outbuf[i+4]);
256 else if (!strncasecmp(&outbuf[i], ">", 4)) {
258 strcpy(&outbuf[i+1], &outbuf[i+4]);
261 else if (!strncasecmp(&outbuf[i], "&", 5)) {
262 strcpy(&outbuf[i+1], &outbuf[i+5]);
265 else if (!strncasecmp(&outbuf[i], """, 6)) {
267 strcpy(&outbuf[i+1], &outbuf[i+6]);
270 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
272 strcpy(&outbuf[i+1], &outbuf[i+7]);
275 else if (!strncasecmp(&outbuf[i], "’", 7)) {
277 strcpy(&outbuf[i+1], &outbuf[i+7]);
280 else if (!strncasecmp(&outbuf[i], "©", 6)) {
284 strcpy(&outbuf[i+3], &outbuf[i+6]);
287 else if (!strncasecmp(&outbuf[i], "…", 8)) {
291 strcpy(&outbuf[i+3], &outbuf[i+8]);
294 else if (!strncasecmp(&outbuf[i], "™", 7)) {
299 strcpy(&outbuf[i+4], &outbuf[i+7]);
302 else if (!strncasecmp(&outbuf[i], "®", 5)) {
306 strcpy(&outbuf[i+3], &outbuf[i+5]);
309 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
313 strcpy(&outbuf[i+3], &outbuf[i+8]);
316 else if (!strncasecmp(&outbuf[i], "½", 8)) {
320 strcpy(&outbuf[i+3], &outbuf[i+8]);
323 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
327 strcpy(&outbuf[i+3], &outbuf[i+8]);
330 else if (!strncasecmp(&outbuf[i], "–", 7)) {
333 strcpy(&outbuf[i+2], &outbuf[i+7]);
336 else if (!strncasecmp(&outbuf[i], "—", 7)) {
340 strcpy(&outbuf[i+3], &outbuf[i+7]);
343 /* two-digit decimal equivalents */
344 else if ((!strncmp(&outbuf[i], "&#", 2))
345 && (outbuf[i+4] == ';') ) {
347 sscanf(&outbuf[i+2], "%02d", &scanch);
349 strcpy(&outbuf[i+1], &outbuf[i+5]);
352 /* three-digit decimal equivalents */
353 else if ((!strncmp(&outbuf[i], "&#", 2))
354 && (outbuf[i+5] == ';') ) {
356 sscanf(&outbuf[i+2], "%03d", &scanch);
358 strcpy(&outbuf[i+1], &outbuf[i+6]);
363 /* Make sure the output buffer is big enough */
364 if ((output_len + strlen(outbuf) + SIZ)
365 > outptr_buffer_size) {
366 outptr_buffer_size += SIZ;
367 outptr = realloc(outptr, outptr_buffer_size);
370 /* Output any lines terminated with hard line breaks */
373 if (strlen(outbuf)>0) {
374 for (i = 0; i<strlen(outbuf); ++i) {
375 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
377 strncpy(&outptr[output_len],
382 strcpy(&outptr[output_len],
387 strcpy(outbuf, &outbuf[i+1]);
395 /* Add soft line breaks */
396 if (strlen(outbuf) > (screenwidth - 2 )) {
398 for (i=0; i<(screenwidth-2); ++i) {
399 if (outbuf[i]==32) rb = i;
402 strncpy(&outptr[output_len], outbuf, rb);
404 strcpy(&outptr[output_len], "\n");
407 strcpy(&outptr[output_len], " ");
410 strcpy(outbuf, &outbuf[rb+1]);
412 strncpy(&outptr[output_len], outbuf,
414 output_len += (screenwidth-2);
415 strcpy(&outptr[output_len], "\n");
418 strcpy(&outptr[output_len], " ");
421 strcpy(outbuf, &outbuf[screenwidth-2]);
425 } while (done_reading == 0);
427 strcpy(&outptr[output_len], outbuf);
428 output_len += strlen(outbuf);
430 /* Strip leading/trailing whitespace. We can't do this with
431 * striplt() because it uses too many strlen()'s
433 while ((output_len > 0) && (isspace(outptr[0]))) {
434 strcpy(outptr, &outptr[1]);
437 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
438 outptr[output_len-1] = 0;
442 if (outptr[output_len-1] != '\n') {
443 strcat(outptr, "\n");