4 * Functions which handle translation between HTML and plain text
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
36 #include "dynloader.h"
38 #include "sysdep_decls.h"
48 * Convert HTML to plain text.
50 * inputmsg = pointer to raw HTML message
51 * screenwidth = desired output screenwidth
52 * do_citaformat = set to 1 to indent newlines with spaces
54 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
62 int i, j, ch, did_out, rb;
63 int nest = 0; /* Bracket nesting level */
69 outptr = mallok(strlen(inptr) + SIZ);
70 if (outptr == NULL) return NULL;
75 /* Fill the input buffer */
76 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
80 inbuf[strlen(inbuf)+1] = 0;
81 inbuf[strlen(inbuf)] = ch;
90 if (strlen(inbuf)>0) {
92 /* Fold in all the spacing */
93 for (i=0; i<strlen(inbuf); ++i) {
94 if (inbuf[i]==10) inbuf[i]=32;
95 if (inbuf[i]==13) inbuf[i]=32;
96 if (inbuf[i]==9) inbuf[i]=32;
97 if ((inbuf[i]<32) || (inbuf[i]>126))
98 strcpy(&inbuf[i], &inbuf[i+1]);
99 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
100 strcpy(&inbuf[i], &inbuf[i+1]);
103 for (i=0; i<strlen(inbuf); ++i) {
112 else if (ch == '>') {
113 if (nest > 0) --nest;
115 if (!strcasecmp(tag, "P")) {
116 strcat(outbuf, "\n\n");
119 if (!strcasecmp(tag, "/DIV")) {
120 strcat(outbuf, "\n\n");
123 else if (!strcasecmp(tag, "H1")) {
124 strcat(outbuf, "\n\n");
127 else if (!strcasecmp(tag, "H2")) {
128 strcat(outbuf, "\n\n");
131 else if (!strcasecmp(tag, "H3")) {
132 strcat(outbuf, "\n\n");
135 else if (!strcasecmp(tag, "H4")) {
136 strcat(outbuf, "\n\n");
139 else if (!strcasecmp(tag, "/H1")) {
140 strcat(outbuf, "\n");
143 else if (!strcasecmp(tag, "/H2")) {
144 strcat(outbuf, "\n");
147 else if (!strcasecmp(tag, "/H3")) {
148 strcat(outbuf, "\n");
151 else if (!strcasecmp(tag, "/H4")) {
152 strcat(outbuf, "\n");
155 else if (!strcasecmp(tag, "HR")) {
156 strcat(outbuf, "\n ");
157 for (j=0; j<screenwidth-2; ++j)
159 strcat(outbuf, "\n");
162 else if (!strcasecmp(tag, "BR")) {
163 strcat(outbuf, "\n");
166 else if (!strcasecmp(tag, "TR")) {
167 strcat(outbuf, "\n");
170 else if (!strcasecmp(tag, "/TABLE")) {
171 strcat(outbuf, "\n");
176 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
177 tag[strlen(tag)+1] = 0;
178 tag[strlen(tag)] = ch;
182 outbuf[strlen(outbuf)+1] = 0;
183 outbuf[strlen(outbuf)] = ch;
186 strcpy(inbuf, &inbuf[i]);
189 /* Convert &; tags to the forbidden characters */
190 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
192 if (!strncasecmp(&outbuf[i], " ", 6)) {
194 strcpy(&outbuf[i+1], &outbuf[i+6]);
197 else if (!strncasecmp(&outbuf[i], "<", 4)) {
199 strcpy(&outbuf[i+1], &outbuf[i+4]);
202 else if (!strncasecmp(&outbuf[i], ">", 4)) {
204 strcpy(&outbuf[i+1], &outbuf[i+4]);
207 else if (!strncasecmp(&outbuf[i], "&", 5)) {
208 strcpy(&outbuf[i+1], &outbuf[i+5]);
211 else if (!strncasecmp(&outbuf[i], """, 6)) {
213 strcpy(&outbuf[i+1], &outbuf[i+6]);
216 else if (!strncasecmp(&outbuf[i], "©", 6)) {
220 strcpy(&outbuf[i+3], &outbuf[i+6]);
223 else if (!strncasecmp(&outbuf[i], "®", 5)) {
227 strcpy(&outbuf[i+3], &outbuf[i+5]);
232 /* Make sure the output buffer is big enough */
233 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
235 outptr = realloc(outptr, outlen);
238 /* Output any lines terminated with hard line breaks */
241 if (strlen(outbuf)>0)
242 for (i = 0; i<strlen(outbuf); ++i) {
243 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
244 strncat(outptr, outbuf, i+1);
245 strcat(outptr, "\n");
248 strcpy(outbuf, &outbuf[i+1]);
255 /* Add soft line breaks */
256 if (strlen(outbuf) > (screenwidth - 2)) {
258 for (i=0; i<(screenwidth-2); ++i) {
259 if (outbuf[i]==32) rb = i;
262 strncat(outptr, outbuf, rb);
263 strcat(outptr, "\n");
266 strcpy(outbuf, &outbuf[rb+1]);
269 strncat(outptr, outbuf, screenwidth-2);
270 strcat(outptr, "\n");
273 strcpy(outbuf, &outbuf[screenwidth-2]);
277 } while (done_reading == 0);
279 strcat(outptr, outbuf);
280 strcat(outptr, "\n");