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>
38 #include "serv_extensions.h"
40 #include "sysdep_decls.h"
50 * Convert HTML to plain text.
52 * inputmsg = pointer to raw HTML message
53 * screenwidth = desired output screenwidth
54 * do_citaformat = set to 1 to indent newlines with spaces
56 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
63 size_t outptr_buffer_size;
64 size_t output_len = 0;
65 int i, j, ch, did_out, rb;
66 int nest = 0; /* Bracket nesting level */
72 outptr_buffer_size = strlen(inptr) + SIZ;
73 outptr = mallok(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 if ((inbuf[i]<32) || (inbuf[i]>126)) {
103 /* strcpy(&inbuf[i], &inbuf[i+1]); */
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 == '>') {
121 if (nest > 0) --nest;
123 if (!strcasecmp(tag, "P")) {
124 strcat(outbuf, "\n\n");
127 if (!strcasecmp(tag, "/DIV")) {
128 strcat(outbuf, "\n\n");
131 else if (!strcasecmp(tag, "H1")) {
132 strcat(outbuf, "\n\n");
135 else if (!strcasecmp(tag, "H2")) {
136 strcat(outbuf, "\n\n");
139 else if (!strcasecmp(tag, "H3")) {
140 strcat(outbuf, "\n\n");
143 else if (!strcasecmp(tag, "H4")) {
144 strcat(outbuf, "\n\n");
147 else if (!strcasecmp(tag, "/H1")) {
148 strcat(outbuf, "\n");
151 else if (!strcasecmp(tag, "/H2")) {
152 strcat(outbuf, "\n");
155 else if (!strcasecmp(tag, "/H3")) {
156 strcat(outbuf, "\n");
159 else if (!strcasecmp(tag, "/H4")) {
160 strcat(outbuf, "\n");
163 else if (!strcasecmp(tag, "HR")) {
164 strcat(outbuf, "\n ");
165 for (j=0; j<screenwidth-2; ++j)
167 strcat(outbuf, "\n");
170 else if (!strcasecmp(tag, "BR")) {
171 strcat(outbuf, "\n");
174 else if (!strcasecmp(tag, "TR")) {
175 strcat(outbuf, "\n");
178 else if (!strcasecmp(tag, "/TABLE")) {
179 strcat(outbuf, "\n");
184 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
185 tag[strlen(tag)+1] = 0;
186 tag[strlen(tag)] = ch;
190 outbuf[strlen(outbuf)+1] = 0;
191 outbuf[strlen(outbuf)] = ch;
194 strcpy(inbuf, &inbuf[i]);
197 /* Convert &; tags to the forbidden characters */
198 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
200 if (!strncasecmp(&outbuf[i], " ", 6)) {
202 strcpy(&outbuf[i+1], &outbuf[i+6]);
205 else if (!strncasecmp(&outbuf[i], "<", 4)) {
207 strcpy(&outbuf[i+1], &outbuf[i+4]);
210 else if (!strncasecmp(&outbuf[i], ">", 4)) {
212 strcpy(&outbuf[i+1], &outbuf[i+4]);
215 else if (!strncasecmp(&outbuf[i], "&", 5)) {
216 strcpy(&outbuf[i+1], &outbuf[i+5]);
219 else if (!strncasecmp(&outbuf[i], """, 6)) {
221 strcpy(&outbuf[i+1], &outbuf[i+6]);
224 else if (!strncasecmp(&outbuf[i], "©", 6)) {
228 strcpy(&outbuf[i+3], &outbuf[i+6]);
231 else if (!strncasecmp(&outbuf[i], "®", 5)) {
235 strcpy(&outbuf[i+3], &outbuf[i+5]);
240 /* Make sure the output buffer is big enough */
241 if ((output_len + strlen(outbuf) + SIZ)
242 > outptr_buffer_size) {
243 outptr_buffer_size += SIZ;
244 outptr = realloc(outptr, outptr_buffer_size);
247 /* Output any lines terminated with hard line breaks */
250 if (strlen(outbuf)>0)
251 for (i = 0; i<strlen(outbuf); ++i) {
252 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
254 strncpy(&outptr[output_len],
259 strcpy(&outptr[output_len],
264 strcpy(outbuf, &outbuf[i+1]);
271 /* Add soft line breaks */
272 if (strlen(outbuf) > (screenwidth - 2)) {
274 for (i=0; i<(screenwidth-2); ++i) {
275 if (outbuf[i]==32) rb = i;
278 strncpy(&outptr[output_len], outbuf, rb);
280 strcpy(&outptr[output_len], "\n");
283 strcpy(&outptr[output_len], " ");
286 strcpy(outbuf, &outbuf[rb+1]);
288 strncpy(&outptr[output_len], outbuf,
290 output_len += (screenwidth-2);
291 strcpy(&outptr[output_len], "\n");
294 strcpy(&outptr[output_len], " ");
297 strcpy(outbuf, &outbuf[screenwidth-2]);
301 } while (done_reading == 0);
303 strcpy(&outptr[output_len], outbuf);
304 output_len += strlen(outbuf);
306 /* Strip leading/trailing whitespace. We can't do this with
307 * striplt() because it uses too many strlen()'s
309 while ((output_len > 0) && (isspace(outptr[0]))) {
310 strcpy(outptr, &outptr[1]);
313 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
314 outptr[output_len-1] = 0;
318 if (outptr[output_len-1] != '\n') {
319 strcat(outptr, "\n");