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 */
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 == '>') { /* We have a tag. */
120 if (nest > 0) --nest;
122 /* Unqualify the tag (truncate at first space) */
123 if (strchr(tag, ' ') != NULL) {
124 strcpy(strchr(tag, ' '), "");
127 if (!strcasecmp(tag, "P")) {
128 strcat(outbuf, "\n\n");
131 if (!strcasecmp(tag, "/DIV")) {
132 strcat(outbuf, "\n\n");
135 else if (!strcasecmp(tag, "H1")) {
136 strcat(outbuf, "\n\n");
139 else if (!strcasecmp(tag, "H2")) {
140 strcat(outbuf, "\n\n");
143 else if (!strcasecmp(tag, "H3")) {
144 strcat(outbuf, "\n\n");
147 else if (!strcasecmp(tag, "H4")) {
148 strcat(outbuf, "\n\n");
151 else if (!strcasecmp(tag, "/H1")) {
152 strcat(outbuf, "\n");
155 else if (!strcasecmp(tag, "/H2")) {
156 strcat(outbuf, "\n");
159 else if (!strcasecmp(tag, "/H3")) {
160 strcat(outbuf, "\n");
163 else if (!strcasecmp(tag, "/H4")) {
164 strcat(outbuf, "\n");
167 else if (!strcasecmp(tag, "HR")) {
168 strcat(outbuf, "\n ");
169 for (j=0; j<screenwidth-2; ++j)
171 strcat(outbuf, "\n");
174 else if (!strcasecmp(tag, "BR")) {
175 strcat(outbuf, "\n");
178 else if (!strcasecmp(tag, "TR")) {
179 strcat(outbuf, "\n");
182 else if (!strcasecmp(tag, "/TABLE")) {
183 strcat(outbuf, "\n");
188 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
189 tag[strlen(tag)+1] = 0;
190 tag[strlen(tag)] = ch;
194 outbuf[strlen(outbuf)+1] = 0;
195 outbuf[strlen(outbuf)] = ch;
198 strcpy(inbuf, &inbuf[i]);
201 /* Convert &; tags to the forbidden characters */
202 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
204 if (!strncasecmp(&outbuf[i], " ", 6)) {
206 strcpy(&outbuf[i+1], &outbuf[i+6]);
209 else if (!strncasecmp(&outbuf[i], "<", 4)) {
211 strcpy(&outbuf[i+1], &outbuf[i+4]);
214 else if (!strncasecmp(&outbuf[i], ">", 4)) {
216 strcpy(&outbuf[i+1], &outbuf[i+4]);
219 else if (!strncasecmp(&outbuf[i], "&", 5)) {
220 strcpy(&outbuf[i+1], &outbuf[i+5]);
223 else if (!strncasecmp(&outbuf[i], """, 6)) {
225 strcpy(&outbuf[i+1], &outbuf[i+6]);
228 else if (!strncasecmp(&outbuf[i], "©", 6)) {
232 strcpy(&outbuf[i+3], &outbuf[i+6]);
235 else if (!strncasecmp(&outbuf[i], "®", 5)) {
239 strcpy(&outbuf[i+3], &outbuf[i+5]);
242 /* two-digit decimal equivalents */
243 else if ((!strncmp(&outbuf[i], "&#", 2))
244 && (outbuf[i+4] == ';') ) {
246 sscanf(&outbuf[i+2], "%02d", &scanch);
248 strcpy(&outbuf[i+1], &outbuf[i+5]);
251 /* three-digit decimal equivalents */
252 else if ((!strncmp(&outbuf[i], "&#", 2))
253 && (outbuf[i+5] == ';') ) {
255 sscanf(&outbuf[i+2], "%03d", &scanch);
257 strcpy(&outbuf[i+1], &outbuf[i+6]);
262 /* Make sure the output buffer is big enough */
263 if ((output_len + strlen(outbuf) + SIZ)
264 > outptr_buffer_size) {
265 outptr_buffer_size += SIZ;
266 outptr = realloc(outptr, outptr_buffer_size);
269 /* Output any lines terminated with hard line breaks */
272 if (strlen(outbuf)>0)
273 for (i = 0; i<strlen(outbuf); ++i) {
274 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
276 strncpy(&outptr[output_len],
281 strcpy(&outptr[output_len],
286 strcpy(outbuf, &outbuf[i+1]);
293 /* Add soft line breaks */
294 if (strlen(outbuf) > (screenwidth - 2)) {
296 for (i=0; i<(screenwidth-2); ++i) {
297 if (outbuf[i]==32) rb = i;
300 strncpy(&outptr[output_len], outbuf, rb);
302 strcpy(&outptr[output_len], "\n");
305 strcpy(&outptr[output_len], " ");
308 strcpy(outbuf, &outbuf[rb+1]);
310 strncpy(&outptr[output_len], outbuf,
312 output_len += (screenwidth-2);
313 strcpy(&outptr[output_len], "\n");
316 strcpy(&outptr[output_len], " ");
319 strcpy(outbuf, &outbuf[screenwidth-2]);
323 } while (done_reading == 0);
325 strcpy(&outptr[output_len], outbuf);
326 output_len += strlen(outbuf);
328 /* Strip leading/trailing whitespace. We can't do this with
329 * striplt() because it uses too many strlen()'s
331 while ((output_len > 0) && (isspace(outptr[0]))) {
332 strcpy(outptr, &outptr[1]);
335 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
336 outptr[output_len-1] = 0;
340 if (outptr[output_len-1] != '\n') {
341 strcat(outptr, "\n");