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) {
42 int i, j, ch, did_out, rb;
43 int nest = 0; /* Bracket nesting level */
50 /* Fill the input buffer */
51 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
53 /* FIX ... genericize this */
56 inbuf[strlen(inbuf)+1] = 0;
57 inbuf[strlen(inbuf)] = ch;
66 if (strlen(inbuf)>0) {
68 /* Fold in all the spacing */
69 for (i=0; i<strlen(inbuf); ++i) {
70 if (inbuf[i]==10) inbuf[i]=32;
71 if (inbuf[i]==13) inbuf[i]=32;
72 if (inbuf[i]==9) inbuf[i]=32;
73 if ((inbuf[i]<32) || (inbuf[i]>126))
74 strcpy(&inbuf[i], &inbuf[i+1]);
75 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
76 strcpy(&inbuf[i], &inbuf[i+1]);
79 for (i=0; i<strlen(inbuf); ++i) {
91 if (!strcasecmp(tag, "P")) {
92 strcat(outbuf, "\n\n");
95 else if (!strcasecmp(tag, "H1")) {
96 strcat(outbuf, "\n\n");
99 else if (!strcasecmp(tag, "H2")) {
100 strcat(outbuf, "\n\n");
103 else if (!strcasecmp(tag, "H3")) {
104 strcat(outbuf, "\n\n");
107 else if (!strcasecmp(tag, "H4")) {
108 strcat(outbuf, "\n\n");
111 else if (!strcasecmp(tag, "/H1")) {
112 strcat(outbuf, "\n");
115 else if (!strcasecmp(tag, "/H2")) {
116 strcat(outbuf, "\n");
119 else if (!strcasecmp(tag, "/H3")) {
120 strcat(outbuf, "\n");
123 else if (!strcasecmp(tag, "/H4")) {
124 strcat(outbuf, "\n");
127 else if (!strcasecmp(tag, "HR")) {
128 strcat(outbuf, "\n ");
129 for (j=0; j<screenwidth-2; ++j)
131 strcat(outbuf, "\n");
134 else if (!strcasecmp(tag, "BR")) {
135 strcat(outbuf, "\n");
138 else if (!strcasecmp(tag, "TR")) {
139 strcat(outbuf, "\n");
142 else if (!strcasecmp(tag, "/TABLE")) {
143 strcat(outbuf, "\n");
148 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
149 tag[strlen(tag)+1] = 0;
150 tag[strlen(tag)] = ch;
154 outbuf[strlen(outbuf)+1] = 0;
155 outbuf[strlen(outbuf)] = ch;
158 strcpy(inbuf, &inbuf[i]);
161 /* Convert &; tags to the forbidden characters */
162 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
164 if (!strncasecmp(&outbuf[i], " ", 6)) {
166 strcpy(&outbuf[i+1], &outbuf[i+6]);
169 else if (!strncasecmp(&outbuf[i], "<", 4)) {
171 strcpy(&outbuf[i+1], &outbuf[i+4]);
174 else if (!strncasecmp(&outbuf[i], ">", 4)) {
176 strcpy(&outbuf[i+1], &outbuf[i+4]);
179 else if (!strncasecmp(&outbuf[i], "&", 5)) {
180 strcpy(&outbuf[i+1], &outbuf[i+5]);
183 else if (!strncasecmp(&outbuf[i], """, 6)) {
185 strcpy(&outbuf[i+1], &outbuf[i+6]);
188 else if (!strncasecmp(&outbuf[i], "©", 6)) {
192 strcpy(&outbuf[i+3], &outbuf[i+6]);
197 /* Output any lines terminated with hard line breaks */
200 if (strlen(outbuf)>0)
201 for (i = 0; i<strlen(outbuf); ++i) {
202 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
203 fwrite(outbuf, i+1, 1, stdout);
204 strcpy(outbuf, &outbuf[i+1]);
211 /* Add soft line breaks */
212 if (strlen(outbuf) > (screenwidth - 2)) {
214 for (i=0; i<(screenwidth-2); ++i) {
215 if (outbuf[i]==32) rb = i;
218 fwrite(outbuf, rb, 1, stdout);
219 fwrite("\n", 1, 1, stdout);
220 strcpy(outbuf, &outbuf[rb+1]);
222 fwrite(outbuf, screenwidth-2, 1, stdout);
223 fwrite("\n", 1, 1, stdout);
224 strcpy(outbuf, &outbuf[screenwidth-2]);
228 } while (done_reading == 0);
229 fwrite(outbuf, strlen(outbuf), 1, stdout);
230 fwrite("\n", 1, 1, stdout);
233 strcpy(inptr, "This is eekish.\n");