sync with 0.8.11cvs5
[claws.git] / src / html.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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 2 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "html.h"
26 #include "codeconv.h"
27 #include "utils.h"
28
29 #define HTMLBUFSIZE     8192
30 #define HR_STR          "------------------------------------------------"
31
32 typedef struct _HTMLSymbol      HTMLSymbol;
33
34 struct _HTMLSymbol
35 {
36         gchar *const key;
37         gchar *const val;
38 };
39
40 static HTMLSymbol symbol_list[] = {
41         {"&lt;"    , "<"},
42         {"&gt;"    , ">"},
43         {"&amp;"   , "&"},
44         {"&quot;"  , "\""},
45         {"&nbsp;"  , " "},
46         {"&trade;" , "(TM)"},
47
48         {"&#153;", "(TM)"},
49 };
50
51 static HTMLSymbol ascii_symbol_list[] = {
52         {"&iexcl;" , "^!"},
53         {"&brvbar;", "|"},
54         {"&copy;"  , "(C)"},
55         {"&laquo;" , "<<"},
56         {"&reg;"   , "(R)"},
57
58         {"&sup2;"  , "^2"},
59         {"&sup3;"  , "^3"},
60         {"&acute;" , "'"},
61         {"&cedil;" , ","},
62         {"&sup1;"  , "^1"},
63         {"&raquo;" , ">>"},
64         {"&frac14;", "1/4"},
65         {"&frac12;", "1/2"},
66         {"&frac34;", "3/4"},
67         {"&iquest;", "^?"},
68
69         {"&Agrave;", "A`"},
70         {"&Aacute;", "A'"},
71         {"&Acirc;" , "A^"},
72         {"&Atilde;", "A~"},
73         {"&AElig;" , "AE"},
74         {"&Egrave;", "E`"},
75         {"&Eacute;", "E'"},
76         {"&Ecirc;" , "E^"},
77         {"&Igrave;", "I`"},
78         {"&Iacute;", "I'"},
79         {"&Icirc;" , "I^"},
80
81         {"&Ntilde;", "N~"},
82         {"&Ograve;", "O`"},
83         {"&Oacute;", "O'"},
84         {"&Ocirc;" , "O^"},
85         {"&Otilde;", "O~"},
86         {"&Ugrave;", "U`"},
87         {"&Uacute;", "U'"},
88         {"&Ucirc;" , "U^"},
89         {"&Yacute;", "Y'"},
90
91         {"&agrave;", "a`"},
92         {"&aacute;", "a'"},
93         {"&acirc;" , "a^"},
94         {"&atilde;", "a~"},
95         {"&aelig;" , "ae"},
96         {"&egrave;", "e`"},
97         {"&eacute;", "e'"},
98         {"&ecirc;" , "e^"},
99         {"&igrave;", "i`"},
100         {"&iacute;", "i'"},
101         {"&icirc;" , "i^"},
102
103         {"&ntilde;", "n~"},
104         {"&ograve;", "o`"},
105         {"&oacute;", "o'"},
106         {"&ocirc;" , "o^"},
107         {"&otilde;", "o~"},
108         {"&ugrave;", "u`"},
109         {"&uacute;", "u'"},
110         {"&ucirc;" , "u^"},
111         {"&yacute;", "y'"},
112 };
113
114 static HTMLSymbol eucjp_symbol_list[] = {
115         {"&iexcl;" , "^!"},
116         {"&cent;"  , "\xa1\xf1"},
117         {"&pound;" , "\xa1\xf2"},
118         {"&yen;"   , "\xa1\xef"},
119         {"&brvbar;", "|"},
120         {"&sect;"  , "\xa1\xf8"},
121         {"&uml;"   , "\xa1\xaf"},
122         {"&copy;"  , "(C)"},
123         {"&laquo;" , "<<"},
124         {"&reg;"   , "(R)"},
125
126         {"&deg;"   , "\xa1\xeb"},
127         {"&plusmn;", "\xa1\xde"},
128         {"&sup2;"  , "^2"},
129         {"&sup3;"  , "^3"},
130         {"&acute;" , "'"},
131         {"&micro;" , "\xa6\xcc"},
132         {"&para;"  , "\xa2\xf9"},
133         {"&middot;", "\xa1\xa6"},
134         {"&cedil;" , ","},
135         {"&sup1;"  , "^1"},
136         {"&raquo;" , ">>"},
137         {"&frac14;", "1/4"},
138         {"&frac12;", "1/2"},
139         {"&frac34;", "3/4"},
140         {"&iquest;", "^?"},
141
142         {"&Agrave;", "A`"},
143         {"&Aacute;", "A'"},
144         {"&Acirc;" , "A^"},
145         {"&Atilde;", "A~"},
146         {"&Auml;"  , "A\xa1\xaf"},
147         {"&Aring;" , "A\xa1\xeb"},
148         {"&AElig;" , "AE"},
149         {"&Egrave;", "E`"},
150         {"&Eacute;", "E'"},
151         {"&Ecirc;" , "E^"},
152         {"&Euml;"  , "E\xa1\xaf"},
153         {"&Igrave;", "I`"},
154         {"&Iacute;", "I'"},
155         {"&Icirc;" , "I^"},
156         {"&Iuml;"  , "I\xa1\xaf"},
157
158         {"&Ntilde;", "N~"},
159         {"&Ograve;", "O`"},
160         {"&Oacute;", "O'"},
161         {"&Ocirc;" , "O^"},
162         {"&Otilde;", "O~"},
163         {"&Ouml;"  , "O\xa1\xaf"},
164         {"&times;" , "\xa1\xdf"},
165         {"&Ugrave;", "U`"},
166         {"&Uacute;", "U'"},
167         {"&Ucirc;" , "U^"},
168         {"&Uuml;"  , "U\xa1\xaf"},
169         {"&Yacute;", "Y'"},
170
171         {"&agrave;", "a`"},
172         {"&aacute;", "a'"},
173         {"&acirc;" , "a^"},
174         {"&atilde;", "a~"},
175         {"&auml;"  , "a\xa1\xaf"},
176         {"&aring;" , "a\xa1\xeb"},
177         {"&aelig;" , "ae"},
178         {"&egrave;", "e`"},
179         {"&eacute;", "e'"},
180         {"&ecirc;" , "e^"},
181         {"&euml;"  , "e\xa1\xaf"},
182         {"&igrave;", "i`"},
183         {"&iacute;", "i'"},
184         {"&icirc;" , "i^"},
185         {"&iuml;"  , "i\xa1\xaf"},
186
187         {"&eth;"   , "\xa2\xdf"},
188         {"&ntilde;", "n~"},
189         {"&ograve;", "o`"},
190         {"&oacute;", "o'"},
191         {"&ocirc;" , "o^"},
192         {"&otilde;", "o~"},
193         {"&ouml;"  , "o\xa1\xaf"},
194         {"&divide;", "\xa1\xe0"},
195         {"&ugrave;", "u`"},
196         {"&uacute;", "u'"},
197         {"&ucirc;" , "u^"},
198         {"&uuml;"  , "u\xa1\xaf"},
199         {"&yacute;", "y'"},
200         {"&yuml;"  , "y\xa1\xaf"},
201 };
202
203 static HTMLSymbol latin_symbol_list[] = {
204         {"&iexcl;" , "\xa1"},
205         {"&cent;"  , "\xa2"},
206         {"&pound;" , "\xa3"},
207         {"&curren;", "\xa4"},
208         {"&yen;"   , "\xa5"},
209         {"&brvbar;", "\xa6"},
210         {"&sect;"  , "\xa7"},
211         {"&uml;"   , "\xa8"},
212         {"&copy;"  , "\xa9"},
213         {"&ordf;"  , "\xaa"},
214         {"&laquo;" , "\xab"},
215         {"&not;"   , "\xac"},
216         {"&shy;"   , "\xad"},
217         {"&reg;"   , "\xae"},
218         {"&macr;"  , "\xaf"},
219
220         {"&deg;"   , "\xb0"},
221         {"&plusmn;", "\xb1"},
222         {"&sup2;"  , "\xb2"},
223         {"&sup3;"  , "\xb3"},
224         {"&acute;" , "\xb4"},
225         {"&micro;" , "\xb5"},
226         {"&para;"  , "\xb6"},
227         {"&middot;", "\xb7"},
228         {"&cedil;" , "\xb8"},
229         {"&sup1;"  , "\xb9"},
230         {"&ordm;"  , "\xba"},
231         {"&raquo;" , "\xbb"},
232         {"&frac14;", "\xbc"},
233         {"&frac12;", "\xbd"},
234         {"&frac34;", "\xbe"},
235         {"&iquest;", "\xbf"},
236
237         {"&Agrave;", "\xc0"},
238         {"&Aacute;", "\xc1"},
239         {"&Acirc;" , "\xc2"},
240         {"&Atilde;", "\xc3"},
241         {"&Auml;"  , "\xc4"},
242         {"&Aring;" , "\xc5"},
243         {"&AElig;" , "\xc6"},
244         {"&Ccedil;", "\xc7"},
245         {"&Egrave;", "\xc8"},
246         {"&Eacute;", "\xc9"},
247         {"&Ecirc;" , "\xca"},
248         {"&Euml;"  , "\xcb"},
249         {"&Igrave;", "\xcc"},
250         {"&Iacute;", "\xcd"},
251         {"&Icirc;" , "\xce"},
252         {"&Iuml;"  , "\xcf"},
253
254         {"&ETH;"   , "\xd0"},
255         {"&Ntilde;", "\xd1"},
256         {"&Ograve;", "\xd2"},
257         {"&Oacute;", "\xd3"},
258         {"&Ocirc;" , "\xd4"},
259         {"&Otilde;", "\xd5"},
260         {"&Ouml;"  , "\xd6"},
261         {"&times;" , "\xd7"},
262         {"&Oslash;", "\xd8"},
263         {"&Ugrave;", "\xd9"},
264         {"&Uacute;", "\xda"},
265         {"&Ucirc;" , "\xdb"},
266         {"&Uuml;"  , "\xdc"},
267         {"&Yacute;", "\xdd"},
268         {"&THORN;" , "\xde"},
269         {"&szlig;" , "\xdf"},
270
271         {"&agrave;", "\xe0"},
272         {"&aacute;", "\xe1"},
273         {"&acirc;" , "\xe2"},
274         {"&atilde;", "\xe3"},
275         {"&auml;"  , "\xe4"},
276         {"&aring;" , "\xe5"},
277         {"&aelig;" , "\xe6"},
278         {"&ccedil;", "\xe7"},
279         {"&egrave;", "\xe8"},
280         {"&eacute;", "\xe9"},
281         {"&ecirc;" , "\xea"},
282         {"&euml;"  , "\xeb"},
283         {"&igrave;", "\xec"},
284         {"&iacute;", "\xed"},
285         {"&icirc;" , "\xee"},
286         {"&iuml;"  , "\xef"},
287
288         {"&eth;"   , "\xf0"},
289         {"&ntilde;", "\xf1"},
290         {"&ograve;", "\xf2"},
291         {"&oacute;", "\xf3"},
292         {"&ocirc;" , "\xf4"},
293         {"&otilde;", "\xf5"},
294         {"&ouml;"  , "\xf6"},
295         {"&divide;", "\xf7"},
296         {"&oslash;", "\xf8"},
297         {"&ugrave;", "\xf9"},
298         {"&uacute;", "\xfa"},
299         {"&ucirc;" , "\xfb"},
300         {"&uuml;"  , "\xfc"},
301         {"&yacute;", "\xfd"},
302         {"&thorn;" , "\xfe"},
303         {"&yuml;"  , "\xff"},
304 };
305
306 static GHashTable *default_symbol_table;
307 static GHashTable *eucjp_symbol_table;
308 static GHashTable *latin_symbol_table;
309
310 static HTMLState html_read_line         (HTMLParser     *parser);
311 static void html_append_char            (HTMLParser     *parser,
312                                          gchar           ch);
313 static void html_append_str             (HTMLParser     *parser,
314                                          const gchar    *str,
315                                          gint            len);
316 static HTMLState html_parse_tag         (HTMLParser     *parser);
317 static void html_parse_special          (HTMLParser     *parser);
318 static void html_get_parenthesis        (HTMLParser     *parser,
319                                          gchar          *buf,
320                                          gint            len);
321
322 #if 0
323 static gint g_str_case_equal            (gconstpointer   v,
324                                          gconstpointer   v2);
325 static guint g_str_case_hash            (gconstpointer   key);
326 #endif
327
328 HTMLParser *html_parser_new(FILE *fp, CodeConverter *conv)
329 {
330         HTMLParser *parser;
331
332         g_return_val_if_fail(fp != NULL, NULL);
333         g_return_val_if_fail(conv != NULL, NULL);
334
335         parser = g_new0(HTMLParser, 1);
336         parser->fp = fp;
337         parser->conv = conv;
338         parser->str = g_string_new(NULL);
339         parser->buf = g_string_new(NULL);
340         parser->bufp = parser->buf->str;
341         parser->state = HTML_NORMAL;
342         parser->href = NULL;
343         parser->newline = TRUE;
344         parser->empty_line = TRUE;
345         parser->space = FALSE;
346         parser->pre = FALSE;
347
348 #define SYMBOL_TABLE_ADD(table, list) \
349 { \
350         gint i; \
351  \
352         for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
353                 g_hash_table_insert(table, list[i].key, list[i].val); \
354 }
355
356         if (!default_symbol_table) {
357                 default_symbol_table =
358                         g_hash_table_new(g_str_hash, g_str_equal);
359                 SYMBOL_TABLE_ADD(default_symbol_table, symbol_list);
360                 SYMBOL_TABLE_ADD(default_symbol_table, ascii_symbol_list);
361         }
362         if (!eucjp_symbol_table) {
363                 eucjp_symbol_table =
364                         g_hash_table_new(g_str_hash, g_str_equal);
365                 SYMBOL_TABLE_ADD(eucjp_symbol_table, symbol_list);
366                 SYMBOL_TABLE_ADD(eucjp_symbol_table, eucjp_symbol_list);
367         }
368         if (!latin_symbol_table) {
369                 latin_symbol_table =
370                         g_hash_table_new(g_str_hash, g_str_equal);
371                 SYMBOL_TABLE_ADD(latin_symbol_table, symbol_list);
372                 SYMBOL_TABLE_ADD(latin_symbol_table, latin_symbol_list);
373         }
374
375 #undef SYMBOL_TABLE_ADD
376
377         if (conv->charset == C_ISO_8859_1)
378                 parser->symbol_table = latin_symbol_table;
379         else if ((conv->charset == C_ISO_2022_JP   ||
380                   conv->charset == C_ISO_2022_JP_2 ||
381                   conv->charset == C_EUC_JP        ||
382                   conv->charset == C_SHIFT_JIS) &&
383                  conv_get_current_charset() == C_EUC_JP)
384                 parser->symbol_table = eucjp_symbol_table;
385         else
386                 parser->symbol_table = default_symbol_table;
387
388         return parser;
389 }
390
391 void html_parser_destroy(HTMLParser *parser)
392 {
393         g_string_free(parser->str, TRUE);
394         g_string_free(parser->buf, TRUE);
395         g_free(parser->href);
396         g_free(parser);
397 }
398
399 gchar *html_parse(HTMLParser *parser)
400 {
401         parser->state = HTML_NORMAL;
402         g_string_truncate(parser->str, 0);
403
404         if (*parser->bufp == '\0') {
405                 g_string_truncate(parser->buf, 0);
406                 parser->bufp = parser->buf->str;
407                 if (html_read_line(parser) == HTML_EOF)
408                         return NULL;
409         }
410
411         while (*parser->bufp != '\0') {
412                 switch (*parser->bufp) {
413                 case '<':
414                         if (parser->str->len == 0)
415                                 html_parse_tag(parser);
416                         else
417                                 return parser->str->str;
418                         break;
419                 case '&':
420                         html_parse_special(parser);
421                         break;
422                 case ' ':
423                 case '\t':
424                 case '\r':
425                 case '\n':
426                         if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
427                                 parser->bufp++;
428
429                         if (!parser->pre) {
430                                 if (!parser->newline)
431                                         parser->space = TRUE;
432
433                                 parser->bufp++;
434                                 break;
435                         }
436                         /* fallthrough */
437                 default:
438                         html_append_char(parser, *parser->bufp++);
439                 }
440         }
441
442         return parser->str->str;
443 }
444
445 static HTMLState html_read_line(HTMLParser *parser)
446 {
447         gchar buf[HTMLBUFSIZE];
448         gchar buf2[HTMLBUFSIZE];
449         gint index;
450
451         if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
452                 parser->state = HTML_EOF;
453                 return HTML_EOF;
454         }
455
456         if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
457                 index = parser->bufp - parser->buf->str;
458
459                 conv_localetodisp(buf2, sizeof(buf2), buf);
460                 g_string_append(parser->buf, buf2);
461
462                 parser->bufp = parser->buf->str + index;
463
464                 return HTML_CONV_FAILED;
465         }
466
467         index = parser->bufp - parser->buf->str;
468
469         g_string_append(parser->buf, buf2);
470
471         parser->bufp = parser->buf->str + index;
472
473         return HTML_NORMAL;
474 }
475
476 static void html_append_char(HTMLParser *parser, gchar ch)
477 {
478         GString *str = parser->str;
479
480         if (!parser->pre && parser->space) {
481                 g_string_append_c(str, ' ');
482                 parser->space = FALSE;
483         }
484
485         g_string_append_c(str, ch);
486
487         parser->empty_line = FALSE;
488         if (ch == '\n') {
489                 parser->newline = TRUE;
490                 if (str->len > 1 && str->str[str->len - 2] == '\n')
491                         parser->empty_line = TRUE;
492         } else
493                 parser->newline = FALSE;
494 }
495
496 static void html_append_str(HTMLParser *parser, const gchar *str, gint len)
497 {
498         GString *string = parser->str;
499
500         if (!parser->pre && parser->space) {
501                 g_string_append_c(string, ' ');
502                 parser->space = FALSE;
503         }
504
505         if (len == 0) return;
506         if (len < 0)
507                 g_string_append(string, str);
508         else {
509                 gchar *s;
510                 Xstrndup_a(s, str, len, return);
511                 g_string_append(string, s);
512         }
513
514         parser->empty_line = FALSE;
515         if (string->len > 0 && string->str[string->len - 1] == '\n') {
516                 parser->newline = TRUE;
517                 if (string->len > 1 && string->str[string->len - 2] == '\n')
518                         parser->empty_line = TRUE;
519         } else
520                 parser->newline = FALSE;
521 }
522
523 static HTMLTag *html_get_tag(const gchar *str)
524 {
525         HTMLTag *tag;
526         gchar *tmp;
527         gchar *tmpp;
528
529         g_return_val_if_fail(str != NULL, NULL);
530
531         if (*str == '\0' || *str == '!') return NULL;
532
533         Xstrdup_a(tmp, str, return NULL);
534
535         tag = g_new0(HTMLTag, 1);
536
537         for (tmpp = tmp; *tmpp != '\0' && !isspace(*tmpp); tmpp++)
538                 ;
539
540         if (*tmpp == '\0') {
541                 g_strdown(tmp);
542                 tag->name = g_strdup(tmp);
543                 return tag;
544         } else {
545                 *tmpp++ = '\0';
546                 g_strdown(tmp);
547                 tag->name = g_strdup(tmp);
548         }
549
550         while (*tmpp != '\0') {
551                 HTMLAttr *attr;
552                 gchar *attr_name;
553                 gchar *attr_value;
554                 gchar *p;
555                 gchar quote;
556
557                 while (isspace(*tmpp)) tmpp++;
558                 attr_name = tmpp;
559                 if ((p = strchr(attr_name, '=')) == NULL) {
560                         g_warning("html_get_tag(): syntax error in tag: '%s'\n", str);
561                         return tag;
562                 }
563                 tmpp = p;
564                 *tmpp++ = '\0';
565                 while (isspace(*tmpp)) tmpp++;
566
567                 if (*tmpp == '\0') {
568                         g_warning("html_get_tag(): syntax error in tag: '%s'\n", str);
569                         return tag;
570                 } else if (*tmpp == '"' || *tmpp == '\'') {
571                         /* name="value" */
572                         quote = *tmpp;
573                         tmpp++;
574                         attr_value = tmpp;
575                         if ((p = strchr(attr_value, quote)) == NULL) {
576                                 g_warning("html_get_tag(): syntax error in tag: '%s'\n", str);
577                                 return tag;
578                         }
579                         tmpp = p;
580                         *tmpp++ = '\0';
581                         while (isspace(*tmpp)) tmpp++;
582                 } else {
583                         /* name=value */
584                         attr_value = tmpp;
585                         while (*tmpp != '\0' && !isspace(*tmpp)) tmpp++;
586                         if (*tmpp != '\0')
587                                 *tmpp++ = '\0';
588                 }
589
590                 g_strchomp(attr_name);
591                 g_strdown(attr_name);
592                 attr = g_new(HTMLAttr, 1);
593                 attr->name = g_strdup(attr_name);
594                 attr->value = g_strdup(attr_value);
595                 tag->attr = g_list_append(tag->attr, attr);
596         }
597
598         return tag;
599 }
600
601 static void html_free_tag(HTMLTag *tag)
602 {
603         if (!tag) return;
604
605         g_free(tag->name);
606         while (tag->attr != NULL) {
607                 HTMLAttr *attr = (HTMLAttr *)tag->attr->data;
608                 g_free(attr->name);
609                 g_free(attr->value);
610                 g_free(attr);
611                 tag->attr = g_list_remove(tag->attr, tag->attr->data);
612         }
613         g_free(tag);
614 }
615
616 static HTMLState html_parse_tag(HTMLParser *parser)
617 {
618         gchar buf[HTMLBUFSIZE];
619         HTMLTag *tag;
620
621         html_get_parenthesis(parser, buf, sizeof(buf));
622
623         tag = html_get_tag(buf);
624
625         parser->state = HTML_UNKNOWN;
626         if (!tag) return HTML_UNKNOWN;
627
628         if (!strcmp(tag->name, "br")) {
629                 parser->space = FALSE;
630                 html_append_char(parser, '\n');
631                 parser->state = HTML_BR;
632         } else if (!strcmp(tag->name, "a")) {
633                 if (tag->attr && tag->attr->data &&
634                     !strcmp(((HTMLAttr *)tag->attr->data)->name, "href")) {
635                         g_free(parser->href);
636                         parser->href =
637                                 g_strdup(((HTMLAttr *)tag->attr->data)->value);
638                         parser->state = HTML_HREF;
639                 }
640         } else if (!strcmp(tag->name, "/a")) {
641                 g_free(parser->href);
642                 parser->href = NULL;
643                 parser->state = HTML_NORMAL;
644         } else if (!strcmp(tag->name, "p")) {
645                 parser->space = FALSE;
646                 if (!parser->empty_line) {
647                         parser->space = FALSE;
648                         if (!parser->newline) html_append_char(parser, '\n');
649                         html_append_char(parser, '\n');
650                 }
651                 parser->state = HTML_PAR;
652         } else if (!strcmp(tag->name, "pre")) {
653                 parser->pre = TRUE;
654                 parser->state = HTML_PRE;
655         } else if (!strcmp(tag->name, "/pre")) {
656                 parser->pre = FALSE;
657                 parser->state = HTML_NORMAL;
658         } else if (!strcmp(tag->name, "hr")) {
659                 if (!parser->newline) {
660                         parser->space = FALSE;
661                         html_append_char(parser, '\n');
662                 }
663                 html_append_str(parser, HR_STR "\n", -1);
664                 parser->state = HTML_HR;
665         } else if (!strcmp(tag->name, "div")    ||
666                    !strcmp(tag->name, "ul")     ||
667                    !strcmp(tag->name, "li")     ||
668                    !strcmp(tag->name, "table")  ||
669                    !strcmp(tag->name, "tr")     ||
670                    (tag->name[0] == 'h' && isdigit(tag->name[1]))) {
671                 if (!parser->newline) {
672                         parser->space = FALSE;
673                         html_append_char(parser, '\n');
674                 }
675                 parser->state = HTML_NORMAL;
676         } else if (!strcmp(tag->name, "/table") ||
677                    (tag->name[0] == '/' &&
678                     tag->name[1] == 'h' &&
679                     isdigit(tag->name[1]))) {
680                 if (!parser->empty_line) {
681                         parser->space = FALSE;
682                         if (!parser->newline) html_append_char(parser, '\n');
683                         html_append_char(parser, '\n');
684                 }
685                 parser->state = HTML_NORMAL;
686         } else if (!strcmp(tag->name, "/div")   ||
687                    !strcmp(tag->name, "/ul")    ||
688                    !strcmp(tag->name, "/li")) {
689                 if (!parser->newline) {
690                         parser->space = FALSE;
691                         html_append_char(parser, '\n');
692                 }
693                 parser->state = HTML_NORMAL;
694                         }
695
696         html_free_tag(tag);
697
698         return parser->state;
699 }
700
701 static void html_parse_special(HTMLParser *parser)
702 {
703         gchar symbol_name[9];
704         gint n;
705         const gchar *val;
706
707         parser->state = HTML_UNKNOWN;
708         g_return_if_fail(*parser->bufp == '&');
709
710         /* &foo; */
711         for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++)
712                 ;
713         if (n > 7 || parser->bufp[n] != ';') {
714                 /* output literal `&' */
715                 html_append_char(parser, *parser->bufp++);
716                 parser->state = HTML_NORMAL;
717                 return;
718         }
719         strncpy2(symbol_name, parser->bufp, n + 2);
720         parser->bufp += n + 1;
721
722         if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
723             != NULL) {
724                 html_append_str(parser, val, -1);
725                 parser->state = HTML_NORMAL;
726                 return;
727         } else if (symbol_name[1] == '#' && isdigit(symbol_name[2])) {
728                 gint ch;
729
730                 ch = atoi(symbol_name + 2);
731                 if ((ch > 0 && ch <= 127) ||
732                     (ch >= 128 && ch <= 255 &&
733                      parser->conv->charset == C_ISO_8859_1)) {
734                         html_append_char(parser, ch);
735                         parser->state = HTML_NORMAL;
736                         return;
737                 }
738         }
739
740         html_append_str(parser, symbol_name, -1);
741 }
742
743 static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len)
744 {
745         gchar *p;
746
747         buf[0] = '\0';
748         g_return_if_fail(*parser->bufp == '<');
749
750         /* ignore comment / CSS / script stuff */
751         if (!strncmp(parser->bufp, "<!--", 4)) {
752                 parser->bufp += 4;
753                 while ((p = strstr(parser->bufp, "-->")) == NULL)
754                         if (html_read_line(parser) == HTML_EOF) return;
755                 parser->bufp = p + 3;
756                 return;
757         }
758         if (!g_strncasecmp(parser->bufp, "<style", 6)) {
759                 parser->bufp += 6;
760                 while ((p = strcasestr(parser->bufp, "</style>")) == NULL)
761                         if (html_read_line(parser) == HTML_EOF) return;
762                 parser->bufp = p + 8;
763                 return;
764         }
765         if (!g_strncasecmp(parser->bufp, "<script", 7)) {
766                 parser->bufp += 7;
767                 while ((p = strcasestr(parser->bufp, "</script>")) == NULL)
768                         if (html_read_line(parser) == HTML_EOF) return;
769                 parser->bufp = p + 9;
770                 return;
771         }
772
773         parser->bufp++;
774         while ((p = strchr(parser->bufp, '>')) == NULL)
775                 if (html_read_line(parser) == HTML_EOF) return;
776
777         strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
778         g_strstrip(buf);
779         parser->bufp = p + 1;
780 }
781
782 /* these hash functions were taken from gstring.c in glib */
783 #if 0
784 static gint g_str_case_equal(gconstpointer v, gconstpointer v2)
785 {
786         return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
787 }
788
789 static guint g_str_case_hash(gconstpointer key)
790 {
791         const gchar *p = key;
792         guint h = *p;
793
794         if (h) {
795                 h = tolower(h);
796                 for (p += 1; *p != '\0'; p++)
797                         h = (h << 5) - h + tolower(*p);
798         }
799
800         return h;
801 }
802 #endif