]> code.citadel.org Git - citadel.git/blob - citadel/html.c
html.c: rendering of BLOCKQUOTE sections is now
[citadel.git] / citadel / html.c
1 /*
2  * $Id$
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 "citadel.h"
32 #include "server.h"
33 #include "serv_extensions.h"
34 #include "control.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "tools.h"
40 #include "room_ops.h"
41 #include "html.h"
42  
43
44 /*
45  * Convert HTML to plain text.
46  *
47  * inputmsg      = pointer to raw HTML message
48  * screenwidth   = desired output screenwidth
49  * do_citaformat = set to 1 to indent newlines with spaces
50  */
51 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
52         char inbuf[SIZ];
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 bytes_processed = 0;
64         char nl[128];
65
66         strcpy(nl, "\n");
67         inptr = inputmsg;
68         strcpy(inbuf, "");
69         strcpy(outbuf, "");
70         if (msglen == 0) msglen = strlen(inputmsg);
71
72         outptr_buffer_size = strlen(inptr) + SIZ;
73         outptr = malloc(outptr_buffer_size);
74         if (outptr == NULL) return NULL;
75         strcpy(outptr, "");
76         output_len = 0;
77
78         do {
79                 /* Fill the input buffer */
80                 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
81
82                         ch = *inptr++;
83                         if (ch != 0) {
84                                 inbuf[strlen(inbuf)+1] = 0;
85                                 inbuf[strlen(inbuf)] = ch;
86                         } 
87                         else {
88                                 done_reading = 1;
89                         }
90
91                         ++bytes_processed;
92                         if (bytes_processed > msglen) {
93                                 done_reading = 1;
94                         }
95
96                 }
97
98                 /* Do some parsing */
99                 if (strlen(inbuf)>0) {
100
101                     /* Fold in all the spacing */
102                     for (i=0; i<strlen(inbuf); ++i) {
103                         if (inbuf[i]==10) inbuf[i]=32;
104                         if (inbuf[i]==13) inbuf[i]=32;
105                         if (inbuf[i]==9) inbuf[i]=32;
106                         /*** we like foreign characters now.
107                         if ((inbuf[i]<32) || (inbuf[i]>126)) {
108                                 inbuf[i] = '?';
109                         } */
110                     }
111                     for (i=0; i<strlen(inbuf); ++i) {
112                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
113                                 strcpy(&inbuf[i], &inbuf[i+1]);
114                     }
115
116                     for (i=0; i<strlen(inbuf); ++i) {
117
118                         ch = inbuf[i];
119
120                         if (ch == '<') {
121                                 ++nest;
122                                 strcpy(tag, "");
123                         }
124
125                         else if (ch == '>') {   /* We have a tag. */
126                                 if (nest > 0) --nest;
127
128                                 /* Unqualify the tag (truncate at first space) */
129                                 if (strchr(tag, ' ') != NULL) {
130                                         strcpy(strchr(tag, ' '), "");
131                                 }
132                                 
133                                 if (!strcasecmp(tag, "P")) {
134                                         strcat(outbuf, nl);
135                                         strcat(outbuf, nl);
136                                 }
137
138                                 if (!strcasecmp(tag, "/DIV")) {
139                                         strcat(outbuf, nl);
140                                         strcat(outbuf, nl);
141                                 }
142
143                                 if (!strcasecmp(tag, "LI")) {
144                                         strcat(outbuf, nl);
145                                         strcat(outbuf, " * ");
146                                 }
147
148                                 else if (!strcasecmp(tag, "/UL")) {
149                                         strcat(outbuf, nl);
150                                         strcat(outbuf, nl);
151                                 }
152
153                                 else if (!strcasecmp(tag, "H1")) {
154                                         strcat(outbuf, nl);
155                                         strcat(outbuf, nl);
156                                 }
157
158                                 else if (!strcasecmp(tag, "H2")) {
159                                         strcat(outbuf, nl);
160                                         strcat(outbuf, nl);
161                                 }
162
163                                 else if (!strcasecmp(tag, "H3")) {
164                                         strcat(outbuf, nl);
165                                         strcat(outbuf, nl);
166                                 }
167
168                                 else if (!strcasecmp(tag, "H4")) {
169                                         strcat(outbuf, nl);
170                                         strcat(outbuf, nl);
171                                 }
172
173                                 else if (!strcasecmp(tag, "/H1")) {
174                                         strcat(outbuf, nl);
175                                 }
176
177                                 else if (!strcasecmp(tag, "/H2")) {
178                                         strcat(outbuf, nl);
179                                 }
180
181                                 else if (!strcasecmp(tag, "/H3")) {
182                                         strcat(outbuf, nl);
183                                 }
184
185                                 else if (!strcasecmp(tag, "/H4")) {
186                                         strcat(outbuf, nl);
187                                 }
188
189                                 else if (!strcasecmp(tag, "HR")) {
190                                         strcat(outbuf, nl);
191                                         strcat(outbuf, " ");
192                                         for (j=0; j<screenwidth-2; ++j)
193                                                 strcat(outbuf, "-");
194                                         strcat(outbuf, nl);
195                                 }
196
197                                 else if (!strcasecmp(tag, "BR")) {
198                                         strcat(outbuf, nl);
199                                 }
200
201                                 else if (!strcasecmp(tag, "TR")) {
202                                         strcat(outbuf, nl);
203                                 }
204
205                                 else if (!strcasecmp(tag, "/TABLE")) {
206                                         strcat(outbuf, nl);
207                                 }
208
209                                 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
210                                         strcat(outbuf, nl);
211                                         ++blockquote;
212                                         strcpy(nl, "\n");
213                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
214                                         strcat(outbuf, nl);
215                                 }
216
217                                 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
218                                         strcat(outbuf, "\n");
219                                         --blockquote;
220                                         strcpy(nl, "\n");
221                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
222                                         strcat(outbuf, nl);
223                                         strcat(outbuf, nl);
224                                 }
225
226                         }
227
228                         else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
229                                 tag[strlen(tag)+1] = 0;
230                                 tag[strlen(tag)] = ch;
231                         }
232                                 
233                         else if (!nest) {
234                                 outbuf[strlen(outbuf)+1] = 0;
235                                 outbuf[strlen(outbuf)] = ch;
236                         }
237                     }
238                     strcpy(inbuf, &inbuf[i]);
239                 }
240
241                 /* Convert &; tags to the forbidden characters */
242                 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
243
244                         /* Character entity references */
245                         if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
246                                 outbuf[i] = ' ';
247                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
248                         }
249
250                         if (!strncasecmp(&outbuf[i], "&ensp;", 6)) {
251                                 outbuf[i] = ' ';
252                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
253                         }
254
255                         if (!strncasecmp(&outbuf[i], "&emsp;", 6)) {
256                                 outbuf[i] = ' ';
257                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
258                         }
259
260                         if (!strncasecmp(&outbuf[i], "&thinsp;", 8)) {
261                                 outbuf[i] = ' ';
262                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
263                         }
264
265                         else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
266                                 outbuf[i] = '<';
267                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
268                         }
269
270                         else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
271                                 outbuf[i] = '>';
272                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
273                         }
274
275                         else if (!strncasecmp(&outbuf[i], "&amp;", 5)) {
276                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
277                         }
278
279                         else if (!strncasecmp(&outbuf[i], "&quot;", 6)) {
280                                 outbuf[i] = '\"';
281                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
282                         }
283
284                         else if (!strncasecmp(&outbuf[i], "&lsquo;", 7)) {
285                                 outbuf[i] = '`';
286                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
287                         }
288
289                         else if (!strncasecmp(&outbuf[i], "&rsquo;", 7)) {
290                                 outbuf[i] = '\'';
291                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
292                         }
293
294                         else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
295                                 outbuf[i] = '(';
296                                 outbuf[i+1] = 'c';
297                                 outbuf[i+2] = ')';
298                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
299                         }
300
301                         else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
302                                 outbuf[i] = '.';
303                                 outbuf[i+1] = '.';
304                                 outbuf[i+2] = '.';
305                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
306                         }
307
308                         else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
309                                 outbuf[i] = '(';
310                                 outbuf[i+1] = 't';
311                                 outbuf[i+2] = 'm';
312                                 outbuf[i+3] = ')';
313                                 strcpy(&outbuf[i+4], &outbuf[i+7]);
314                         }
315
316                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
317                                 outbuf[i] = '(';
318                                 outbuf[i+1] = 'r';
319                                 outbuf[i+2] = ')';
320                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
321                         }
322
323                         else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
324                                 outbuf[i] = '1';
325                                 outbuf[i+1] = '/';
326                                 outbuf[i+2] = '4';
327                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
328                         }
329
330                         else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
331                                 outbuf[i] = '1';
332                                 outbuf[i+1] = '/';
333                                 outbuf[i+2] = '2';
334                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
335                         }
336
337                         else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
338                                 outbuf[i] = '3';
339                                 outbuf[i+1] = '/';
340                                 outbuf[i+2] = '4';
341                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
342                         }
343
344                         else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
345                                 outbuf[i] = '-';
346                                 outbuf[i+1] = '-';
347                                 strcpy(&outbuf[i+2], &outbuf[i+7]);
348                         }
349
350                         else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
351                                 outbuf[i] = '-';
352                                 outbuf[i+1] = '-';
353                                 outbuf[i+2] = '-';
354                                 strcpy(&outbuf[i+3], &outbuf[i+7]);
355                         }
356
357                         /* two-digit decimal equivalents */
358                         else if ((!strncmp(&outbuf[i], "&#", 2))
359                               && (outbuf[i+4] == ';') ) {
360                                 scanch = 0;
361                                 sscanf(&outbuf[i+2], "%02d", &scanch);
362                                 outbuf[i] = scanch;
363                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
364                         }
365
366                         /* three-digit decimal equivalents */
367                         else if ((!strncmp(&outbuf[i], "&#", 2))
368                               && (outbuf[i+5] == ';') ) {
369                                 scanch = 0;
370                                 sscanf(&outbuf[i+2], "%03d", &scanch);
371                                 outbuf[i] = scanch;
372                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
373                         }
374
375                 }
376
377                 /* Make sure the output buffer is big enough */
378                 if ((output_len + strlen(outbuf) + SIZ)
379                    > outptr_buffer_size) {
380                         outptr_buffer_size += SIZ;
381                         outptr = realloc(outptr, outptr_buffer_size);
382                 }
383
384                 /* Output any lines terminated with hard line breaks */
385                 do {
386                         did_out = 0;
387                         if (strlen(outbuf)>0) {
388                             for (i = 0; i<strlen(outbuf); ++i) {
389                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
390
391                                         strncpy(&outptr[output_len],
392                                                 outbuf, i+1);
393                                         output_len += (i+1);
394
395                                         if (do_citaformat) {
396                                                 strcpy(&outptr[output_len],
397                                                         " ");
398                                                 ++output_len;
399                                         }
400
401                                         strcpy(outbuf, &outbuf[i+1]);
402                                         i = 0;
403                                         did_out = 1;
404                                 }
405                         }
406                     }
407                 } while (did_out);
408
409                 /* Add soft line breaks */
410                 if (strlen(outbuf) > (screenwidth - 2 )) {
411                         rb = (-1);
412                         for (i=0; i<(screenwidth-2); ++i) {
413                                 if (outbuf[i]==32) rb = i;
414                         }
415                         if (rb>=0) {
416                                 strncpy(&outptr[output_len], outbuf, rb);
417                                 output_len += rb;
418                                 strcpy(&outptr[output_len], nl);
419                                 output_len += strlen(nl);
420                                 if (do_citaformat) {
421                                         strcpy(&outptr[output_len], " ");
422                                         ++output_len;
423                                 }
424                                 strcpy(outbuf, &outbuf[rb+1]);
425                         } else {
426                                 strncpy(&outptr[output_len], outbuf,
427                                         screenwidth-2);
428                                 output_len += (screenwidth-2);
429                                 strcpy(&outptr[output_len], nl);
430                                 output_len += strlen(nl);
431                                 if (do_citaformat) {
432                                         strcpy(&outptr[output_len], " ");
433                                         ++output_len;
434                                 }
435                                 strcpy(outbuf, &outbuf[screenwidth-2]);
436                         }
437                 }
438
439         } while (done_reading == 0);
440
441         strcpy(&outptr[output_len], outbuf);
442         output_len += strlen(outbuf);
443
444         /* Strip leading/trailing whitespace.  We can't do this with
445          * striplt() because it uses too many strlen()'s
446          */
447         while ((output_len > 0) && (isspace(outptr[0]))) {
448                 strcpy(outptr, &outptr[1]);
449                 --output_len;
450         }
451         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
452                 outptr[output_len-1] = 0;
453                 --output_len;
454         }
455
456         if (outptr[output_len-1] != '\n') {
457                 strcat(outptr, "\n");
458                 ++output_len;
459         }
460
461         return outptr;
462
463 }