4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2001 by Art Cancro and others. This program is
6 * released under the terms of the GNU General Public License.
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
38 #include "dynloader.h"
40 #include "sysdep_decls.h"
50 * Convert HTML to plain text.
52 * inputmsg = pointer to raw HTML message
53 * screenwidth = desired output screenwidth
54 * do_citaformat = set to 1 to indent newlines with spaces
56 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
64 int i, j, ch, did_out, rb;
65 int nest = 0; /* Bracket nesting level */
71 outptr = mallok(strlen(inptr) + SIZ);
72 if (outptr == NULL) return NULL;
77 /* Fill the input buffer */
78 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
82 inbuf[strlen(inbuf)+1] = 0;
83 inbuf[strlen(inbuf)] = ch;
92 if (strlen(inbuf)>0) {
94 /* Fold in all the spacing */
95 for (i=0; i<strlen(inbuf); ++i) {
96 if (inbuf[i]==10) inbuf[i]=32;
97 if (inbuf[i]==13) inbuf[i]=32;
98 if (inbuf[i]==9) inbuf[i]=32;
99 if ((inbuf[i]<32) || (inbuf[i]>126))
100 strcpy(&inbuf[i], &inbuf[i+1]);
102 for (i=0; i<strlen(inbuf); ++i) {
103 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
104 strcpy(&inbuf[i], &inbuf[i+1]);
107 for (i=0; i<strlen(inbuf); ++i) {
116 else if (ch == '>') {
117 if (nest > 0) --nest;
119 if (!strcasecmp(tag, "P")) {
120 strcat(outbuf, "\n\n");
123 if (!strcasecmp(tag, "/DIV")) {
124 strcat(outbuf, "\n\n");
127 else if (!strcasecmp(tag, "H1")) {
128 strcat(outbuf, "\n\n");
131 else if (!strcasecmp(tag, "H2")) {
132 strcat(outbuf, "\n\n");
135 else if (!strcasecmp(tag, "H3")) {
136 strcat(outbuf, "\n\n");
139 else if (!strcasecmp(tag, "H4")) {
140 strcat(outbuf, "\n\n");
143 else if (!strcasecmp(tag, "/H1")) {
144 strcat(outbuf, "\n");
147 else if (!strcasecmp(tag, "/H2")) {
148 strcat(outbuf, "\n");
151 else if (!strcasecmp(tag, "/H3")) {
152 strcat(outbuf, "\n");
155 else if (!strcasecmp(tag, "/H4")) {
156 strcat(outbuf, "\n");
159 else if (!strcasecmp(tag, "HR")) {
160 strcat(outbuf, "\n ");
161 for (j=0; j<screenwidth-2; ++j)
163 strcat(outbuf, "\n");
166 else if (!strcasecmp(tag, "BR")) {
167 strcat(outbuf, "\n");
170 else if (!strcasecmp(tag, "TR")) {
171 strcat(outbuf, "\n");
174 else if (!strcasecmp(tag, "/TABLE")) {
175 strcat(outbuf, "\n");
180 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
181 tag[strlen(tag)+1] = 0;
182 tag[strlen(tag)] = ch;
186 outbuf[strlen(outbuf)+1] = 0;
187 outbuf[strlen(outbuf)] = ch;
190 strcpy(inbuf, &inbuf[i]);
193 /* Convert &; tags to the forbidden characters */
194 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
196 if (!strncasecmp(&outbuf[i], " ", 6)) {
198 strcpy(&outbuf[i+1], &outbuf[i+6]);
201 else if (!strncasecmp(&outbuf[i], "<", 4)) {
203 strcpy(&outbuf[i+1], &outbuf[i+4]);
206 else if (!strncasecmp(&outbuf[i], ">", 4)) {
208 strcpy(&outbuf[i+1], &outbuf[i+4]);
211 else if (!strncasecmp(&outbuf[i], "&", 5)) {
212 strcpy(&outbuf[i+1], &outbuf[i+5]);
215 else if (!strncasecmp(&outbuf[i], """, 6)) {
217 strcpy(&outbuf[i+1], &outbuf[i+6]);
220 else if (!strncasecmp(&outbuf[i], "©", 6)) {
224 strcpy(&outbuf[i+3], &outbuf[i+6]);
227 else if (!strncasecmp(&outbuf[i], "®", 5)) {
231 strcpy(&outbuf[i+3], &outbuf[i+5]);
236 /* Make sure the output buffer is big enough */
237 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
239 outptr = realloc(outptr, outlen);
242 /* Output any lines terminated with hard line breaks */
245 if (strlen(outbuf)>0)
246 for (i = 0; i<strlen(outbuf); ++i) {
247 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
248 strncat(outptr, outbuf, i+1);
249 /* strcat(outptr, "\n"); */
252 strcpy(outbuf, &outbuf[i+1]);
259 /* Add soft line breaks */
260 if (strlen(outbuf) > (screenwidth - 2)) {
262 for (i=0; i<(screenwidth-2); ++i) {
263 if (outbuf[i]==32) rb = i;
266 strncat(outptr, outbuf, rb);
267 strcat(outptr, "\n");
270 strcpy(outbuf, &outbuf[rb+1]);
273 strncat(outptr, outbuf, screenwidth-2);
274 strcat(outptr, "\n");
277 strcpy(outbuf, &outbuf[screenwidth-2]);
281 } while (done_reading == 0);
283 strcat(outptr, outbuf);
285 if (outptr[strlen(outptr)-1] != '\n') strcat(outptr, "\n");