2 * html.c -- Functions which handle translation between HTML and plain text
24 #include "sysdep_decls.h"
33 * Convert HTML to plain text.
35 char *html_to_ascii(char *inputmsg, int screenwidth) {
40 char *readptr, *outputbuf;
41 size_t input_length, this_read, output_length;
42 int i, ch, did_out, rb;
43 int nest = 0; /* Bracket nesting level */
45 input_length = strlen(inputmsg);
47 output_length = strlen(inputmsg);
48 outputbuf = mallok(output_length);
49 if (outputbuf==NULL) return NULL;
53 lprintf(9, "Decoding %d bytes of HTML\n", input_length);
56 /* Fill the input buffer */
57 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
59 /* copy from the input buffer */
60 lprintf(9, "input loop\n");
61 this_read = strlen(readptr);
62 if (this_read > 127) this_read = 127;
63 lprintf(9, "%d bytes\n", this_read);
64 for (i=0; i<this_read; ++i) {
65 inbuf[strlen(inbuf)+1] = 0;
66 inbuf[strlen(inbuf)] = readptr[0];
70 if (strlen(readptr)==0) done_reading = 1;
73 lprintf(9, "skipped input loop\n");
77 lprintf(9, "parse loop\n");
78 if (strlen(inbuf)>0) {
80 /* Fold in all the spacing */
81 lprintf(9, "spacing loop\n");
82 for (i=0; i<strlen(inbuf); ++i) {
83 if (inbuf[i]==10) inbuf[i]=32;
84 if (inbuf[i]==13) inbuf[i]=32;
85 if (inbuf[i]==9) inbuf[i]=32;
86 if ((inbuf[i]<32) || (inbuf[i]>126))
87 strcpy(&inbuf[i], &inbuf[i+1]);
88 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
89 strcpy(&inbuf[i], &inbuf[i+1]);
92 lprintf(9, "foo loop\n");
93 for (i=0; i<strlen(inbuf); ++i) {
98 lprintf(9, "bar loop\n");
103 else if (ch == '>') {
104 lprintf(9, "baz loop\n");
105 if (nest > 0) --nest;
107 if (!strcasecmp(tag, "P")) {
108 strcat(outbuf, "\n\n");
111 else if (!strcasecmp(tag, "H1")) {
112 strcat(outbuf, "\n\n");
115 else if (!strcasecmp(tag, "H2")) {
116 strcat(outbuf, "\n\n");
119 else if (!strcasecmp(tag, "H3")) {
120 strcat(outbuf, "\n\n");
123 else if (!strcasecmp(tag, "H4")) {
124 strcat(outbuf, "\n\n");
127 else if (!strcasecmp(tag, "/H1")) {
128 strcat(outbuf, "\n");
131 else if (!strcasecmp(tag, "/H2")) {
132 strcat(outbuf, "\n");
135 else if (!strcasecmp(tag, "/H3")) {
136 strcat(outbuf, "\n");
139 else if (!strcasecmp(tag, "/H4")) {
140 strcat(outbuf, "\n");
143 else if (!strcasecmp(tag, "HR")) {
144 strcat(outbuf, "\n ");
145 for (i=0; i<screenwidth-2; ++i)
147 strcat(outbuf, "\n");
150 else if (!strcasecmp(tag, "BR")) {
151 strcat(outbuf, "\n");
154 else if (!strcasecmp(tag, "TR")) {
155 strcat(outbuf, "\n");
158 else if (!strcasecmp(tag, "/TABLE")) {
159 strcat(outbuf, "\n");
164 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
165 tag[strlen(tag)+1] = 0;
166 tag[strlen(tag)] = ch;
170 outbuf[strlen(outbuf)+1] = 0;
171 outbuf[strlen(outbuf)] = ch;
174 strcpy(inbuf, &inbuf[i]);
177 lprintf(9, "checkquepoynte\n");
179 /* Convert &; tags to the forbidden characters */
180 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
181 lprintf(9, "eek loop\n");
183 if (!strncasecmp(&outbuf[i], " ", 6)) {
185 strcpy(&outbuf[i+1], &outbuf[i+6]);
188 else if (!strncasecmp(&outbuf[i], "&lb;", 4)) {
190 strcpy(&outbuf[i+1], &outbuf[i+4]);
193 else if (!strncasecmp(&outbuf[i], "&rb;", 4)) {
195 strcpy(&outbuf[i+1], &outbuf[i+4]);
198 else if (!strncasecmp(&outbuf[i], "&", 5)) {
199 strcpy(&outbuf[i+1], &outbuf[i+5]);
202 else if (!strncasecmp(&outbuf[i], """, 6)) {
204 strcpy(&outbuf[i+1], &outbuf[i+6]);
209 /* Make sure the output buffer is big enough */
210 if ((strlen(outputbuf) + strlen(outbuf) + 2) > output_length) {
211 lprintf(9, "realloc loop\n");
212 output_length = output_length + strlen(outbuf) + 2;
213 outputbuf = reallok(outputbuf, output_length);
216 /* Output any lines terminated with hard line breaks */
217 lprintf(9, "output loop 1\n");
220 if (strlen(outbuf)>0)
221 for (i = 0; i<strlen(outbuf); ++i) {
222 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
223 strncat(outputbuf, outbuf, i+1);
224 strcpy(outbuf, &outbuf[i+1]);
231 /* Add soft line breaks */
232 lprintf(9, "output loop 2\n");
233 if (strlen(outbuf) > (screenwidth - 2)) {
235 for (i=0; i<(screenwidth-2); ++i) {
236 if (outbuf[i]==32) rb = i;
239 strncat(outputbuf, outbuf, rb);
240 strcat(outputbuf, "\n");
241 strcpy(outbuf, &outbuf[rb+1]);
243 strncat(outputbuf, outbuf, screenwidth-2);
244 strcat(outputbuf, "\n");
245 strcpy(outbuf, &outbuf[screenwidth-2]);
249 } while (done_reading == 0);
250 lprintf(9, "output loop 3\n");
251 strncat(outputbuf, outbuf, strlen(outbuf));
252 strcat(outputbuf, "\n");