4 * Functions which handle translation between HTML and plain text
5 * Copyright (c) 2000-2005 by Art Cancro and others. This program is
6 * released under the terms of the GNU General Public License.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
33 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
45 * Convert HTML to plain text.
47 * inputmsg = pointer to raw HTML message
48 * screenwidth = desired output screenwidth
49 * do_citaformat = set to 1 to indent newlines with spaces
51 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
58 size_t outptr_buffer_size;
59 size_t output_len = 0;
60 int i, j, ch, did_out, rb, scanch;
61 int nest = 0; /* Bracket nesting level */
62 int blockquote = 0; /* BLOCKQUOTE nesting level */
63 int bytes_processed = 0;
68 if (msglen == 0) msglen = strlen(inputmsg);
70 outptr_buffer_size = strlen(inptr) + SIZ;
71 outptr = malloc(outptr_buffer_size);
72 if (outptr == NULL) return NULL;
77 /* Fill the input buffer */
78 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
82 inbuf[strlen(inbuf)+1] = 0;
83 inbuf[strlen(inbuf)] = ch;
90 if (bytes_processed > msglen) {
97 if (strlen(inbuf)>0) {
99 /* Fold in all the spacing */
100 for (i=0; i<strlen(inbuf); ++i) {
101 if (inbuf[i]==10) inbuf[i]=32;
102 if (inbuf[i]==13) inbuf[i]=32;
103 if (inbuf[i]==9) inbuf[i]=32;
104 /*** we like foreign characters now.
105 if ((inbuf[i]<32) || (inbuf[i]>126)) {
109 for (i=0; i<strlen(inbuf); ++i) {
110 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
111 strcpy(&inbuf[i], &inbuf[i+1]);
114 for (i=0; i<strlen(inbuf); ++i) {
123 else if (ch == '>') { /* We have a tag. */
124 if (nest > 0) --nest;
126 /* Unqualify the tag (truncate at first space) */
127 if (strchr(tag, ' ') != NULL) {
128 strcpy(strchr(tag, ' '), "");
131 if (!strcasecmp(tag, "P")) {
132 strcat(outbuf, "\n\n");
135 if (!strcasecmp(tag, "/DIV")) {
136 strcat(outbuf, "\n\n");
139 if (!strcasecmp(tag, "LI")) {
140 strcat(outbuf, "\n * ");
143 else if (!strcasecmp(tag, "/UL")) {
144 strcat(outbuf, "\n\n");
147 else if (!strcasecmp(tag, "H1")) {
148 strcat(outbuf, "\n\n");
151 else if (!strcasecmp(tag, "H2")) {
152 strcat(outbuf, "\n\n");
155 else if (!strcasecmp(tag, "H3")) {
156 strcat(outbuf, "\n\n");
159 else if (!strcasecmp(tag, "H4")) {
160 strcat(outbuf, "\n\n");
163 else if (!strcasecmp(tag, "/H1")) {
164 strcat(outbuf, "\n");
167 else if (!strcasecmp(tag, "/H2")) {
168 strcat(outbuf, "\n");
171 else if (!strcasecmp(tag, "/H3")) {
172 strcat(outbuf, "\n");
175 else if (!strcasecmp(tag, "/H4")) {
176 strcat(outbuf, "\n");
179 else if (!strcasecmp(tag, "HR")) {
180 strcat(outbuf, "\n ");
181 for (j=0; j<screenwidth-2; ++j)
183 strcat(outbuf, "\n");
186 else if (!strcasecmp(tag, "BR")) {
187 strcat(outbuf, "\n");
190 else if (!strcasecmp(tag, "TR")) {
191 strcat(outbuf, "\n");
194 else if (!strcasecmp(tag, "/TABLE")) {
195 strcat(outbuf, "\n");
198 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
199 strcat(outbuf, "\n\n <<\n");
203 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
204 strcat(outbuf, "\n >>\n\n");
210 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
211 tag[strlen(tag)+1] = 0;
212 tag[strlen(tag)] = ch;
216 outbuf[strlen(outbuf)+1] = 0;
217 outbuf[strlen(outbuf)] = ch;
220 strcpy(inbuf, &inbuf[i]);
223 /* Convert &; tags to the forbidden characters */
224 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
226 /* Character entity references */
227 if (!strncasecmp(&outbuf[i], " ", 6)) {
229 strcpy(&outbuf[i+1], &outbuf[i+6]);
232 if (!strncasecmp(&outbuf[i], " ", 6)) {
234 strcpy(&outbuf[i+1], &outbuf[i+6]);
237 if (!strncasecmp(&outbuf[i], " ", 6)) {
239 strcpy(&outbuf[i+1], &outbuf[i+6]);
242 if (!strncasecmp(&outbuf[i], " ", 8)) {
244 strcpy(&outbuf[i+1], &outbuf[i+8]);
247 else if (!strncasecmp(&outbuf[i], "<", 4)) {
249 strcpy(&outbuf[i+1], &outbuf[i+4]);
252 else if (!strncasecmp(&outbuf[i], ">", 4)) {
254 strcpy(&outbuf[i+1], &outbuf[i+4]);
257 else if (!strncasecmp(&outbuf[i], "&", 5)) {
258 strcpy(&outbuf[i+1], &outbuf[i+5]);
261 else if (!strncasecmp(&outbuf[i], """, 6)) {
263 strcpy(&outbuf[i+1], &outbuf[i+6]);
266 else if (!strncasecmp(&outbuf[i], "‘", 7)) {
268 strcpy(&outbuf[i+1], &outbuf[i+7]);
271 else if (!strncasecmp(&outbuf[i], "’", 7)) {
273 strcpy(&outbuf[i+1], &outbuf[i+7]);
276 else if (!strncasecmp(&outbuf[i], "©", 6)) {
280 strcpy(&outbuf[i+3], &outbuf[i+6]);
283 else if (!strncasecmp(&outbuf[i], "…", 8)) {
287 strcpy(&outbuf[i+3], &outbuf[i+8]);
290 else if (!strncasecmp(&outbuf[i], "™", 7)) {
295 strcpy(&outbuf[i+4], &outbuf[i+7]);
298 else if (!strncasecmp(&outbuf[i], "®", 5)) {
302 strcpy(&outbuf[i+3], &outbuf[i+5]);
305 else if (!strncasecmp(&outbuf[i], "¼", 8)) {
309 strcpy(&outbuf[i+3], &outbuf[i+8]);
312 else if (!strncasecmp(&outbuf[i], "½", 8)) {
316 strcpy(&outbuf[i+3], &outbuf[i+8]);
319 else if (!strncasecmp(&outbuf[i], "¾", 8)) {
323 strcpy(&outbuf[i+3], &outbuf[i+8]);
326 else if (!strncasecmp(&outbuf[i], "–", 7)) {
329 strcpy(&outbuf[i+2], &outbuf[i+7]);
332 else if (!strncasecmp(&outbuf[i], "—", 7)) {
336 strcpy(&outbuf[i+3], &outbuf[i+7]);
339 /* two-digit decimal equivalents */
340 else if ((!strncmp(&outbuf[i], "&#", 2))
341 && (outbuf[i+4] == ';') ) {
343 sscanf(&outbuf[i+2], "%02d", &scanch);
345 strcpy(&outbuf[i+1], &outbuf[i+5]);
348 /* three-digit decimal equivalents */
349 else if ((!strncmp(&outbuf[i], "&#", 2))
350 && (outbuf[i+5] == ';') ) {
352 sscanf(&outbuf[i+2], "%03d", &scanch);
354 strcpy(&outbuf[i+1], &outbuf[i+6]);
359 /* Make sure the output buffer is big enough */
360 if ((output_len + strlen(outbuf) + SIZ)
361 > outptr_buffer_size) {
362 outptr_buffer_size += SIZ;
363 outptr = realloc(outptr, outptr_buffer_size);
366 /* Output any lines terminated with hard line breaks */
369 if (strlen(outbuf)>0) {
370 for (i = 0; i<strlen(outbuf); ++i) {
371 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
373 strncpy(&outptr[output_len],
378 strcpy(&outptr[output_len],
383 strcpy(outbuf, &outbuf[i+1]);
391 /* Add soft line breaks */
392 if (strlen(outbuf) > (screenwidth - 2 )) {
394 for (i=0; i<(screenwidth-2); ++i) {
395 if (outbuf[i]==32) rb = i;
398 strncpy(&outptr[output_len], outbuf, rb);
400 strcpy(&outptr[output_len], "\n");
403 strcpy(&outptr[output_len], " ");
406 strcpy(outbuf, &outbuf[rb+1]);
408 strncpy(&outptr[output_len], outbuf,
410 output_len += (screenwidth-2);
411 strcpy(&outptr[output_len], "\n");
414 strcpy(&outptr[output_len], " ");
417 strcpy(outbuf, &outbuf[screenwidth-2]);
421 } while (done_reading == 0);
423 strcpy(&outptr[output_len], outbuf);
424 output_len += strlen(outbuf);
426 /* Strip leading/trailing whitespace. We can't do this with
427 * striplt() because it uses too many strlen()'s
429 while ((output_len > 0) && (isspace(outptr[0]))) {
430 strcpy(outptr, &outptr[1]);
433 while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
434 outptr[output_len-1] = 0;
438 if (outptr[output_len-1] != '\n') {
439 strcat(outptr, "\n");