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 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 */
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;
94 if (strlen(inbuf)>0) {
96 /* Fold in all the spacing */
97 for (i=0; i<strlen(inbuf); ++i) {
98 if (inbuf[i]==10) inbuf[i]=32;
99 if (inbuf[i]==13) inbuf[i]=32;
100 if (inbuf[i]==9) inbuf[i]=32;
101 /*** we like foreign characters now.
102 if ((inbuf[i]<32) || (inbuf[i]>126)) {
106 for (i=0; i<strlen(inbuf); ++i) {
107 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
108 strcpy(&inbuf[i], &inbuf[i+1]);
111 for (i=0; i<strlen(inbuf); ++i) {
120 else if (ch == '>') { /* We have a tag. */
121 if (nest > 0) --nest;
123 /* Unqualify the tag (truncate at first space) */
124 if (strchr(tag, ' ') != NULL) {
125 strcpy(strchr(tag, ' '), "");
128 if (!strcasecmp(tag, "P")) {
129 strcat(outbuf, "\n\n");
132 if (!strcasecmp(tag, "/DIV")) {
133 strcat(outbuf, "\n\n");
136 if (!strcasecmp(tag, "LI")) {
137 strcat(outbuf, "\n * ");
140 else if (!strcasecmp(tag, "/UL")) {
141 strcat(outbuf, "\n\n");
144 else if (!strcasecmp(tag, "H1")) {
145 strcat(outbuf, "\n\n");
148 else if (!strcasecmp(tag, "H2")) {
149 strcat(outbuf, "\n\n");
152 else if (!strcasecmp(tag, "H3")) {
153 strcat(outbuf, "\n\n");
156 else if (!strcasecmp(tag, "H4")) {
157 strcat(outbuf, "\n\n");
160 else if (!strcasecmp(tag, "/H1")) {
161 strcat(outbuf, "\n");
164 else if (!strcasecmp(tag, "/H2")) {
165 strcat(outbuf, "\n");
168 else if (!strcasecmp(tag, "/H3")) {
169 strcat(outbuf, "\n");
172 else if (!strcasecmp(tag, "/H4")) {
173 strcat(outbuf, "\n");
176 else if (!strcasecmp(tag, "HR")) {
177 strcat(outbuf, "\n ");
178 for (j=0; j<screenwidth-2; ++j)
180 strcat(outbuf, "\n");
183 else if (!strcasecmp(tag, "BR")) {
184 strcat(outbuf, "\n");
187 else if (!strcasecmp(tag, "TR")) {
188 strcat(outbuf, "\n");
191 else if (!strcasecmp(tag, "/TABLE")) {
192 strcat(outbuf, "\n");
195 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
196 strcat(outbuf, "\n\n <<\n");
200 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
201 strcat(outbuf, "\n >>\n\n");
207 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
208 tag[strlen(tag)+1] = 0;
209 tag[strlen(tag)] = ch;
213 outbuf[strlen(outbuf)+1] = 0;
214 outbuf[strlen(outbuf)] = ch;
217 strcpy(inbuf, &inbuf[i]);
220 /* Convert &; tags to the forbidden characters */
221 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
223 if (!strncasecmp(&outbuf[i], " ", 6)) {
225 strcpy(&outbuf[i+1], &outbuf[i+6]);
228 else if (!strncasecmp(&outbuf[i], "<", 4)) {
230 strcpy(&outbuf[i+1], &outbuf[i+4]);
233 else if (!strncasecmp(&outbuf[i], ">", 4)) {
235 strcpy(&outbuf[i+1], &outbuf[i+4]);
238 else if (!strncasecmp(&outbuf[i], "&", 5)) {
239 strcpy(&outbuf[i+1], &outbuf[i+5]);
242 else if (!strncasecmp(&outbuf[i], """, 6)) {
244 strcpy(&outbuf[i+1], &outbuf[i+6]);
247 else if (!strncasecmp(&outbuf[i], "©", 6)) {
251 strcpy(&outbuf[i+3], &outbuf[i+6]);
254 else if (!strncasecmp(&outbuf[i], "®", 5)) {
258 strcpy(&outbuf[i+3], &outbuf[i+5]);
261 /* two-digit decimal equivalents */
262 else if ((!strncmp(&outbuf[i], "&#", 2))
263 && (outbuf[i+4] == ';') ) {
265 sscanf(&outbuf[i+2], "%02d", &scanch);
267 strcpy(&outbuf[i+1], &outbuf[i+5]);
270 /* three-digit decimal equivalents */
271 else if ((!strncmp(&outbuf[i], "&#", 2))
272 && (outbuf[i+5] == ';') ) {
274 sscanf(&outbuf[i+2], "%03d", &scanch);
276 strcpy(&outbuf[i+1], &outbuf[i+6]);
281 /* Make sure the output buffer is big enough */
282 if ((output_len + strlen(outbuf) + SIZ)
283 > outptr_buffer_size) {
284 outptr_buffer_size += SIZ;
285 outptr = realloc(outptr, outptr_buffer_size);
288 /* Output any lines terminated with hard line breaks */
291 if (strlen(outbuf)>0) {
292 for (i = 0; i<strlen(outbuf); ++i) {
293 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
295 strncpy(&outptr[output_len],
300 strcpy(&outptr[output_len],
305 strcpy(outbuf, &outbuf[i+1]);
313 /* Add soft line breaks */
314 if (strlen(outbuf) > (screenwidth - 2 )) {
316 for (i=0; i<(screenwidth-2); ++i) {
317 if (outbuf[i]==32) rb = i;
320 strncpy(&outptr[output_len], outbuf, rb);
322 strcpy(&outptr[output_len], "\n");
325 strcpy(&outptr[output_len], " ");
328 strcpy(outbuf, &outbuf[rb+1]);
330 strncpy(&outptr[output_len], outbuf,
332 output_len += (screenwidth-2);
333 strcpy(&outptr[output_len], "\n");
336 strcpy(&outptr[output_len], " ");
339 strcpy(outbuf, &outbuf[screenwidth-2]);
343 } while (done_reading == 0);
345 strcpy(&outptr[output_len], outbuf);
346 output_len += strlen(outbuf);
348 /* Strip leading/trailing whitespace. We can't do this with
349 * striplt() because it uses too many strlen()'s
351 while ((output_len > 0) && (isspace(outptr[0]))) {
352 strcpy(outptr, &outptr[1]);
355 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
356 outptr[output_len-1] = 0;
360 if (outptr[output_len-1] != '\n') {
361 strcat(outptr, "\n");