4 * Functions which handle translation between HTML and plain text
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
33 #include "sysdep_decls.h"
43 * Convert HTML to plain text.
45 * inputmsg = pointer to raw HTML message
46 * screenwidth = desired output screenwidth
47 * do_citaformat = set to 1 to indent newlines with spaces
49 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
57 int i, j, ch, did_out, rb;
58 int nest = 0; /* Bracket nesting level */
64 outptr = mallok(strlen(inptr) + SIZ);
65 if (outptr == NULL) return NULL;
70 /* Fill the input buffer */
71 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
75 inbuf[strlen(inbuf)+1] = 0;
76 inbuf[strlen(inbuf)] = ch;
85 if (strlen(inbuf)>0) {
87 /* Fold in all the spacing */
88 for (i=0; i<strlen(inbuf); ++i) {
89 if (inbuf[i]==10) inbuf[i]=32;
90 if (inbuf[i]==13) inbuf[i]=32;
91 if (inbuf[i]==9) inbuf[i]=32;
92 if ((inbuf[i]<32) || (inbuf[i]>126))
93 strcpy(&inbuf[i], &inbuf[i+1]);
94 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
95 strcpy(&inbuf[i], &inbuf[i+1]);
98 for (i=0; i<strlen(inbuf); ++i) {
107 else if (ch == '>') {
108 if (nest > 0) --nest;
110 if (!strcasecmp(tag, "P")) {
111 strcat(outbuf, "\n\n");
114 if (!strcasecmp(tag, "/DIV")) {
115 strcat(outbuf, "\n\n");
118 else if (!strcasecmp(tag, "H1")) {
119 strcat(outbuf, "\n\n");
122 else if (!strcasecmp(tag, "H2")) {
123 strcat(outbuf, "\n\n");
126 else if (!strcasecmp(tag, "H3")) {
127 strcat(outbuf, "\n\n");
130 else if (!strcasecmp(tag, "H4")) {
131 strcat(outbuf, "\n\n");
134 else if (!strcasecmp(tag, "/H1")) {
135 strcat(outbuf, "\n");
138 else if (!strcasecmp(tag, "/H2")) {
139 strcat(outbuf, "\n");
142 else if (!strcasecmp(tag, "/H3")) {
143 strcat(outbuf, "\n");
146 else if (!strcasecmp(tag, "/H4")) {
147 strcat(outbuf, "\n");
150 else if (!strcasecmp(tag, "HR")) {
151 strcat(outbuf, "\n ");
152 for (j=0; j<screenwidth-2; ++j)
154 strcat(outbuf, "\n");
157 else if (!strcasecmp(tag, "BR")) {
158 strcat(outbuf, "\n");
161 else if (!strcasecmp(tag, "TR")) {
162 strcat(outbuf, "\n");
165 else if (!strcasecmp(tag, "/TABLE")) {
166 strcat(outbuf, "\n");
171 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
172 tag[strlen(tag)+1] = 0;
173 tag[strlen(tag)] = ch;
177 outbuf[strlen(outbuf)+1] = 0;
178 outbuf[strlen(outbuf)] = ch;
181 strcpy(inbuf, &inbuf[i]);
184 /* Convert &; tags to the forbidden characters */
185 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
187 if (!strncasecmp(&outbuf[i], " ", 6)) {
189 strcpy(&outbuf[i+1], &outbuf[i+6]);
192 else if (!strncasecmp(&outbuf[i], "<", 4)) {
194 strcpy(&outbuf[i+1], &outbuf[i+4]);
197 else if (!strncasecmp(&outbuf[i], ">", 4)) {
199 strcpy(&outbuf[i+1], &outbuf[i+4]);
202 else if (!strncasecmp(&outbuf[i], "&", 5)) {
203 strcpy(&outbuf[i+1], &outbuf[i+5]);
206 else if (!strncasecmp(&outbuf[i], """, 6)) {
208 strcpy(&outbuf[i+1], &outbuf[i+6]);
211 else if (!strncasecmp(&outbuf[i], "©", 6)) {
215 strcpy(&outbuf[i+3], &outbuf[i+6]);
218 else if (!strncasecmp(&outbuf[i], "®", 5)) {
222 strcpy(&outbuf[i+3], &outbuf[i+5]);
227 /* Make sure the output buffer is big enough */
228 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
230 outptr = realloc(outptr, outlen);
233 /* Output any lines terminated with hard line breaks */
236 if (strlen(outbuf)>0)
237 for (i = 0; i<strlen(outbuf); ++i) {
238 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
239 strncat(outptr, outbuf, i+1);
240 strcat(outptr, "\n");
243 strcpy(outbuf, &outbuf[i+1]);
250 /* Add soft line breaks */
251 if (strlen(outbuf) > (screenwidth - 2)) {
253 for (i=0; i<(screenwidth-2); ++i) {
254 if (outbuf[i]==32) rb = i;
257 strncat(outptr, outbuf, rb);
258 strcat(outptr, "\n");
261 strcpy(outbuf, &outbuf[rb+1]);
264 strncat(outptr, outbuf, screenwidth-2);
265 strcat(outptr, "\n");
268 strcpy(outbuf, &outbuf[screenwidth-2]);
272 } while (done_reading == 0);
274 strcat(outptr, outbuf);
275 strcat(outptr, "\n");