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))
102 strcpy(&inbuf[i], &inbuf[i+1]);
104 for (i=0; i<strlen(inbuf); ++i) {
105 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
106 strcpy(&inbuf[i], &inbuf[i+1]);
109 for (i=0; i<strlen(inbuf); ++i) {
118 else if (ch == '>') {
119 if (nest > 0) --nest;
121 if (!strcasecmp(tag, "P")) {
122 strcat(outbuf, "\n\n");
125 if (!strcasecmp(tag, "/DIV")) {
126 strcat(outbuf, "\n\n");
129 else if (!strcasecmp(tag, "H1")) {
130 strcat(outbuf, "\n\n");
133 else if (!strcasecmp(tag, "H2")) {
134 strcat(outbuf, "\n\n");
137 else if (!strcasecmp(tag, "H3")) {
138 strcat(outbuf, "\n\n");
141 else if (!strcasecmp(tag, "H4")) {
142 strcat(outbuf, "\n\n");
145 else if (!strcasecmp(tag, "/H1")) {
146 strcat(outbuf, "\n");
149 else if (!strcasecmp(tag, "/H2")) {
150 strcat(outbuf, "\n");
153 else if (!strcasecmp(tag, "/H3")) {
154 strcat(outbuf, "\n");
157 else if (!strcasecmp(tag, "/H4")) {
158 strcat(outbuf, "\n");
161 else if (!strcasecmp(tag, "HR")) {
162 strcat(outbuf, "\n ");
163 for (j=0; j<screenwidth-2; ++j)
165 strcat(outbuf, "\n");
168 else if (!strcasecmp(tag, "BR")) {
169 strcat(outbuf, "\n");
172 else if (!strcasecmp(tag, "TR")) {
173 strcat(outbuf, "\n");
176 else if (!strcasecmp(tag, "/TABLE")) {
177 strcat(outbuf, "\n");
182 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
183 tag[strlen(tag)+1] = 0;
184 tag[strlen(tag)] = ch;
188 outbuf[strlen(outbuf)+1] = 0;
189 outbuf[strlen(outbuf)] = ch;
192 strcpy(inbuf, &inbuf[i]);
195 /* Convert &; tags to the forbidden characters */
196 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
198 if (!strncasecmp(&outbuf[i], " ", 6)) {
200 strcpy(&outbuf[i+1], &outbuf[i+6]);
203 else if (!strncasecmp(&outbuf[i], "<", 4)) {
205 strcpy(&outbuf[i+1], &outbuf[i+4]);
208 else if (!strncasecmp(&outbuf[i], ">", 4)) {
210 strcpy(&outbuf[i+1], &outbuf[i+4]);
213 else if (!strncasecmp(&outbuf[i], "&", 5)) {
214 strcpy(&outbuf[i+1], &outbuf[i+5]);
217 else if (!strncasecmp(&outbuf[i], """, 6)) {
219 strcpy(&outbuf[i+1], &outbuf[i+6]);
222 else if (!strncasecmp(&outbuf[i], "©", 6)) {
226 strcpy(&outbuf[i+3], &outbuf[i+6]);
229 else if (!strncasecmp(&outbuf[i], "®", 5)) {
233 strcpy(&outbuf[i+3], &outbuf[i+5]);
238 /* Make sure the output buffer is big enough */
239 if ((output_len + strlen(outbuf) + SIZ)
240 > outptr_buffer_size) {
241 outptr_buffer_size += SIZ;
242 outptr = realloc(outptr, outptr_buffer_size);
245 /* Output any lines terminated with hard line breaks */
248 if (strlen(outbuf)>0)
249 for (i = 0; i<strlen(outbuf); ++i) {
250 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
252 strncpy(&outptr[output_len],
257 strcpy(&outptr[output_len],
262 strcpy(outbuf, &outbuf[i+1]);
269 /* Add soft line breaks */
270 if (strlen(outbuf) > (screenwidth - 2)) {
272 for (i=0; i<(screenwidth-2); ++i) {
273 if (outbuf[i]==32) rb = i;
276 strncpy(&outptr[output_len], outbuf, rb);
278 strcpy(&outptr[output_len], "\n");
281 strcpy(&outptr[output_len], " ");
284 strcpy(outbuf, &outbuf[rb+1]);
286 strncpy(&outptr[output_len], outbuf,
288 output_len += (screenwidth-2);
289 strcpy(&outptr[output_len], "\n");
292 strcpy(&outptr[output_len], " ");
295 strcpy(outbuf, &outbuf[screenwidth-2]);
299 } while (done_reading == 0);
301 strcpy(&outptr[output_len], outbuf);
302 output_len += strlen(outbuf);
304 /* Strip leading/trailing whitespace. We can't do this with
305 * striplt() because it uses too many strlen()'s
307 while ((output_len > 0) && (isspace(outptr[0]))) {
308 strcpy(outptr, &outptr[1]);
311 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
312 outptr[output_len-1] = 0;
316 if (outptr[output_len-1] != '\n') {
317 strcat(outptr, "\n");