2 * html.c -- Functions which handle translation between HTML and plain text
24 #include "sysdep_decls.h"
34 * Convert HTML to plain text.
36 char *html_to_ascii(char *inputmsg, int screenwidth) {
44 int i, j, ch, did_out, rb;
45 int nest = 0; /* Bracket nesting level */
51 outptr = mallok(strlen(inptr) + 256);
52 if (outptr == NULL) return NULL;
57 /* Fill the input buffer */
58 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
60 /* FIX ... genericize this */
63 inbuf[strlen(inbuf)+1] = 0;
64 inbuf[strlen(inbuf)] = ch;
73 if (strlen(inbuf)>0) {
75 /* Fold in all the spacing */
76 for (i=0; i<strlen(inbuf); ++i) {
77 if (inbuf[i]==10) inbuf[i]=32;
78 if (inbuf[i]==13) inbuf[i]=32;
79 if (inbuf[i]==9) inbuf[i]=32;
80 if ((inbuf[i]<32) || (inbuf[i]>126))
81 strcpy(&inbuf[i], &inbuf[i+1]);
82 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
83 strcpy(&inbuf[i], &inbuf[i+1]);
86 for (i=0; i<strlen(inbuf); ++i) {
98 if (!strcasecmp(tag, "P")) {
99 strcat(outbuf, "\n\n");
102 else if (!strcasecmp(tag, "H1")) {
103 strcat(outbuf, "\n\n");
106 else if (!strcasecmp(tag, "H2")) {
107 strcat(outbuf, "\n\n");
110 else if (!strcasecmp(tag, "H3")) {
111 strcat(outbuf, "\n\n");
114 else if (!strcasecmp(tag, "H4")) {
115 strcat(outbuf, "\n\n");
118 else if (!strcasecmp(tag, "/H1")) {
119 strcat(outbuf, "\n");
122 else if (!strcasecmp(tag, "/H2")) {
123 strcat(outbuf, "\n");
126 else if (!strcasecmp(tag, "/H3")) {
127 strcat(outbuf, "\n");
130 else if (!strcasecmp(tag, "/H4")) {
131 strcat(outbuf, "\n");
134 else if (!strcasecmp(tag, "HR")) {
135 strcat(outbuf, "\n ");
136 for (j=0; j<screenwidth-2; ++j)
138 strcat(outbuf, "\n");
141 else if (!strcasecmp(tag, "BR")) {
142 strcat(outbuf, "\n");
145 else if (!strcasecmp(tag, "TR")) {
146 strcat(outbuf, "\n");
149 else if (!strcasecmp(tag, "/TABLE")) {
150 strcat(outbuf, "\n");
155 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
156 tag[strlen(tag)+1] = 0;
157 tag[strlen(tag)] = ch;
161 outbuf[strlen(outbuf)+1] = 0;
162 outbuf[strlen(outbuf)] = ch;
165 strcpy(inbuf, &inbuf[i]);
168 /* Convert &; tags to the forbidden characters */
169 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
171 if (!strncasecmp(&outbuf[i], " ", 6)) {
173 strcpy(&outbuf[i+1], &outbuf[i+6]);
176 else if (!strncasecmp(&outbuf[i], "<", 4)) {
178 strcpy(&outbuf[i+1], &outbuf[i+4]);
181 else if (!strncasecmp(&outbuf[i], ">", 4)) {
183 strcpy(&outbuf[i+1], &outbuf[i+4]);
186 else if (!strncasecmp(&outbuf[i], "&", 5)) {
187 strcpy(&outbuf[i+1], &outbuf[i+5]);
190 else if (!strncasecmp(&outbuf[i], """, 6)) {
192 strcpy(&outbuf[i+1], &outbuf[i+6]);
195 else if (!strncasecmp(&outbuf[i], "©", 6)) {
199 strcpy(&outbuf[i+3], &outbuf[i+6]);
202 else if (!strncasecmp(&outbuf[i], "®", 5)) {
206 strcpy(&outbuf[i+3], &outbuf[i+5]);
211 /* Make sure the output buffer is big enough */
212 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
214 outptr = realloc(outptr, outlen);
217 /* Output any lines terminated with hard line breaks */
220 if (strlen(outbuf)>0)
221 for (i = 0; i<strlen(outbuf); ++i) {
222 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
224 strncat(outptr, outbuf, i+1);
226 strcpy(outbuf, &outbuf[i+1]);
233 /* Add soft line breaks */
234 if (strlen(outbuf) > (screenwidth - 2)) {
236 for (i=0; i<(screenwidth-2); ++i) {
237 if (outbuf[i]==32) rb = i;
241 strncat(outptr, outbuf, rb);
242 strcat(outptr, "\n");
244 strcpy(outbuf, &outbuf[rb+1]);
247 strncat(outptr, outbuf, screenwidth-2);
248 strcat(outptr, "\n");
250 strcpy(outbuf, &outbuf[screenwidth-2]);
254 } while (done_reading == 0);
256 strcat(outptr, outbuf);
257 strcat(outptr, "\n");