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;
65 int nest = 0; /* Bracket nesting level */
71 outptr_buffer_size = strlen(inptr) + SIZ;
72 outptr = malloc(outptr_buffer_size);
73 if (outptr == NULL) return NULL;
78 /* Fill the input buffer */
79 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
83 inbuf[strlen(inbuf)+1] = 0;
84 inbuf[strlen(inbuf)] = ch;
93 if (strlen(inbuf)>0) {
95 /* Fold in all the spacing */
96 for (i=0; i<strlen(inbuf); ++i) {
97 if (inbuf[i]==10) inbuf[i]=32;
98 if (inbuf[i]==13) inbuf[i]=32;
99 if (inbuf[i]==9) inbuf[i]=32;
100 if ((inbuf[i]<32) || (inbuf[i]>126)) {
102 /* strcpy(&inbuf[i], &inbuf[i+1]); */
105 for (i=0; i<strlen(inbuf); ++i) {
106 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
107 strcpy(&inbuf[i], &inbuf[i+1]);
110 for (i=0; i<strlen(inbuf); ++i) {
119 else if (ch == '>') {
120 if (nest > 0) --nest;
122 if (!strcasecmp(tag, "P")) {
123 strcat(outbuf, "\n\n");
126 if (!strcasecmp(tag, "/DIV")) {
127 strcat(outbuf, "\n\n");
130 else if (!strcasecmp(tag, "H1")) {
131 strcat(outbuf, "\n\n");
134 else if (!strcasecmp(tag, "H2")) {
135 strcat(outbuf, "\n\n");
138 else if (!strcasecmp(tag, "H3")) {
139 strcat(outbuf, "\n\n");
142 else if (!strcasecmp(tag, "H4")) {
143 strcat(outbuf, "\n\n");
146 else if (!strcasecmp(tag, "/H1")) {
147 strcat(outbuf, "\n");
150 else if (!strcasecmp(tag, "/H2")) {
151 strcat(outbuf, "\n");
154 else if (!strcasecmp(tag, "/H3")) {
155 strcat(outbuf, "\n");
158 else if (!strcasecmp(tag, "/H4")) {
159 strcat(outbuf, "\n");
162 else if (!strcasecmp(tag, "HR")) {
163 strcat(outbuf, "\n ");
164 for (j=0; j<screenwidth-2; ++j)
166 strcat(outbuf, "\n");
169 else if (!strcasecmp(tag, "BR")) {
170 strcat(outbuf, "\n");
173 else if (!strcasecmp(tag, "TR")) {
174 strcat(outbuf, "\n");
177 else if (!strcasecmp(tag, "/TABLE")) {
178 strcat(outbuf, "\n");
183 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
184 tag[strlen(tag)+1] = 0;
185 tag[strlen(tag)] = ch;
189 outbuf[strlen(outbuf)+1] = 0;
190 outbuf[strlen(outbuf)] = ch;
193 strcpy(inbuf, &inbuf[i]);
196 /* Convert &; tags to the forbidden characters */
197 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
199 if (!strncasecmp(&outbuf[i], " ", 6)) {
201 strcpy(&outbuf[i+1], &outbuf[i+6]);
204 else if (!strncasecmp(&outbuf[i], "<", 4)) {
206 strcpy(&outbuf[i+1], &outbuf[i+4]);
209 else if (!strncasecmp(&outbuf[i], ">", 4)) {
211 strcpy(&outbuf[i+1], &outbuf[i+4]);
214 else if (!strncasecmp(&outbuf[i], "&", 5)) {
215 strcpy(&outbuf[i+1], &outbuf[i+5]);
218 else if (!strncasecmp(&outbuf[i], """, 6)) {
220 strcpy(&outbuf[i+1], &outbuf[i+6]);
223 else if (!strncasecmp(&outbuf[i], "©", 6)) {
227 strcpy(&outbuf[i+3], &outbuf[i+6]);
230 else if (!strncasecmp(&outbuf[i], "®", 5)) {
234 strcpy(&outbuf[i+3], &outbuf[i+5]);
239 /* Make sure the output buffer is big enough */
240 if ((output_len + strlen(outbuf) + SIZ)
241 > outptr_buffer_size) {
242 outptr_buffer_size += SIZ;
243 outptr = realloc(outptr, outptr_buffer_size);
246 /* Output any lines terminated with hard line breaks */
249 if (strlen(outbuf)>0)
250 for (i = 0; i<strlen(outbuf); ++i) {
251 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
253 strncpy(&outptr[output_len],
258 strcpy(&outptr[output_len],
263 strcpy(outbuf, &outbuf[i+1]);
270 /* Add soft line breaks */
271 if (strlen(outbuf) > (screenwidth - 2)) {
273 for (i=0; i<(screenwidth-2); ++i) {
274 if (outbuf[i]==32) rb = i;
277 strncpy(&outptr[output_len], outbuf, rb);
279 strcpy(&outptr[output_len], "\n");
282 strcpy(&outptr[output_len], " ");
285 strcpy(outbuf, &outbuf[rb+1]);
287 strncpy(&outptr[output_len], outbuf,
289 output_len += (screenwidth-2);
290 strcpy(&outptr[output_len], "\n");
293 strcpy(&outptr[output_len], " ");
296 strcpy(outbuf, &outbuf[screenwidth-2]);
300 } while (done_reading == 0);
302 strcpy(&outptr[output_len], outbuf);
303 output_len += strlen(outbuf);
305 /* Strip leading/trailing whitespace. We can't do this with
306 * striplt() because it uses too many strlen()'s
308 while ((output_len > 0) && (isspace(outptr[0]))) {
309 strcpy(outptr, &outptr[1]);
312 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
313 outptr[output_len-1] = 0;
317 if (outptr[output_len-1] != '\n') {
318 strcat(outptr, "\n");