1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright (C) 2001 Ximian Inc.
*
* Authors: Michael Zucchi <notzed@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "camel-mime-filter-html.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "xmlmemory.h"
#include "HTMLparser.h"
#include "HTMLtree.h"
#define d(x)
static void camel_mime_filter_html_class_init (CamelMimeFilterHTMLClass *klass);
static void camel_mime_filter_html_init (CamelObject *o);
static void camel_mime_filter_html_finalize (CamelObject *o);
static CamelMimeFilterClass *camel_mime_filter_html_parent;
struct _CamelMimeFilterHTMLPrivate {
htmlParserCtxtPtr ctxt;
};
/* ********************************************************************** */
/* HTML parser */
#define ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0]))
static struct {
char *element;
char *remap;
} map_start[] = {
{ "p", "\n\n" },
{ "br", "\n" },
{ "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
};
static struct {
char *element;
char *remap;
} map_end[] = {
{ "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
};
static void
characters(void *ctx, const xmlChar *ch, int len)
{
CamelMimeFilter *mf = ctx;
memcpy(mf->outptr, ch, len);
mf->outptr+= len;
}
#if 0
/* we probably dont want to index comments */
static void
comment(void *ctx, const xmlChar *value)
{
CamelMimeFilter *mf = ctx;
mf->outptr += sprintf(mf->outptr, " %s \n", value);
}
#endif
/* we map element starts to stuff sometimes, so we can properly break up
words and lines.
This is very dumb, and needs to be smarter: e.g.
<b>F</b>\nooBar should -> "FooBar"
*/
static void
startElement(void *ctx, const xmlChar *name, const xmlChar **atts)
{
int i;
CamelMimeFilter *mf = ctx;
/* we grab all "content" from "meta" tags, and dump it in the output,
it might be useful for searching with. This should probably be pickier */
if (!strcasecmp(name, "meta")) {
if (atts) {
for (i=0;atts[i];i+=2) {
if (!strcmp(atts[i], "content"))
mf->outptr += sprintf(mf->outptr, " %s \n", atts[i+1]);
}
}
return;
}
/* FIXME: use a hashtable */
for (i=0;i<ARRAY_LEN(map_start);i++) {
if (!strcasecmp(map_start[i].element, name)) {
characters(ctx, map_start[i].remap, strlen(map_start[i].remap));
break;
}
}
}
static void
endElement(void *ctx, const xmlChar *name)
{
int i;
/* FIXME: use a hashtable */
for (i=0;i<ARRAY_LEN(map_end);i++) {
if (!strcasecmp(map_end[i].element, name)) {
characters(ctx, map_end[i].remap, strlen(map_end[i].remap));
break;
}
}
}
/* dum de dum, well we can print out some crap for now */
static void
warning(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
fprintf(stdout, "SAX.warning: ");
vfprintf(stdout, msg, args);
va_end(args);
}
static void
error(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
fprintf(stdout, "SAX.error: ");
vfprintf(stdout, msg, args);
va_end(args);
}
static void
fatalError(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
fprintf(stdout, "SAX.fatalError: ");
vfprintf(stdout, msg, args);
va_end(args);
}
static xmlSAXHandler indexSAXHandler = {
NULL, /* internalSubset */
NULL, /*isStandalone,*/
NULL, /*hasInternalSubset,*/
NULL, /*hasExternalSubset,*/
NULL, /*resolveEntity,*/
NULL, /*getEntity,*/
NULL, /*entityDecl,*/
NULL, /*notationDecl,*/
NULL, /*attributeDecl,*/
NULL, /*elementDecl,*/
NULL, /*unparsedEntityDecl,*/
NULL, /*setDocumentLocator,*/
NULL, /*startDocument,*/
NULL, /*endDocument,*/
startElement,
endElement,
NULL, /*reference,*/
characters,
NULL, /*ignorableWhitespace,*/
NULL, /*processingInstruction,*/
NULL, /*comment,*/
warning,
error,
fatalError,
NULL, /*getParameterEntity,*/
};
/* ********************************************************************** */
CamelType
camel_mime_filter_html_get_type (void)
{
static CamelType type = CAMEL_INVALID_TYPE;
if (type == CAMEL_INVALID_TYPE) {
type = camel_type_register (camel_mime_filter_get_type (), "CamelMimeFilterHTML",
sizeof (CamelMimeFilterHTML),
sizeof (CamelMimeFilterHTMLClass),
(CamelObjectClassInitFunc) camel_mime_filter_html_class_init,
NULL,
(CamelObjectInitFunc) camel_mime_filter_html_init,
(CamelObjectFinalizeFunc) camel_mime_filter_html_finalize);
}
return type;
}
static void
camel_mime_filter_html_finalize(CamelObject *o)
{
CamelMimeFilterHTML *f = (CamelMimeFilterHTML *)o;
if (f->priv->ctxt)
htmlFreeParserCtxt(f->priv->ctxt);
}
static void
camel_mime_filter_html_init (CamelObject *o)
{
CamelMimeFilterHTML *f = (CamelMimeFilterHTML *)o;
f->priv = g_malloc0(sizeof(*f->priv));
}
static void
complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlenptr, size_t *outprespace)
{
CamelMimeFilterHTML *f = (CamelMimeFilterHTML *)mf;
camel_mime_filter_set_size(mf, len*2+256, FALSE);
mf->outptr = mf->outbuf;
d(printf("converting html end:\n%.*s\n", (int)len, in));
if (f->priv->ctxt == NULL) {
f->priv->ctxt = htmlCreatePushParserCtxt(&indexSAXHandler, f, in, len, "", 0);
len = 0;
}
htmlParseChunk(f->priv->ctxt, in, len, 1);
*out = mf->outbuf;
*outlenptr = mf->outptr - mf->outbuf;
*outprespace = mf->outbuf - mf->outreal;
d(printf("converted html end:\n%.*s\n", (int)*outlenptr, *out));
}
static void
filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlenptr, size_t *outprespace)
{
CamelMimeFilterHTML *f = (CamelMimeFilterHTML *)mf;
camel_mime_filter_set_size(mf, len*2+16, FALSE);
mf->outptr = mf->outbuf;
d(printf("converting html:\n%.*s\n", (int)len, in));
if (f->priv->ctxt == NULL)
f->priv->ctxt = htmlCreatePushParserCtxt(&indexSAXHandler, f, in, len, "", 0);
else
htmlParseChunk(f->priv->ctxt, in, len, 0);
*out = mf->outbuf;
*outlenptr = mf->outptr - mf->outbuf;
*outprespace = mf->outbuf - mf->outreal;
d(printf("converted html:\n%.*s\n", (int)*outlenptr, *out));
}
static void
reset(CamelMimeFilter *mf)
{
CamelMimeFilterHTML *f = (CamelMimeFilterHTML *)mf;
if (f->priv->ctxt != NULL) {
htmlFreeParserCtxt(f->priv->ctxt);
f->priv->ctxt = NULL;
}
}
static void
camel_mime_filter_html_class_init (CamelMimeFilterHTMLClass *klass)
{
CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
camel_mime_filter_html_parent = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
filter_class->reset = reset;
filter_class->filter = filter;
filter_class->complete = complete;
}
/**
* camel_mime_filter_html_new:
*
* Create a new CamelMimeFilterHTML object.
*
* Return value: A new CamelMimeFilterHTML widget.
**/
CamelMimeFilterHTML *
camel_mime_filter_html_new (void)
{
CamelMimeFilterHTML *new = CAMEL_MIME_FILTER_HTML ( camel_object_new (camel_mime_filter_html_get_type ()));
return new;
}
|