4 * Functions which handle translation between HTML and plain text
22 #include "sysdep_decls.h"
32 * Convert HTML to plain text.
34 * inputmsg = pointer to raw HTML message
35 * screenwidth = desired output screenwidth
36 * do_citaformat = set to 1 to indent newlines with spaces
38 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
46 int i, j, ch, did_out, rb;
47 int nest = 0; /* Bracket nesting level */
53 outptr = mallok(strlen(inptr) + SIZ);
54 if (outptr == NULL) return NULL;
59 /* Fill the input buffer */
60 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
64 inbuf[strlen(inbuf)+1] = 0;
65 inbuf[strlen(inbuf)] = ch;
74 if (strlen(inbuf)>0) {
76 /* Fold in all the spacing */
77 for (i=0; i<strlen(inbuf); ++i) {
78 if (inbuf[i]==10) inbuf[i]=32;
79 if (inbuf[i]==13) inbuf[i]=32;
80 if (inbuf[i]==9) inbuf[i]=32;
81 if ((inbuf[i]<32) || (inbuf[i]>126))
82 strcpy(&inbuf[i], &inbuf[i+1]);
83 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
84 strcpy(&inbuf[i], &inbuf[i+1]);
87 for (i=0; i<strlen(inbuf); ++i) {
99 if (!strcasecmp(tag, "P")) {
100 strcat(outbuf, "\n\n");
103 if (!strcasecmp(tag, "/DIV")) {
104 strcat(outbuf, "\n\n");
107 else if (!strcasecmp(tag, "H1")) {
108 strcat(outbuf, "\n\n");
111 else if (!strcasecmp(tag, "H2")) {
112 strcat(outbuf, "\n\n");
115 else if (!strcasecmp(tag, "H3")) {
116 strcat(outbuf, "\n\n");
119 else if (!strcasecmp(tag, "H4")) {
120 strcat(outbuf, "\n\n");
123 else if (!strcasecmp(tag, "/H1")) {
124 strcat(outbuf, "\n");
127 else if (!strcasecmp(tag, "/H2")) {
128 strcat(outbuf, "\n");
131 else if (!strcasecmp(tag, "/H3")) {
132 strcat(outbuf, "\n");
135 else if (!strcasecmp(tag, "/H4")) {
136 strcat(outbuf, "\n");
139 else if (!strcasecmp(tag, "HR")) {
140 strcat(outbuf, "\n ");
141 for (j=0; j<screenwidth-2; ++j)
143 strcat(outbuf, "\n");
146 else if (!strcasecmp(tag, "BR")) {
147 strcat(outbuf, "\n");
150 else if (!strcasecmp(tag, "TR")) {
151 strcat(outbuf, "\n");
154 else if (!strcasecmp(tag, "/TABLE")) {
155 strcat(outbuf, "\n");
160 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
161 tag[strlen(tag)+1] = 0;
162 tag[strlen(tag)] = ch;
166 outbuf[strlen(outbuf)+1] = 0;
167 outbuf[strlen(outbuf)] = ch;
170 strcpy(inbuf, &inbuf[i]);
173 /* Convert &; tags to the forbidden characters */
174 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
176 if (!strncasecmp(&outbuf[i], " ", 6)) {
178 strcpy(&outbuf[i+1], &outbuf[i+6]);
181 else if (!strncasecmp(&outbuf[i], "<", 4)) {
183 strcpy(&outbuf[i+1], &outbuf[i+4]);
186 else if (!strncasecmp(&outbuf[i], ">", 4)) {
188 strcpy(&outbuf[i+1], &outbuf[i+4]);
191 else if (!strncasecmp(&outbuf[i], "&", 5)) {
192 strcpy(&outbuf[i+1], &outbuf[i+5]);
195 else if (!strncasecmp(&outbuf[i], """, 6)) {
197 strcpy(&outbuf[i+1], &outbuf[i+6]);
200 else if (!strncasecmp(&outbuf[i], "©", 6)) {
204 strcpy(&outbuf[i+3], &outbuf[i+6]);
207 else if (!strncasecmp(&outbuf[i], "®", 5)) {
211 strcpy(&outbuf[i+3], &outbuf[i+5]);
216 /* Make sure the output buffer is big enough */
217 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
219 outptr = realloc(outptr, outlen);
222 /* Output any lines terminated with hard line breaks */
225 if (strlen(outbuf)>0)
226 for (i = 0; i<strlen(outbuf); ++i) {
227 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
228 strncat(outptr, outbuf, i+1);
229 strcat(outptr, "\n");
232 strcpy(outbuf, &outbuf[i+1]);
239 /* Add soft line breaks */
240 if (strlen(outbuf) > (screenwidth - 2)) {
242 for (i=0; i<(screenwidth-2); ++i) {
243 if (outbuf[i]==32) rb = i;
246 strncat(outptr, outbuf, rb);
247 strcat(outptr, "\n");
250 strcpy(outbuf, &outbuf[rb+1]);
253 strncat(outptr, outbuf, screenwidth-2);
254 strcat(outptr, "\n");
257 strcpy(outbuf, &outbuf[screenwidth-2]);
261 } while (done_reading == 0);
263 strcat(outptr, outbuf);
264 strcat(outptr, "\n");