1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include <sys/types.h>
31 #include "camel-html-parser.h"
32 #include "camel-mime-filter-html.h"
36 #define CAMEL_MIME_FILTER_HTML_GET_PRIVATE(obj) \
37 (G_TYPE_INSTANCE_GET_PRIVATE \
38 ((obj), CAMEL_TYPE_MIME_FILTER_HTML, CamelMimeFilterHTMLPrivate))
40 struct _CamelMimeFilterHTMLPrivate {
41 CamelHTMLParser *ctxt;
44 G_DEFINE_TYPE (CamelMimeFilterHTML, camel_mime_filter_html, CAMEL_TYPE_MIME_FILTER)
46 /* ********************************************************************** */
50 /* well we odnt use this stuff yet */
58 { "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
65 { "h1", "\n" }, { "h2", "\n" }, { "h3", "\n" }, { "h4", "\n" }, { "h5", "\n" }, { "h6", "\n" },
69 /* ********************************************************************** */
72 mime_filter_html_run (CamelMimeFilter *mime_filter,
81 CamelMimeFilterHTMLPrivate *priv;
82 camel_html_parser_t state;
85 priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
87 d(printf("converting html:\n%.*s\n", (gint)inlen, in));
89 /* We should generally shrink the data, but this'll do */
90 camel_mime_filter_set_size (mime_filter, inlen * 2 + 256, FALSE);
91 outp = mime_filter->outbuf;
93 camel_html_parser_set_data (priv->ctxt, in, inlen, last);
98 state = camel_html_parser_step (priv->ctxt, &data, &len);
101 case CAMEL_HTML_PARSER_DATA:
102 case CAMEL_HTML_PARSER_ENT:
103 memcpy (outp, data, len);
106 case CAMEL_HTML_PARSER_ELEMENT:
107 /* FIXME: do some whitespace processing here */
110 /* ignore everything else */
113 } while (state != CAMEL_HTML_PARSER_EOF && state != CAMEL_HTML_PARSER_EOD);
115 *out = mime_filter->outbuf;
116 *outlenptr = outp - mime_filter->outbuf;
117 *outprespace = mime_filter->outbuf - mime_filter->outreal;
119 d(printf("converted html end:\n%.*s\n", (gint)*outlenptr, *out));
123 mime_filter_html_dispose (GObject *object)
125 CamelMimeFilterHTMLPrivate *priv;
127 priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (object);
129 if (priv->ctxt != NULL) {
130 g_object_unref (priv->ctxt);
134 /* Chain up to parent's dispose() method. */
135 G_OBJECT_CLASS (camel_mime_filter_html_parent_class)->dispose (object);
139 mime_filter_html_filter (CamelMimeFilter *mime_filter,
147 mime_filter_html_run (
148 mime_filter, in, len, prespace,
149 out, outlenptr, outprespace, FALSE);
153 mime_filter_html_complete (CamelMimeFilter *mime_filter,
161 mime_filter_html_run (
162 mime_filter, in, len, prespace,
163 out, outlenptr, outprespace, TRUE);
167 mime_filter_html_reset (CamelMimeFilter *mime_filter)
169 CamelMimeFilterHTMLPrivate *priv;
171 priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
173 g_object_unref (priv->ctxt);
174 priv->ctxt = camel_html_parser_new ();
178 camel_mime_filter_html_class_init (CamelMimeFilterHTMLClass *class)
180 GObjectClass *object_class;
181 CamelMimeFilterClass *mime_filter_class;
183 g_type_class_add_private (class, sizeof (CamelMimeFilterHTMLPrivate));
185 object_class = G_OBJECT_CLASS (class);
186 object_class->dispose = mime_filter_html_dispose;
188 mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
189 mime_filter_class->filter = mime_filter_html_filter;
190 mime_filter_class->complete = mime_filter_html_complete;
191 mime_filter_class->reset = mime_filter_html_reset;
195 camel_mime_filter_html_init (CamelMimeFilterHTML *mime_filter)
197 mime_filter->priv = CAMEL_MIME_FILTER_HTML_GET_PRIVATE (mime_filter);
198 mime_filter->priv->ctxt = camel_html_parser_new ();
202 * camel_mime_filter_html_new:
204 * Create a new #CamelMimeFilterHTML object.
206 * Returns: a new #CamelMimeFilterHTML object
209 camel_mime_filter_html_new (void)
211 return g_object_new (CAMEL_TYPE_MIME_FILTER_HTML, NULL);