]> code.citadel.org Git - citadel.git/blob - citadel/html.c
* html.c: Try to something sane with lists
[citadel.git] / citadel / html.c
1 /*
2  * $Id$
3  *
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.
7  */
8
9 #ifdef DLL_EXPORT
10 #define IN_LIBCIT
11 #endif
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <ctype.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include "citadel.h"
36 #include "server.h"
37 #include "serv_extensions.h"
38 #include "control.h"
39 #include "sysdep_decls.h"
40 #include "support.h"
41 #include "config.h"
42 #include "msgbase.h"
43 #include "tools.h"
44 #include "room_ops.h"
45 #include "html.h"
46  
47
48 /*
49  * Convert HTML to plain text.
50  *
51  * inputmsg      = pointer to raw HTML message
52  * screenwidth   = desired output screenwidth
53  * do_citaformat = set to 1 to indent newlines with spaces
54  */
55 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
56         char inbuf[SIZ];
57         char outbuf[SIZ];
58         char tag[1024];
59         int done_reading = 0;
60         char *inptr;
61         char *outptr;
62         size_t outptr_buffer_size;
63         size_t output_len = 0;
64         int i, j, ch, did_out, rb, scanch;
65         int nest = 0;           /* Bracket nesting level */
66         int blockquote = 0;     /* BLOCKQUOTE nesting level */
67
68         inptr = inputmsg;
69         strcpy(inbuf, "");
70         strcpy(outbuf, "");
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                 }
92
93                 /* Do some parsing */
94                 if (strlen(inbuf)>0) {
95
96                     /* Fold in all the spacing */
97                     for (i=0; i<strlen(inbuf); ++i) {
98                         if (inbuf[i]==10) inbuf[i]=32;
99                         if (inbuf[i]==13) inbuf[i]=32;
100                         if (inbuf[i]==9) inbuf[i]=32;
101                         if ((inbuf[i]<32) || (inbuf[i]>126)) {
102                                 inbuf[i] = '?';
103                         }
104                     }
105                     for (i=0; i<strlen(inbuf); ++i) {
106                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
107                                 strcpy(&inbuf[i], &inbuf[i+1]);
108                     }
109
110                     for (i=0; i<strlen(inbuf); ++i) {
111
112                         ch = inbuf[i];
113
114                         if (ch == '<') {
115                                 ++nest;
116                                 strcpy(tag, "");
117                         }
118
119                         else if (ch == '>') {   /* We have a tag. */
120                                 if (nest > 0) --nest;
121
122                                 /* Unqualify the tag (truncate at first space) */
123                                 if (strchr(tag, ' ') != NULL) {
124                                         strcpy(strchr(tag, ' '), "");
125                                 }
126                                 
127                                 if (!strcasecmp(tag, "P")) {
128                                         strcat(outbuf, "\n\n");
129                                 }
130
131                                 if (!strcasecmp(tag, "/DIV")) {
132                                         strcat(outbuf, "\n\n");
133                                 }
134
135                                 if (!strcasecmp(tag, "LI")) {
136                                         strcat(outbuf, "\n * ");
137                                 }
138
139                                 else if (!strcasecmp(tag, "/UL")) {
140                                         strcat(outbuf, "\n\n");
141                                 }
142
143                                 else if (!strcasecmp(tag, "H1")) {
144                                         strcat(outbuf, "\n\n");
145                                 }
146
147                                 else if (!strcasecmp(tag, "H2")) {
148                                         strcat(outbuf, "\n\n");
149                                 }
150
151                                 else if (!strcasecmp(tag, "H3")) {
152                                         strcat(outbuf, "\n\n");
153                                 }
154
155                                 else if (!strcasecmp(tag, "H4")) {
156                                         strcat(outbuf, "\n\n");
157                                 }
158
159                                 else if (!strcasecmp(tag, "/H1")) {
160                                         strcat(outbuf, "\n");
161                                 }
162
163                                 else if (!strcasecmp(tag, "/H2")) {
164                                         strcat(outbuf, "\n");
165                                 }
166
167                                 else if (!strcasecmp(tag, "/H3")) {
168                                         strcat(outbuf, "\n");
169                                 }
170
171                                 else if (!strcasecmp(tag, "/H4")) {
172                                         strcat(outbuf, "\n");
173                                 }
174
175                                 else if (!strcasecmp(tag, "HR")) {
176                                         strcat(outbuf, "\n ");
177                                         for (j=0; j<screenwidth-2; ++j)
178                                                 strcat(outbuf, "-");
179                                         strcat(outbuf, "\n");
180                                 }
181
182                                 else if (!strcasecmp(tag, "BR")) {
183                                         strcat(outbuf, "\n");
184                                 }
185
186                                 else if (!strcasecmp(tag, "TR")) {
187                                         strcat(outbuf, "\n");
188                                 }
189
190                                 else if (!strcasecmp(tag, "/TABLE")) {
191                                         strcat(outbuf, "\n");
192                                 }
193
194                                 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
195                                         strcat(outbuf, "\n\n <<\n");
196                                         ++blockquote;
197                                 }
198
199                                 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
200                                         strcat(outbuf, "\n >>\n\n");
201                                         --blockquote;
202                                 }
203
204                         }
205
206                         else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
207                                 tag[strlen(tag)+1] = 0;
208                                 tag[strlen(tag)] = ch;
209                         }
210                                 
211                         else if (!nest) {
212                                 outbuf[strlen(outbuf)+1] = 0;
213                                 outbuf[strlen(outbuf)] = ch;
214                         }
215                     }
216                     strcpy(inbuf, &inbuf[i]);
217                 }
218
219                 /* Convert &; tags to the forbidden characters */
220                 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
221
222                         if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
223                                 outbuf[i] = ' ';
224                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
225                         }
226
227                         else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
228                                 outbuf[i] = '<';
229                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
230                         }
231
232                         else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
233                                 outbuf[i] = '>';
234                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
235                         }
236
237                         else if (!strncasecmp(&outbuf[i], "&amp;", 5)) {
238                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
239                         }
240
241                         else if (!strncasecmp(&outbuf[i], "&quot;", 6)) {
242                                 outbuf[i] = '\"';
243                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
244                         }
245
246                         else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
247                                 outbuf[i] = '(';
248                                 outbuf[i+1] = 'c';
249                                 outbuf[i+2] = ')';
250                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
251                         }
252
253                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
254                                 outbuf[i] = '(';
255                                 outbuf[i+1] = 'r';
256                                 outbuf[i+2] = ')';
257                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
258                         }
259
260                         /* two-digit decimal equivalents */
261                         else if ((!strncmp(&outbuf[i], "&#", 2))
262                               && (outbuf[i+4] == ';') ) {
263                                 scanch = 0;
264                                 sscanf(&outbuf[i+2], "%02d", &scanch);
265                                 outbuf[i] = scanch;
266                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
267                         }
268
269                         /* three-digit decimal equivalents */
270                         else if ((!strncmp(&outbuf[i], "&#", 2))
271                               && (outbuf[i+5] == ';') ) {
272                                 scanch = 0;
273                                 sscanf(&outbuf[i+2], "%03d", &scanch);
274                                 outbuf[i] = scanch;
275                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
276                         }
277
278                 }
279
280                 /* Make sure the output buffer is big enough */
281                 if ((output_len + strlen(outbuf) + SIZ)
282                    > outptr_buffer_size) {
283                         outptr_buffer_size += SIZ;
284                         outptr = realloc(outptr, outptr_buffer_size);
285                 }
286
287                 /* Output any lines terminated with hard line breaks */
288                 do {
289                         did_out = 0;
290                         if (strlen(outbuf)>0) {
291                             for (i = 0; i<strlen(outbuf); ++i) {
292                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
293
294                                         strncpy(&outptr[output_len],
295                                                 outbuf, i+1);
296                                         output_len += (i+1);
297
298                                         if (do_citaformat) {
299                                                 strcpy(&outptr[output_len],
300                                                         " ");
301                                                 ++output_len;
302                                         }
303
304                                         strcpy(outbuf, &outbuf[i+1]);
305                                         i = 0;
306                                         did_out = 1;
307                                 }
308                         }
309                     }
310                 } while (did_out);
311
312                 /* Add soft line breaks */
313                 if (strlen(outbuf) > (screenwidth - 2 )) {
314                         rb = (-1);
315                         for (i=0; i<(screenwidth-2); ++i) {
316                                 if (outbuf[i]==32) rb = i;
317                         }
318                         if (rb>=0) {
319                                 strncpy(&outptr[output_len], outbuf, rb);
320                                 output_len += rb;
321                                 strcpy(&outptr[output_len], "\n");
322                                 output_len += 1;
323                                 if (do_citaformat) {
324                                         strcpy(&outptr[output_len], " ");
325                                         ++output_len;
326                                 }
327                                 strcpy(outbuf, &outbuf[rb+1]);
328                         } else {
329                                 strncpy(&outptr[output_len], outbuf,
330                                         screenwidth-2);
331                                 output_len += (screenwidth-2);
332                                 strcpy(&outptr[output_len], "\n");
333                                 output_len += 1;
334                                 if (do_citaformat) {
335                                         strcpy(&outptr[output_len], " ");
336                                         ++output_len;
337                                 }
338                                 strcpy(outbuf, &outbuf[screenwidth-2]);
339                         }
340                 }
341
342         } while (done_reading == 0);
343
344         strcpy(&outptr[output_len], outbuf);
345         output_len += strlen(outbuf);
346
347         /* Strip leading/trailing whitespace.  We can't do this with
348          * striplt() because it uses too many strlen()'s
349          */
350         while ((output_len > 0) && (isspace(outptr[0]))) {
351                 strcpy(outptr, &outptr[1]);
352                 --output_len;
353         }
354         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
355                 outptr[output_len-1] = 0;
356                 --output_len;
357         }
358
359         if (outptr[output_len-1] != '\n') {
360                 strcat(outptr, "\n");
361                 ++output_len;
362         }
363
364         return outptr;
365
366 }