2 * $Id: html.c 6014 2008-02-04 18:38:35Z ajc $
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.
13 * Convert HTML to plain text.
15 * inputmsg = pointer to raw HTML message
16 * screenwidth = desired output screenwidth
17 * do_citaformat = set to 1 to indent newlines with spaces
19 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
27 size_t outptr_buffer_size;
28 size_t output_len = 0;
29 int i, j, ch, did_out, rb, scanch;
30 int nest = 0; /* Bracket nesting level */
31 int blockquote = 0; /* BLOCKQUOTE nesting level */
32 int styletag = 0; /* STYLE tag nesting level */
33 int styletag_start = 0;
34 int bytes_processed = 0;
41 if (msglen == 0) msglen = strlen(inputmsg);
43 outptr_buffer_size = strlen(inptr) + SIZ;
44 outptr = malloc(outptr_buffer_size);
45 if (outptr == NULL) return NULL;
50 /* Fill the input buffer */
51 inbuf_len = strlen(inbuf);
52 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
56 inbuf[inbuf_len++] = ch;
64 if (bytes_processed > msglen) {
71 if (!IsEmptyStr(inbuf)) {
74 /* Fold in all the spacing */
75 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
76 if (inbuf[i]==10) inbuf[i]=32;
77 if (inbuf[i]==13) inbuf[i]=32;
78 if (inbuf[i]==9) inbuf[i]=32;
79 /*** we like foreign characters now.
80 if ((inbuf[i]<32) || (inbuf[i]>126)) {
84 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
85 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
86 strcpy(&inbuf[i], &inbuf[i+1]);
89 for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
98 else if (ch == '>') { /* We have a tag. */
101 /* Unqualify the tag (truncate at first space) */
102 if (strchr(tag, ' ') != NULL) {
103 strcpy(strchr(tag, ' '), "");
106 if (!strcasecmp(tag, "P")) {
111 if (!strcasecmp(tag, "/DIV")) {
116 if (!strcasecmp(tag, "LI")) {
118 strcat(outbuf, " * ");
121 else if (!strcasecmp(tag, "/UL")) {
126 else if (!strcasecmp(tag, "H1")) {
131 else if (!strcasecmp(tag, "H2")) {
136 else if (!strcasecmp(tag, "H3")) {
141 else if (!strcasecmp(tag, "H4")) {
146 else if (!strcasecmp(tag, "/H1")) {
150 else if (!strcasecmp(tag, "/H2")) {
154 else if (!strcasecmp(tag, "/H3")) {
158 else if (!strcasecmp(tag, "/H4")) {
162 else if (!strcasecmp(tag, "HR")) {
165 for (j=0; j<screenwidth-2; ++j)
170 else if (!strcasecmp(tag, "BR")) {
174 else if (!strcasecmp(tag, "TR")) {
178 else if (!strcasecmp(tag, "/TABLE")) {
182 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
185 for (j=0; j<blockquote; ++j) strcat(nl, ">");
189 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
190 strcat(outbuf, "\n");
193 for (j=0; j<blockquote; ++j) strcat(nl, ">");
197 else if (!strcasecmp(tag, "STYLE")) {
200 styletag_start = strlen(outbuf);
204 else if (!strcasecmp(tag, "/STYLE")) {
207 outbuf[styletag_start] = 0;
213 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
214 tag[strlen(tag)+1] = 0;
215 tag[strlen(tag)] = ch;
219 outbuf[strlen(outbuf)+1] = 0;
220 outbuf[strlen(outbuf)] = ch;
223 strcpy(inbuf, &inbuf[i]);
226 /* Convert &; tags to the forbidden characters */
227 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
229 /* Character entity references */
230 if (!strncasecmp(&outbuf[i], " ", 6)) {
232 strcpy(&outbuf[i+1], &outbuf[i+6]);
235 if (!strncasecmp(&outbuf[i], " ", 6)) {
237 strcpy(&outbuf[i+1], &outbuf[i+6]);
240 if (!strncasecmp(&outbuf[i], " ", 6)) {
242 strcpy(&outbuf[i+1], &outbuf[i+6]);
245 if (!strncasecmp(&outbuf[i], " ", 8)) {
247 strcpy(&outbuf[i+1], &outbuf[i+8]);
250 else if (!strncasecmp(&outbuf[i], "<", 4)) {
252 strcpy(&outbuf[i+1], &outbuf[i+4]);
255 else if (!strncasecmp(&outbuf[i], ">", 4)) {
257 strcpy(&outbuf[i+1], &outbuf[i+4]);
260 else if (!strncasecmp(&outbuf[i], "&", 5)) {
261 strcpy(&outbuf[i+1], &outbuf[i+5]);
264 else if (!strncasecmp(&outbuf[i], """, 6)) {
266 strcpy(&outbuf[i+1], &outbuf[i+6]);
269 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
271 strcpy(&outbuf[i+1], &outbuf[i+7]);
274 else if (!strncasecmp(&outbuf[i], "’", 7)) {
276 strcpy(&outbuf[i+1], &outbuf[i+7]);
279 else if (!strncasecmp(&outbuf[i], "©", 6)) {
283 strcpy(&outbuf[i+3], &outbuf[i+6]);
286 else if (!strncasecmp(&outbuf[i], "•", 6)) {
290 strcpy(&outbuf[i+3], &outbuf[i+6]);
293 else if (!strncasecmp(&outbuf[i], "…", 8)) {
297 strcpy(&outbuf[i+3], &outbuf[i+8]);
300 else if (!strncasecmp(&outbuf[i], "™", 7)) {
305 strcpy(&outbuf[i+4], &outbuf[i+7]);
308 else if (!strncasecmp(&outbuf[i], "®", 5)) {
312 strcpy(&outbuf[i+3], &outbuf[i+5]);
315 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
319 strcpy(&outbuf[i+3], &outbuf[i+8]);
322 else if (!strncasecmp(&outbuf[i], "½", 8)) {
326 strcpy(&outbuf[i+3], &outbuf[i+8]);
329 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
333 strcpy(&outbuf[i+3], &outbuf[i+8]);
336 else if (!strncasecmp(&outbuf[i], "–", 7)) {
339 strcpy(&outbuf[i+2], &outbuf[i+7]);
342 else if (!strncasecmp(&outbuf[i], "—", 7)) {
346 strcpy(&outbuf[i+3], &outbuf[i+7]);
349 else if (!strncmp(&outbuf[i], "Ç", 8)) {
351 strcpy(&outbuf[i+1], &outbuf[i+8]);
354 else if (!strncasecmp(&outbuf[i], "ç", 8)) {
356 strcpy(&outbuf[i+1], &outbuf[i+8]);
359 else if (!strncmp(&outbuf[i], "È", 8)) {
361 strcpy(&outbuf[i+1], &outbuf[i+8]);
364 else if (!strncasecmp(&outbuf[i], "è", 8)) {
366 strcpy(&outbuf[i+1], &outbuf[i+8]);
369 else if (!strncmp(&outbuf[i], "Ê", 7)) {
371 strcpy(&outbuf[i+1], &outbuf[i+7]);
374 else if (!strncasecmp(&outbuf[i], "ê", 7)) {
376 strcpy(&outbuf[i+1], &outbuf[i+7]);
379 else if (!strncmp(&outbuf[i], "É", 8)) {
381 strcpy(&outbuf[i+1], &outbuf[i+8]);
384 else if (!strncasecmp(&outbuf[i], "é", 8)) {
386 strcpy(&outbuf[i+1], &outbuf[i+8]);
389 else if (!strncmp(&outbuf[i], "À", 8)) {
391 strcpy(&outbuf[i+1], &outbuf[i+8]);
394 else if (!strncasecmp(&outbuf[i], "à", 8)) {
396 strcpy(&outbuf[i+1], &outbuf[i+8]);
399 else if (!strncasecmp(&outbuf[i], "“", 7)) {
401 strcpy(&outbuf[i+1], &outbuf[i+7]);
404 else if (!strncasecmp(&outbuf[i], "”", 7)) {
406 strcpy(&outbuf[i+1], &outbuf[i+7]);
409 else if (!strncasecmp(&outbuf[i], "´", 7)) {
411 strcpy(&outbuf[i+1], &outbuf[i+7]);
414 /* two-digit decimal equivalents */
415 else if ((!strncmp(&outbuf[i], "&#", 2))
416 && (outbuf[i+4] == ';') ) {
418 sscanf(&outbuf[i+2], "%02d", &scanch);
420 strcpy(&outbuf[i+1], &outbuf[i+5]);
423 /* three-digit decimal equivalents */
424 else if ((!strncmp(&outbuf[i], "&#", 2))
425 && (outbuf[i+5] == ';') ) {
427 sscanf(&outbuf[i+2], "%03d", &scanch);
429 strcpy(&outbuf[i+1], &outbuf[i+6]);
434 /* Make sure the output buffer is big enough */
435 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
436 outptr_buffer_size += SIZ;
437 outptr = realloc(outptr, outptr_buffer_size);
438 if (outptr == NULL) {
443 /* Output any lines terminated with hard line breaks */
446 if (strlen(outbuf) > 0) {
447 for (i = 0; i<strlen(outbuf); ++i) {
448 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
450 strncpy(&outptr[output_len], outbuf, i+1);
454 strcpy(&outptr[output_len], " ");
458 strcpy(outbuf, &outbuf[i+1]);
466 /* Add soft line breaks */
467 if (strlen(outbuf) > (screenwidth - 2 )) {
469 for (i=0; i<(screenwidth-2); ++i) {
470 if (outbuf[i]==32) rb = i;
473 strncpy(&outptr[output_len], outbuf, rb);
475 strcpy(&outptr[output_len], nl);
476 output_len += strlen(nl);
478 strcpy(&outptr[output_len], " ");
481 strcpy(outbuf, &outbuf[rb+1]);
483 strncpy(&outptr[output_len], outbuf,
485 output_len += (screenwidth-2);
486 strcpy(&outptr[output_len], nl);
487 output_len += strlen(nl);
489 strcpy(&outptr[output_len], " ");
492 strcpy(outbuf, &outbuf[screenwidth-2]);
496 } while (done_reading == 0);
498 strcpy(&outptr[output_len], outbuf);
499 output_len += strlen(outbuf);
501 /* Strip leading/trailing whitespace. We can't do this with
502 * striplt() because it uses too many strlen()'s
504 while ((output_len > 0) && (isspace(outptr[0]))) {
505 strcpy(outptr, &outptr[1]);
508 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
509 outptr[output_len-1] = 0;
513 if (outptr[output_len-1] != '\n') {
514 strcat(outptr, "\n");