* sample aproach to use the citadel commandline client html renderer to create a...
[citadel.git] / webcit / html.c
1 /*
2  * $Id: html.c 6014 2008-02-04 18:38:35Z ajc $
3  *
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.
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <libcitadel.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "control.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "room_ops.h"
40 #include "html.h"
41  
42
43 /*
44  * Convert HTML to plain text.
45  *
46  * inputmsg      = pointer to raw HTML message
47  * screenwidth   = desired output screenwidth
48  * do_citaformat = set to 1 to indent newlines with spaces
49  */
50 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
51         char inbuf[SIZ];
52         int inbuf_len = 0;
53         char outbuf[SIZ];
54         char tag[1024];
55         int done_reading = 0;
56         char *inptr;
57         char *outptr;
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 styletag = 0;       /* STYLE tag nesting level */
64         int styletag_start = 0;
65         int bytes_processed = 0;
66         char nl[128];
67
68         strcpy(nl, "\n");
69         inptr = inputmsg;
70         strcpy(inbuf, "");
71         strcpy(outbuf, "");
72         if (msglen == 0) msglen = strlen(inputmsg);
73
74         outptr_buffer_size = strlen(inptr) + SIZ;
75         outptr = malloc(outptr_buffer_size);
76         if (outptr == NULL) return NULL;
77         strcpy(outptr, "");
78         output_len = 0;
79
80         do {
81                 /* Fill the input buffer */
82                 inbuf_len = strlen(inbuf);
83                 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
84
85                         ch = *inptr++;
86                         if (ch != 0) {
87                                 inbuf[inbuf_len++] = ch;
88                                 inbuf[inbuf_len] = 0;
89                         } 
90                         else {
91                                 done_reading = 1;
92                         }
93
94                         ++bytes_processed;
95                         if (bytes_processed > msglen) {
96                                 done_reading = 1;
97                         }
98
99                 }
100
101                 /* Do some parsing */
102                 if (!IsEmptyStr(inbuf)) {
103
104
105                     /* Fold in all the spacing */
106                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
107                         if (inbuf[i]==10) inbuf[i]=32;
108                         if (inbuf[i]==13) inbuf[i]=32;
109                         if (inbuf[i]==9) inbuf[i]=32;
110                         /*** we like foreign characters now.
111                         if ((inbuf[i]<32) || (inbuf[i]>126)) {
112                                 inbuf[i] = '?';
113                         } */
114                     }
115                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
116                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
117                                 strcpy(&inbuf[i], &inbuf[i+1]);
118                     }
119
120                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
121
122                         ch = inbuf[i];
123
124                         if (ch == '<') {
125                                 ++nest;
126                                 strcpy(tag, "");
127                         }
128
129                         else if (ch == '>') {   /* We have a tag. */
130                                 if (nest > 0) --nest;
131
132                                 /* Unqualify the tag (truncate at first space) */
133                                 if (strchr(tag, ' ') != NULL) {
134                                         strcpy(strchr(tag, ' '), "");
135                                 }
136                                 
137                                 if (!strcasecmp(tag, "P")) {
138                                         strcat(outbuf, nl);
139                                         strcat(outbuf, nl);
140                                 }
141
142                                 if (!strcasecmp(tag, "/DIV")) {
143                                         strcat(outbuf, nl);
144                                         strcat(outbuf, nl);
145                                 }
146
147                                 if (!strcasecmp(tag, "LI")) {
148                                         strcat(outbuf, nl);
149                                         strcat(outbuf, " * ");
150                                 }
151
152                                 else if (!strcasecmp(tag, "/UL")) {
153                                         strcat(outbuf, nl);
154                                         strcat(outbuf, nl);
155                                 }
156
157                                 else if (!strcasecmp(tag, "H1")) {
158                                         strcat(outbuf, nl);
159                                         strcat(outbuf, nl);
160                                 }
161
162                                 else if (!strcasecmp(tag, "H2")) {
163                                         strcat(outbuf, nl);
164                                         strcat(outbuf, nl);
165                                 }
166
167                                 else if (!strcasecmp(tag, "H3")) {
168                                         strcat(outbuf, nl);
169                                         strcat(outbuf, nl);
170                                 }
171
172                                 else if (!strcasecmp(tag, "H4")) {
173                                         strcat(outbuf, nl);
174                                         strcat(outbuf, nl);
175                                 }
176
177                                 else if (!strcasecmp(tag, "/H1")) {
178                                         strcat(outbuf, nl);
179                                 }
180
181                                 else if (!strcasecmp(tag, "/H2")) {
182                                         strcat(outbuf, nl);
183                                 }
184
185                                 else if (!strcasecmp(tag, "/H3")) {
186                                         strcat(outbuf, nl);
187                                 }
188
189                                 else if (!strcasecmp(tag, "/H4")) {
190                                         strcat(outbuf, nl);
191                                 }
192
193                                 else if (!strcasecmp(tag, "HR")) {
194                                         strcat(outbuf, nl);
195                                         strcat(outbuf, " ");
196                                         for (j=0; j<screenwidth-2; ++j)
197                                                 strcat(outbuf, "-");
198                                         strcat(outbuf, nl);
199                                 }
200
201                                 else if (!strcasecmp(tag, "BR")) {
202                                         strcat(outbuf, nl);
203                                 }
204
205                                 else if (!strcasecmp(tag, "TR")) {
206                                         strcat(outbuf, nl);
207                                 }
208
209                                 else if (!strcasecmp(tag, "/TABLE")) {
210                                         strcat(outbuf, nl);
211                                 }
212
213                                 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
214                                         ++blockquote;
215                                         strcpy(nl, "\n");
216                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
217                                         strcat(outbuf, nl);
218                                 }
219
220                                 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
221                                         strcat(outbuf, "\n");
222                                         --blockquote;
223                                         strcpy(nl, "\n");
224                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
225                                         strcat(outbuf, nl);
226                                 }
227
228                                 else if (!strcasecmp(tag, "STYLE")) {
229                                         ++styletag;
230                                         if (styletag == 1) {
231                                                 styletag_start = strlen(outbuf);
232                                         }
233                                 }
234
235                                 else if (!strcasecmp(tag, "/STYLE")) {
236                                         --styletag;
237                                         if (styletag == 0) {
238                                                 outbuf[styletag_start] = 0;
239                                         }
240                                 }
241
242                         }
243
244                         else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
245                                 tag[strlen(tag)+1] = 0;
246                                 tag[strlen(tag)] = ch;
247                         }
248                                 
249                         else if (!nest) {
250                                 outbuf[strlen(outbuf)+1] = 0;
251                                 outbuf[strlen(outbuf)] = ch;
252                         }
253                     }
254                     strcpy(inbuf, &inbuf[i]);
255                 }
256
257                 /* Convert &; tags to the forbidden characters */
258                 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
259
260                         /* Character entity references */
261                         if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
262                                 outbuf[i] = ' ';
263                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
264                         }
265
266                         if (!strncasecmp(&outbuf[i], "&ensp;", 6)) {
267                                 outbuf[i] = ' ';
268                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
269                         }
270
271                         if (!strncasecmp(&outbuf[i], "&emsp;", 6)) {
272                                 outbuf[i] = ' ';
273                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
274                         }
275
276                         if (!strncasecmp(&outbuf[i], "&thinsp;", 8)) {
277                                 outbuf[i] = ' ';
278                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
279                         }
280
281                         else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
282                                 outbuf[i] = '<';
283                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
284                         }
285
286                         else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
287                                 outbuf[i] = '>';
288                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
289                         }
290
291                         else if (!strncasecmp(&outbuf[i], "&amp;", 5)) {
292                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
293                         }
294
295                         else if (!strncasecmp(&outbuf[i], "&quot;", 6)) {
296                                 outbuf[i] = '\"';
297                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
298                         }
299
300                         else if (!strncasecmp(&outbuf[i], "&lsquo;", 7)) {
301                                 outbuf[i] = '`';
302                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
303                         }
304
305                         else if (!strncasecmp(&outbuf[i], "&rsquo;", 7)) {
306                                 outbuf[i] = '\'';
307                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
308                         }
309
310                         else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
311                                 outbuf[i] = '(';
312                                 outbuf[i+1] = 'c';
313                                 outbuf[i+2] = ')';
314                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
315                         }
316
317                         else if (!strncasecmp(&outbuf[i], "&bull;", 6)) {
318                                 outbuf[i] = ' ';
319                                 outbuf[i+1] = '*';
320                                 outbuf[i+2] = ' ';
321                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
322                         }
323
324                         else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
325                                 outbuf[i] = '.';
326                                 outbuf[i+1] = '.';
327                                 outbuf[i+2] = '.';
328                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
329                         }
330
331                         else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
332                                 outbuf[i] = '(';
333                                 outbuf[i+1] = 't';
334                                 outbuf[i+2] = 'm';
335                                 outbuf[i+3] = ')';
336                                 strcpy(&outbuf[i+4], &outbuf[i+7]);
337                         }
338
339                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
340                                 outbuf[i] = '(';
341                                 outbuf[i+1] = 'r';
342                                 outbuf[i+2] = ')';
343                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
344                         }
345
346                         else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
347                                 outbuf[i] = '1';
348                                 outbuf[i+1] = '/';
349                                 outbuf[i+2] = '4';
350                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
351                         }
352
353                         else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
354                                 outbuf[i] = '1';
355                                 outbuf[i+1] = '/';
356                                 outbuf[i+2] = '2';
357                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
358                         }
359
360                         else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
361                                 outbuf[i] = '3';
362                                 outbuf[i+1] = '/';
363                                 outbuf[i+2] = '4';
364                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
365                         }
366
367                         else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
368                                 outbuf[i] = '-';
369                                 outbuf[i+1] = '-';
370                                 strcpy(&outbuf[i+2], &outbuf[i+7]);
371                         }
372
373                         else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
374                                 outbuf[i] = '-';
375                                 outbuf[i+1] = '-';
376                                 outbuf[i+2] = '-';
377                                 strcpy(&outbuf[i+3], &outbuf[i+7]);
378                         }
379
380                         else if (!strncmp(&outbuf[i], "&Ccedil;", 8)) {
381                                 outbuf[i] = 'C';
382                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
383                         }
384
385                         else if (!strncasecmp(&outbuf[i], "&ccedil;", 8)) {
386                                 outbuf[i] = 'c';
387                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
388                         }
389
390                         else if (!strncmp(&outbuf[i], "&Egrave;", 8)) {
391                                 outbuf[i] = 'E';
392                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
393                         }
394
395                         else if (!strncasecmp(&outbuf[i], "&egrave;", 8)) {
396                                 outbuf[i] = 'e';
397                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
398                         }
399
400                         else if (!strncmp(&outbuf[i], "&Ecirc;", 7)) {
401                                 outbuf[i] = 'E';
402                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
403                         }
404
405                         else if (!strncasecmp(&outbuf[i], "&ecirc;", 7)) {
406                                 outbuf[i] = 'e';
407                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
408                         }
409
410                         else if (!strncmp(&outbuf[i], "&Eacute;", 8)) {
411                                 outbuf[i] = 'E';
412                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
413                         }
414
415                         else if (!strncasecmp(&outbuf[i], "&eacute;", 8)) {
416                                 outbuf[i] = 'e';
417                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
418                         }
419
420                         else if (!strncmp(&outbuf[i], "&Agrave;", 8)) {
421                                 outbuf[i] = 'A';
422                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
423                         }
424
425                         else if (!strncasecmp(&outbuf[i], "&agrave;", 8)) {
426                                 outbuf[i] = 'a';
427                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
428                         }
429
430                         else if (!strncasecmp(&outbuf[i], "&ldquo;", 7)) {
431                                 outbuf[i] = '\"';
432                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
433                         }
434
435                         else if (!strncasecmp(&outbuf[i], "&rdquo;", 7)) {
436                                 outbuf[i] = '\"';
437                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
438                         }
439
440                         else if (!strncasecmp(&outbuf[i], "&acute;", 7)) {
441                                 outbuf[i] = '\'';
442                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
443                         }
444
445                         /* two-digit decimal equivalents */
446                         else if ((!strncmp(&outbuf[i], "&#", 2))
447                               && (outbuf[i+4] == ';') ) {
448                                 scanch = 0;
449                                 sscanf(&outbuf[i+2], "%02d", &scanch);
450                                 outbuf[i] = scanch;
451                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
452                         }
453
454                         /* three-digit decimal equivalents */
455                         else if ((!strncmp(&outbuf[i], "&#", 2))
456                               && (outbuf[i+5] == ';') ) {
457                                 scanch = 0;
458                                 sscanf(&outbuf[i+2], "%03d", &scanch);
459                                 outbuf[i] = scanch;
460                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
461                         }
462
463                 }
464
465                 /* Make sure the output buffer is big enough */
466                 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
467                         outptr_buffer_size += SIZ;
468                         outptr = realloc(outptr, outptr_buffer_size);
469                         if (outptr == NULL) {
470                                 abort();
471                         }
472                 }
473
474                 /* Output any lines terminated with hard line breaks */
475                 do {
476                         did_out = 0;
477                         if (strlen(outbuf) > 0) {
478                             for (i = 0; i<strlen(outbuf); ++i) {
479                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
480
481                                         strncpy(&outptr[output_len], outbuf, i+1);
482                                         output_len += (i+1);
483
484                                         if (do_citaformat) {
485                                                 strcpy(&outptr[output_len], " ");
486                                                 ++output_len;
487                                         }
488
489                                         strcpy(outbuf, &outbuf[i+1]);
490                                         i = 0;
491                                         did_out = 1;
492                                 }
493                         }
494                     }
495                 } while (did_out);
496
497                 /* Add soft line breaks */
498                 if (strlen(outbuf) > (screenwidth - 2 )) {
499                         rb = (-1);
500                         for (i=0; i<(screenwidth-2); ++i) {
501                                 if (outbuf[i]==32) rb = i;
502                         }
503                         if (rb>=0) {
504                                 strncpy(&outptr[output_len], outbuf, rb);
505                                 output_len += rb;
506                                 strcpy(&outptr[output_len], nl);
507                                 output_len += strlen(nl);
508                                 if (do_citaformat) {
509                                         strcpy(&outptr[output_len], " ");
510                                         ++output_len;
511                                 }
512                                 strcpy(outbuf, &outbuf[rb+1]);
513                         } else {
514                                 strncpy(&outptr[output_len], outbuf,
515                                         screenwidth-2);
516                                 output_len += (screenwidth-2);
517                                 strcpy(&outptr[output_len], nl);
518                                 output_len += strlen(nl);
519                                 if (do_citaformat) {
520                                         strcpy(&outptr[output_len], " ");
521                                         ++output_len;
522                                 }
523                                 strcpy(outbuf, &outbuf[screenwidth-2]);
524                         }
525                 }
526
527         } while (done_reading == 0);
528
529         strcpy(&outptr[output_len], outbuf);
530         output_len += strlen(outbuf);
531
532         /* Strip leading/trailing whitespace.  We can't do this with
533          * striplt() because it uses too many strlen()'s
534          */
535         while ((output_len > 0) && (isspace(outptr[0]))) {
536                 strcpy(outptr, &outptr[1]);
537                 --output_len;
538         }
539         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
540                 outptr[output_len-1] = 0;
541                 --output_len;
542         }
543
544         if (outptr[output_len-1] != '\n') {
545                 strcat(outptr, "\n");
546                 ++output_len;
547         }
548
549         return outptr;
550
551 }