2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define SC_HTMLBUFSIZE 8192
30 #define HR_STR "------------------------------------------------"
32 typedef struct _SC_HTMLSymbol SC_HTMLSymbol
;
40 static SC_HTMLSymbol symbol_list
[] = {
47 {"™", "\342\204\242"},
49 {"¡", "\302\241"},
50 {"¢", "\302\242"},
51 {"£", "\302\243"},
52 {"¤", "\302\244"},
53 {"¥", "\302\245"},
54 {"¦", "\302\246"},
55 {"§", "\302\247"},
56 {"¨", "\302\250"},
57 {"©", "\302\251"},
58 {"ª", "\302\252"},
59 {"«", "\302\253"},
60 {"¬", "\302\254"},
61 {"­", "\302\255"},
62 {"®", "\302\256"},
63 {"¯", "\302\257"},
64 {"°", "\302\260"},
65 {"±", "\302\261"},
66 {"²", "\302\262"},
67 {"³", "\302\263"},
68 {"´", "\302\264"},
69 {"µ", "\302\265"},
70 {"¶", "\302\266"},
71 {"·", "\302\267"},
72 {"¸", "\302\270"},
73 {"¹", "\302\271"},
74 {"º", "\302\272"},
75 {"»", "\302\273"},
76 {"¼", "\302\274"},
77 {"½", "\302\275"},
78 {"¾", "\302\276"},
79 {"¿", "\302\277"},
80 {"À", "\303\200"},
81 {"Á", "\303\201"},
82 {"Â", "\303\202"},
83 {"Ã", "\303\203"},
84 {"Ä", "\303\204"},
85 {"Å", "\303\205"},
86 {"Æ", "\303\206"},
87 {"Ç", "\303\207"},
88 {"È", "\303\210"},
89 {"É", "\303\211"},
90 {"Ê", "\303\212"},
91 {"Ë", "\303\213"},
92 {"Ì", "\303\214"},
93 {"Í", "\303\215"},
94 {"Î", "\303\216"},
95 {"Ï", "\303\217"},
96 {"Ð", "\303\220"},
97 {"Ñ", "\303\221"},
98 {"Ò", "\303\222"},
99 {"Ó", "\303\223"},
100 {"Ô", "\303\224"},
101 {"Õ", "\303\225"},
102 {"Ö", "\303\226"},
103 {"×", "\303\227"},
104 {"Ø", "\303\230"},
105 {"Ù", "\303\231"},
106 {"Ú", "\303\232"},
107 {"Û", "\303\233"},
108 {"Ü", "\303\234"},
109 {"Ý", "\303\235"},
110 {"Þ", "\303\236"},
111 {"ß", "\303\237"},
112 {"à", "\303\240"},
113 {"á", "\303\241"},
114 {"â", "\303\242"},
115 {"ã", "\303\243"},
116 {"ä", "\303\244"},
117 {"å", "\303\245"},
118 {"æ", "\303\246"},
119 {"ç", "\303\247"},
120 {"è", "\303\250"},
121 {"é", "\303\251"},
122 {"ê", "\303\252"},
123 {"ë", "\303\253"},
124 {"ì", "\303\254"},
125 {"í", "\303\255"},
126 {"î", "\303\256"},
127 {"ï", "\303\257"},
128 {"ð", "\303\260"},
129 {"ñ", "\303\261"},
130 {"ò", "\303\262"},
131 {"ó", "\303\263"},
132 {"ô", "\303\264"},
133 {"õ", "\303\265"},
134 {"ö", "\303\266"},
135 {"÷", "\303\267"},
136 {"ø", "\303\270"},
137 {"ù", "\303\271"},
138 {"ú", "\303\272"},
139 {"û", "\303\273"},
140 {"ü", "\303\274"},
141 {"ý", "\303\275"},
142 {"þ", "\303\276"},
143 {"ÿ", "\303\277"},
144 {"Œ", "\305\222"},
145 {"œ", "\305\223"},
146 {"Š", "\305\240"},
147 {"š", "\305\241"},
148 {"Ÿ", "\305\270"},
149 {"ˆ", "\313\206"},
150 {"˜", "\313\234"},
151 {" ", "\342\200\202"},
152 {" ", "\342\200\203"},
153 {" ", "\342\200\211"},
154 {"–", "\342\200\223"},
155 {"—", "\342\200\224"},
156 {"‘", "\342\200\230"},
157 {"’", "\342\200\231"},
158 {"‚", "\342\200\232"},
159 {"“", "\342\200\234"},
160 {"”", "\342\200\235"},
161 {"„", "\342\200\236"},
162 {"†", "\342\200\240"},
163 {"‡", "\342\200\241"},
164 {"•", "\342\200\242"},
165 {"…", "\342\200\246"},
166 {"‰", "\342\200\260"},
167 {"‹", "\342\200\271"},
168 {"›", "\342\200\272"},
169 {"€", "\342\202\254"},
170 {"™", "\342\204\242"},
178 {"¡", "\302\241"},
179 {"¢", "\302\242"},
180 {"£", "\302\243"},
181 {"¤", "\302\244"},
182 {"¥", "\302\245"},
183 {"¦", "\302\246"},
184 {"§", "\302\247"},
185 {"¨", "\302\250"},
186 {"©", "\302\251"},
187 {"ª", "\302\252"},
188 {"«", "\302\253"},
189 {"¬", "\302\254"},
190 {"­", "\302\255"},
191 {"®", "\302\256"},
192 {"¯", "\302\257"},
193 {"°", "\302\260"},
194 {"±", "\302\261"},
195 {"²", "\302\262"},
196 {"³", "\302\263"},
197 {"´", "\302\264"},
198 {"µ", "\302\265"},
199 {"¶", "\302\266"},
200 {"·", "\302\267"},
201 {"¸", "\302\270"},
202 {"¹", "\302\271"},
203 {"º", "\302\272"},
204 {"»", "\302\273"},
205 {"¼", "\302\274"},
206 {"½", "\302\275"},
207 {"¾", "\302\276"},
208 {"¿", "\302\277"},
209 {"À", "\303\200"},
210 {"Á", "\303\201"},
211 {"Â", "\303\202"},
212 {"Ã", "\303\203"},
213 {"Ä", "\303\204"},
214 {"Å", "\303\205"},
215 {"Æ", "\303\206"},
216 {"Ç", "\303\207"},
217 {"È", "\303\210"},
218 {"É", "\303\211"},
219 {"Ê", "\303\212"},
220 {"Ë", "\303\213"},
221 {"Ì", "\303\214"},
222 {"Í", "\303\215"},
223 {"Î", "\303\216"},
224 {"Ï", "\303\217"},
225 {"Ð", "\303\220"},
226 {"Ñ", "\303\221"},
227 {"Ò", "\303\222"},
228 {"Ó", "\303\223"},
229 {"Ô", "\303\224"},
230 {"Õ", "\303\225"},
231 {"Ö", "\303\226"},
232 {"×", "\303\227"},
233 {"Ø", "\303\230"},
234 {"Ù", "\303\231"},
235 {"Ú", "\303\232"},
236 {"Û", "\303\233"},
237 {"Ü", "\303\234"},
238 {"Ý", "\303\235"},
239 {"Þ", "\303\236"},
240 {"ß", "\303\237"},
241 {"à", "\303\240"},
242 {"á", "\303\241"},
243 {"â", "\303\242"},
244 {"ã", "\303\243"},
245 {"ä", "\303\244"},
246 {"å", "\303\245"},
247 {"æ", "\303\246"},
248 {"ç", "\303\247"},
249 {"è", "\303\250"},
250 {"é", "\303\251"},
251 {"ê", "\303\252"},
252 {"ë", "\303\253"},
253 {"ì", "\303\254"},
254 {"í", "\303\255"},
255 {"î", "\303\256"},
256 {"ï", "\303\257"},
257 {"ð", "\303\260"},
258 {"ñ", "\303\261"},
259 {"ò", "\303\262"},
260 {"ó", "\303\263"},
261 {"ô", "\303\264"},
262 {"õ", "\303\265"},
263 {"ö", "\303\266"},
264 {"÷", "\303\267"},
265 {"ø", "\303\270"},
266 {"ù", "\303\271"},
267 {"ú", "\303\272"},
268 {"û", "\303\273"},
269 {"ü", "\303\274"},
270 {"ý", "\303\275"},
271 {"þ", "\303\276"},
272 {"ÿ", "\303\277"},
273 {"Œ", "\305\222"},
274 {"œ", "\305\223"},
275 {"Š", "\305\240"},
276 {"š", "\305\241"},
277 {"Ÿ", "\305\270"},
278 {"ˆ", "\313\206"},
279 {"˜", "\313\234"},
280 {" ", "\342\200\202"},
281 {" ", "\342\200\203"},
282 {" ", "\342\200\211"},
283 {"–", "\342\200\223"},
284 {"—", "\342\200\224"},
285 {"‘", "\342\200\230"},
286 {"’", "\342\200\231"},
287 {"‚", "\342\200\232"},
288 {"“", "\342\200\234"},
289 {"”", "\342\200\235"},
290 {"„", "\342\200\236"},
291 {"†", "\342\200\240"},
292 {"‡", "\342\200\241"},
293 {"•", "\342\200\242"},
294 {"…", "\342\200\246"},
295 {"‰", "\342\200\260"},
296 {"‹", "\342\200\271"},
297 {"›", "\342\200\272"},
298 {"€", "\342\202\254"},
299 {"™", "\342\204\242"}
302 typedef struct _SC_HTMLAltSymbol SC_HTMLAltSymbol
;
304 struct _SC_HTMLAltSymbol
310 static GHashTable
*default_symbol_table
;
312 static SC_HTMLState
sc_html_read_line (SC_HTMLParser
*parser
);
313 static void sc_html_append_char (SC_HTMLParser
*parser
,
315 static void sc_html_append_str (SC_HTMLParser
*parser
,
318 static SC_HTMLState
sc_html_parse_tag (SC_HTMLParser
*parser
);
319 static void sc_html_parse_special (SC_HTMLParser
*parser
);
320 static void sc_html_get_parenthesis (SC_HTMLParser
*parser
,
325 SC_HTMLParser
*sc_html_parser_new(FILE *fp
, CodeConverter
*conv
)
327 SC_HTMLParser
*parser
;
329 g_return_val_if_fail(fp
!= NULL
, NULL
);
330 g_return_val_if_fail(conv
!= NULL
, NULL
);
332 parser
= g_new0(SC_HTMLParser
, 1);
335 parser
->str
= g_string_new(NULL
);
336 parser
->buf
= g_string_new(NULL
);
337 parser
->bufp
= parser
->buf
->str
;
338 parser
->state
= SC_HTML_NORMAL
;
340 parser
->newline
= TRUE
;
341 parser
->empty_line
= TRUE
;
342 parser
->space
= FALSE
;
345 #define SYMBOL_TABLE_ADD(table, list) \
349 for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
350 g_hash_table_insert(table, list[i].key, list[i].val); \
352 #define SYMBOL_TABLE_REF_ADD(table, list) \
356 for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
357 g_hash_table_insert(table, &list[i].key, list[i].val); \
360 if (!default_symbol_table
) {
361 default_symbol_table
=
362 g_hash_table_new(g_str_hash
, g_str_equal
);
363 SYMBOL_TABLE_ADD(default_symbol_table
, symbol_list
);
366 #undef SYMBOL_TABLE_ADD
367 #undef SYMBOL_TABLE_REF_ADD
369 parser
->symbol_table
= default_symbol_table
;
374 void sc_html_parser_destroy(SC_HTMLParser
*parser
)
376 g_string_free(parser
->str
, TRUE
);
377 g_string_free(parser
->buf
, TRUE
);
378 g_free(parser
->href
);
382 gchar
*sc_html_parse(SC_HTMLParser
*parser
)
384 parser
->state
= SC_HTML_NORMAL
;
385 g_string_truncate(parser
->str
, 0);
387 if (*parser
->bufp
== '\0') {
388 g_string_truncate(parser
->buf
, 0);
389 parser
->bufp
= parser
->buf
->str
;
390 if (sc_html_read_line(parser
) == SC_HTML_EOF
)
394 while (*parser
->bufp
!= '\0') {
395 switch (*parser
->bufp
) {
398 st
= sc_html_parse_tag(parser
);
399 /* when we see an href, we need to flush the str
400 * buffer. Then collect all the chars until we
401 * see the end anchor tag
403 if (SC_HTML_HREF_BEG
== st
|| SC_HTML_HREF
== st
)
404 return parser
->str
->str
;
408 sc_html_parse_special(parser
);
414 if (parser
->bufp
[0] == '\r' && parser
->bufp
[1] == '\n')
418 if (!parser
->newline
)
419 parser
->space
= TRUE
;
426 sc_html_append_char(parser
, *parser
->bufp
++);
430 return parser
->str
->str
;
433 static SC_HTMLState
sc_html_read_line(SC_HTMLParser
*parser
)
435 gchar buf
[SC_HTMLBUFSIZE
];
436 gchar buf2
[SC_HTMLBUFSIZE
];
439 if (fgets(buf
, sizeof(buf
), parser
->fp
) == NULL
) {
440 parser
->state
= SC_HTML_EOF
;
444 if (conv_convert(parser
->conv
, buf2
, sizeof(buf2
), buf
) < 0) {
445 index
= parser
->bufp
- parser
->buf
->str
;
447 conv_utf8todisp(buf2
, sizeof(buf2
), buf
);
448 g_string_append(parser
->buf
, buf2
);
450 parser
->bufp
= parser
->buf
->str
+ index
;
452 return SC_HTML_CONV_FAILED
;
455 index
= parser
->bufp
- parser
->buf
->str
;
457 g_string_append(parser
->buf
, buf2
);
459 parser
->bufp
= parser
->buf
->str
+ index
;
461 return SC_HTML_NORMAL
;
464 static void sc_html_append_char(SC_HTMLParser
*parser
, gchar ch
)
466 GString
*str
= parser
->str
;
468 if (!parser
->pre
&& parser
->space
) {
469 g_string_append_c(str
, ' ');
470 parser
->space
= FALSE
;
473 g_string_append_c(str
, ch
);
475 parser
->empty_line
= FALSE
;
477 parser
->newline
= TRUE
;
478 if (str
->len
> 1 && str
->str
[str
->len
- 2] == '\n')
479 parser
->empty_line
= TRUE
;
481 parser
->newline
= FALSE
;
484 static void sc_html_append_str(SC_HTMLParser
*parser
, const gchar
*str
, gint len
)
486 GString
*string
= parser
->str
;
488 if (!parser
->pre
&& parser
->space
) {
489 g_string_append_c(string
, ' ');
490 parser
->space
= FALSE
;
493 if (len
== 0) return;
495 g_string_append(string
, str
);
498 Xstrndup_a(s
, str
, len
, return);
499 g_string_append(string
, s
);
502 parser
->empty_line
= FALSE
;
503 if (string
->len
> 0 && string
->str
[string
->len
- 1] == '\n') {
504 parser
->newline
= TRUE
;
505 if (string
->len
> 1 && string
->str
[string
->len
- 2] == '\n')
506 parser
->empty_line
= TRUE
;
508 parser
->newline
= FALSE
;
511 static SC_HTMLTag
*sc_html_get_tag(const gchar
*str
)
517 g_return_val_if_fail(str
!= NULL
, NULL
);
519 if (*str
== '\0' || *str
== '!') return NULL
;
521 Xstrdup_a(tmp
, str
, return NULL
);
523 tag
= g_new0(SC_HTMLTag
, 1);
525 for (tmpp
= tmp
; *tmpp
!= '\0' && !g_ascii_isspace(*tmpp
); tmpp
++)
530 tag
->name
= g_strdup(tmp
);
535 tag
->name
= g_strdup(tmp
);
538 while (*tmpp
!= '\0') {
545 while (g_ascii_isspace(*tmpp
)) tmpp
++;
548 while (*tmpp
!= '\0' && !g_ascii_isspace(*tmpp
) &&
551 if (*tmpp
!= '\0' && *tmpp
!= '=') {
553 while (g_ascii_isspace(*tmpp
)) tmpp
++;
558 while (g_ascii_isspace(*tmpp
)) tmpp
++;
560 if (*tmpp
== '"' || *tmpp
== '\'') {
565 if ((p
= strchr(attr_value
, quote
)) == NULL
) {
566 g_warning("sc_html_get_tag(): syntax error in tag: '%s'\n", str
);
571 while (g_ascii_isspace(*tmpp
)) tmpp
++;
575 while (*tmpp
!= '\0' && !g_ascii_isspace(*tmpp
)) tmpp
++;
582 g_strchomp(attr_name
);
583 g_strdown(attr_name
);
584 attr
= g_new(SC_HTMLAttr
, 1);
585 attr
->name
= g_strdup(attr_name
);
586 attr
->value
= g_strdup(attr_value
);
587 tag
->attr
= g_list_append(tag
->attr
, attr
);
593 static void sc_html_free_tag(SC_HTMLTag
*tag
)
598 while (tag
->attr
!= NULL
) {
599 SC_HTMLAttr
*attr
= (SC_HTMLAttr
*)tag
->attr
->data
;
603 tag
->attr
= g_list_remove(tag
->attr
, tag
->attr
->data
);
608 static SC_HTMLState
sc_html_parse_tag(SC_HTMLParser
*parser
)
610 gchar buf
[SC_HTMLBUFSIZE
];
613 sc_html_get_parenthesis(parser
, buf
, sizeof(buf
));
615 tag
= sc_html_get_tag(buf
);
617 parser
->state
= SC_HTML_UNKNOWN
;
618 if (!tag
) return SC_HTML_UNKNOWN
;
620 if (!strcmp(tag
->name
, "br")) {
621 parser
->space
= FALSE
;
622 sc_html_append_char(parser
, '\n');
623 parser
->state
= SC_HTML_BR
;
624 } else if (!strcmp(tag
->name
, "a")) {
626 for (cur
= tag
->attr
; cur
!= NULL
; cur
= cur
->next
) {
627 if (cur
->data
&& !strcmp(((SC_HTMLAttr
*)cur
->data
)->name
, "href")) {
628 g_free(parser
->href
);
629 parser
->href
= g_strdup(((SC_HTMLAttr
*)cur
->data
)->value
);
630 parser
->state
= SC_HTML_HREF_BEG
;
634 } else if (!strcmp(tag
->name
, "/a")) {
635 parser
->state
= SC_HTML_HREF
;
636 } else if (!strcmp(tag
->name
, "p")) {
637 parser
->space
= FALSE
;
638 if (!parser
->empty_line
) {
639 parser
->space
= FALSE
;
640 if (!parser
->newline
) sc_html_append_char(parser
, '\n');
641 sc_html_append_char(parser
, '\n');
643 parser
->state
= SC_HTML_PAR
;
644 } else if (!strcmp(tag
->name
, "pre")) {
646 parser
->state
= SC_HTML_PRE
;
647 } else if (!strcmp(tag
->name
, "/pre")) {
649 parser
->state
= SC_HTML_NORMAL
;
650 } else if (!strcmp(tag
->name
, "hr")) {
651 if (!parser
->newline
) {
652 parser
->space
= FALSE
;
653 sc_html_append_char(parser
, '\n');
655 sc_html_append_str(parser
, HR_STR
"\n", -1);
656 parser
->state
= SC_HTML_HR
;
657 } else if (!strcmp(tag
->name
, "div") ||
658 !strcmp(tag
->name
, "ul") ||
659 !strcmp(tag
->name
, "li") ||
660 !strcmp(tag
->name
, "table") ||
661 !strcmp(tag
->name
, "tr") ||
662 (tag
->name
[0] == 'h' && g_ascii_isdigit(tag
->name
[1]))) {
663 if (!parser
->newline
) {
664 parser
->space
= FALSE
;
665 sc_html_append_char(parser
, '\n');
667 parser
->state
= SC_HTML_NORMAL
;
668 } else if (!strcmp(tag
->name
, "/table") ||
669 (tag
->name
[0] == '/' &&
670 tag
->name
[1] == 'h' &&
671 g_ascii_isdigit(tag
->name
[1]))) {
672 if (!parser
->empty_line
) {
673 parser
->space
= FALSE
;
674 if (!parser
->newline
) sc_html_append_char(parser
, '\n');
675 sc_html_append_char(parser
, '\n');
677 parser
->state
= SC_HTML_NORMAL
;
678 } else if (!strcmp(tag
->name
, "/div") ||
679 !strcmp(tag
->name
, "/ul") ||
680 !strcmp(tag
->name
, "/li")) {
681 if (!parser
->newline
) {
682 parser
->space
= FALSE
;
683 sc_html_append_char(parser
, '\n');
685 parser
->state
= SC_HTML_NORMAL
;
688 sc_html_free_tag(tag
);
690 return parser
->state
;
693 static void sc_html_parse_special(SC_HTMLParser
*parser
)
695 gchar symbol_name
[9];
699 parser
->state
= SC_HTML_UNKNOWN
;
700 g_return_if_fail(*parser
->bufp
== '&');
703 for (n
= 0; parser
->bufp
[n
] != '\0' && parser
->bufp
[n
] != ';'; n
++)
705 if (n
> 7 || parser
->bufp
[n
] != ';') {
706 /* output literal `&' */
707 sc_html_append_char(parser
, *parser
->bufp
++);
708 parser
->state
= SC_HTML_NORMAL
;
711 strncpy2(symbol_name
, parser
->bufp
, n
+ 2);
712 parser
->bufp
+= n
+ 1;
714 if ((val
= g_hash_table_lookup(parser
->symbol_table
, symbol_name
))
716 sc_html_append_str(parser
, val
, -1);
717 parser
->state
= SC_HTML_NORMAL
;
721 sc_html_append_str(parser
, symbol_name
, -1);
724 static void sc_html_get_parenthesis(SC_HTMLParser
*parser
, gchar
*buf
, gint len
)
729 g_return_if_fail(*parser
->bufp
== '<');
731 /* ignore comment / CSS / script stuff */
732 if (!strncmp(parser
->bufp
, "<!--", 4)) {
734 while ((p
= strstr(parser
->bufp
, "-->")) == NULL
)
735 if (sc_html_read_line(parser
) == SC_HTML_EOF
) return;
736 parser
->bufp
= p
+ 3;
739 if (!g_ascii_strncasecmp(parser
->bufp
, "<style", 6)) {
741 while ((p
= strcasestr(parser
->bufp
, "</style>")) == NULL
)
742 if (sc_html_read_line(parser
) == SC_HTML_EOF
) return;
743 parser
->bufp
= p
+ 8;
746 if (!g_ascii_strncasecmp(parser
->bufp
, "<script", 7)) {
748 while ((p
= strcasestr(parser
->bufp
, "</script>")) == NULL
)
749 if (sc_html_read_line(parser
) == SC_HTML_EOF
) return;
750 parser
->bufp
= p
+ 9;
755 while ((p
= strchr(parser
->bufp
, '>')) == NULL
)
756 if (sc_html_read_line(parser
) == SC_HTML_EOF
) return;
758 strncpy2(buf
, parser
->bufp
, MIN(p
- parser
->bufp
+ 1, len
));
760 parser
->bufp
= p
+ 1;