2 * html.c -- Functions which handle translation between HTML and plain text
14 * Convert HTML to plain text.
16 void html_to_ascii(int screenwidth) {
22 int i, ch, did_out, rb;
23 int nest = 0; /* Bracket nesting level */
29 /* Fill the input buffer */
30 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
31 /* FIX ... genericize this */
32 ptr = fgets(&inbuf[strlen(inbuf)], 127, stdin);
33 if (ptr == NULL) done_reading = 1;
37 if (strlen(inbuf)>0) {
39 /* Fold in all the spacing */
40 for (i=0; i<strlen(inbuf); ++i) {
41 if (inbuf[i]==10) inbuf[i]=32;
42 if (inbuf[i]==13) inbuf[i]=32;
43 if (inbuf[i]==9) inbuf[i]=32;
44 if ((inbuf[i]<32) || (inbuf[i]>126))
45 strcpy(&inbuf[i], &inbuf[i+1]);
46 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
47 strcpy(&inbuf[i], &inbuf[i+1]);
50 for (i=0; i<strlen(inbuf); ++i) {
62 if (!strcasecmp(tag, "P")) {
63 strcat(outbuf, "\n\n");
66 else if (!strcasecmp(tag, "H1")) {
67 strcat(outbuf, "\n\n");
70 else if (!strcasecmp(tag, "H2")) {
71 strcat(outbuf, "\n\n");
74 else if (!strcasecmp(tag, "H3")) {
75 strcat(outbuf, "\n\n");
78 else if (!strcasecmp(tag, "H4")) {
79 strcat(outbuf, "\n\n");
82 else if (!strcasecmp(tag, "/H1")) {
86 else if (!strcasecmp(tag, "/H2")) {
90 else if (!strcasecmp(tag, "/H3")) {
94 else if (!strcasecmp(tag, "/H4")) {
98 else if (!strcasecmp(tag, "HR")) {
99 strcat(outbuf, "\n ");
100 for (i=0; i<screenwidth-2; ++i)
102 strcat(outbuf, "\n");
105 else if (!strcasecmp(tag, "BR")) {
106 strcat(outbuf, "\n");
109 else if (!strcasecmp(tag, "TR")) {
110 strcat(outbuf, "\n");
113 else if (!strcasecmp(tag, "/TABLE")) {
114 strcat(outbuf, "\n");
119 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
120 tag[strlen(tag)+1] = 0;
121 tag[strlen(tag)] = ch;
125 outbuf[strlen(outbuf)+1] = 0;
126 outbuf[strlen(outbuf)] = ch;
129 strcpy(inbuf, &inbuf[i]);
132 /* Convert &; tags to the forbidden characters */
133 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
135 if (!strncasecmp(&outbuf[i], " ", 6)) {
137 strcpy(&outbuf[i+1], &outbuf[i+6]);
140 else if (!strncasecmp(&outbuf[i], "&lb;", 4)) {
142 strcpy(&outbuf[i+1], &outbuf[i+4]);
145 else if (!strncasecmp(&outbuf[i], "&rb;", 4)) {
147 strcpy(&outbuf[i+1], &outbuf[i+4]);
150 else if (!strncasecmp(&outbuf[i], "&", 5)) {
151 strcpy(&outbuf[i+1], &outbuf[i+5]);
154 else if (!strncasecmp(&outbuf[i], """, 6)) {
156 strcpy(&outbuf[i+1], &outbuf[i+6]);
161 /* Output any lines terminated with hard line breaks */
164 if (strlen(outbuf)>0)
165 for (i = 0; i<strlen(outbuf); ++i) {
166 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
167 fwrite(outbuf, i+1, 1, stdout);
168 strcpy(outbuf, &outbuf[i+1]);
175 /* Add soft line breaks */
176 if (strlen(outbuf) > (screenwidth - 2)) {
178 for (i=0; i<(screenwidth-2); ++i) {
179 if (outbuf[i]==32) rb = i;
182 fwrite(outbuf, rb, 1, stdout);
183 fwrite("\n", 1, 1, stdout);
184 strcpy(outbuf, &outbuf[rb+1]);
186 fwrite(outbuf, screenwidth-2, 1, stdout);
187 fwrite("\n", 1, 1, stdout);
188 strcpy(outbuf, &outbuf[screenwidth-2]);
192 } while (done_reading == 0);
193 fwrite(outbuf, strlen(outbuf), 1, stdout);
194 fwrite("\n", 1, 1, stdout);
200 * Temporary main loop for testing