2 * html.c -- Functions which handle translation between HTML and plain text
24 #include "sysdep_decls.h"
34 * Convert HTML to plain text.
36 * inputmsg = pointer to raw HTML message
37 * screenwidth = desired output screenwidth
38 * do_citaformat = set to 1 to indent newlines with spaces
40 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
48 int i, j, ch, did_out, rb;
49 int nest = 0; /* Bracket nesting level */
55 outptr = mallok(strlen(inptr) + 256);
56 if (outptr == NULL) return NULL;
61 /* Fill the input buffer */
62 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
64 /* FIX ... genericize this */
67 inbuf[strlen(inbuf)+1] = 0;
68 inbuf[strlen(inbuf)] = ch;
77 if (strlen(inbuf)>0) {
79 /* Fold in all the spacing */
80 for (i=0; i<strlen(inbuf); ++i) {
81 if (inbuf[i]==10) inbuf[i]=32;
82 if (inbuf[i]==13) inbuf[i]=32;
83 if (inbuf[i]==9) inbuf[i]=32;
84 if ((inbuf[i]<32) || (inbuf[i]>126))
85 strcpy(&inbuf[i], &inbuf[i+1]);
86 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
87 strcpy(&inbuf[i], &inbuf[i+1]);
90 for (i=0; i<strlen(inbuf); ++i) {
100 if (nest > 0) --nest;
102 if (!strcasecmp(tag, "P")) {
103 strcat(outbuf, "\n\n");
106 if (!strcasecmp(tag, "/DIV")) {
107 strcat(outbuf, "\n\n");
110 else if (!strcasecmp(tag, "H1")) {
111 strcat(outbuf, "\n\n");
114 else if (!strcasecmp(tag, "H2")) {
115 strcat(outbuf, "\n\n");
118 else if (!strcasecmp(tag, "H3")) {
119 strcat(outbuf, "\n\n");
122 else if (!strcasecmp(tag, "H4")) {
123 strcat(outbuf, "\n\n");
126 else if (!strcasecmp(tag, "/H1")) {
127 strcat(outbuf, "\n");
130 else if (!strcasecmp(tag, "/H2")) {
131 strcat(outbuf, "\n");
134 else if (!strcasecmp(tag, "/H3")) {
135 strcat(outbuf, "\n");
138 else if (!strcasecmp(tag, "/H4")) {
139 strcat(outbuf, "\n");
142 else if (!strcasecmp(tag, "HR")) {
143 strcat(outbuf, "\n ");
144 for (j=0; j<screenwidth-2; ++j)
146 strcat(outbuf, "\n");
149 else if (!strcasecmp(tag, "BR")) {
150 strcat(outbuf, "\n");
153 else if (!strcasecmp(tag, "TR")) {
154 strcat(outbuf, "\n");
157 else if (!strcasecmp(tag, "/TABLE")) {
158 strcat(outbuf, "\n");
163 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
164 tag[strlen(tag)+1] = 0;
165 tag[strlen(tag)] = ch;
169 outbuf[strlen(outbuf)+1] = 0;
170 outbuf[strlen(outbuf)] = ch;
173 strcpy(inbuf, &inbuf[i]);
176 /* Convert &; tags to the forbidden characters */
177 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
179 if (!strncasecmp(&outbuf[i], " ", 6)) {
181 strcpy(&outbuf[i+1], &outbuf[i+6]);
184 else if (!strncasecmp(&outbuf[i], "<", 4)) {
186 strcpy(&outbuf[i+1], &outbuf[i+4]);
189 else if (!strncasecmp(&outbuf[i], ">", 4)) {
191 strcpy(&outbuf[i+1], &outbuf[i+4]);
194 else if (!strncasecmp(&outbuf[i], "&", 5)) {
195 strcpy(&outbuf[i+1], &outbuf[i+5]);
198 else if (!strncasecmp(&outbuf[i], """, 6)) {
200 strcpy(&outbuf[i+1], &outbuf[i+6]);
203 else if (!strncasecmp(&outbuf[i], "©", 6)) {
207 strcpy(&outbuf[i+3], &outbuf[i+6]);
210 else if (!strncasecmp(&outbuf[i], "®", 5)) {
214 strcpy(&outbuf[i+3], &outbuf[i+5]);
219 /* Make sure the output buffer is big enough */
220 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
222 outptr = realloc(outptr, outlen);
225 /* Output any lines terminated with hard line breaks */
228 if (strlen(outbuf)>0)
229 for (i = 0; i<strlen(outbuf); ++i) {
230 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
231 strncat(outptr, outbuf, i+1);
232 strcat(outptr, "\n");
235 strcpy(outbuf, &outbuf[i+1]);
242 /* Add soft line breaks */
243 if (strlen(outbuf) > (screenwidth - 2)) {
245 for (i=0; i<(screenwidth-2); ++i) {
246 if (outbuf[i]==32) rb = i;
249 strncat(outptr, outbuf, rb);
250 strcat(outptr, "\n");
253 strcpy(outbuf, &outbuf[rb+1]);
256 strncat(outptr, outbuf, screenwidth-2);
257 strcat(outptr, "\n");
260 strcpy(outbuf, &outbuf[screenwidth-2]);
264 } while (done_reading == 0);
266 strcat(outptr, outbuf);
267 strcat(outptr, "\n");