fa32337d65595ffbb4606cca2d3ff21907c2c0b5
[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
323 HTMLParser *html_parser_new(FILE *fp, CodeConverter *conv)
324 {
325         HTMLParser *parser;
326
327         g_return_val_if_fail(fp != NULL, NULL);
328         g_return_val_if_fail(conv != NULL, NULL);
329
330         parser = g_new0(HTMLParser, 1);
331         parser->fp = fp;
332         parser->conv = conv;
333         parser->str = g_string_new(NULL);
334         parser->buf = g_string_new(NULL);
335         parser->bufp = parser->buf->str;
336         parser->state = HTML_NORMAL;
337         parser->href = NULL;
338         parser->newline = TRUE;
339         parser->empty_line = TRUE;
340         parser->space = FALSE;
341         parser->pre = FALSE;
342
343 #define SYMBOL_TABLE_ADD(table, list) \
344 { \
345         gint i; \
346  \
347         for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
348                 g_hash_table_insert(table, list[i].key, list[i].val); \
349 }
350
351         if (!default_symbol_table) {
352                 default_symbol_table =
353                         g_hash_table_new(g_str_hash, g_str_equal);
354                 SYMBOL_TABLE_ADD(default_symbol_table, symbol_list);
355                 SYMBOL_TABLE_ADD(default_symbol_table, ascii_symbol_list);
356         }
357         if (!eucjp_symbol_table) {
358                 eucjp_symbol_table =
359                         g_hash_table_new(g_str_hash, g_str_equal);
360                 SYMBOL_TABLE_ADD(eucjp_symbol_table, symbol_list);
361                 SYMBOL_TABLE_ADD(eucjp_symbol_table, eucjp_symbol_list);
362         }
363         if (!latin_symbol_table) {
364                 latin_symbol_table =
365                         g_hash_table_new(g_str_hash, g_str_equal);
366                 SYMBOL_TABLE_ADD(latin_symbol_table, symbol_list);
367                 SYMBOL_TABLE_ADD(latin_symbol_table, latin_symbol_list);
368         }
369
370 #undef SYMBOL_TABLE_ADD
371
372         if (conv->charset == C_ISO_8859_1)
373                 parser->symbol_table = latin_symbol_table;
374         else if ((conv->charset == C_ISO_2022_JP   ||
375                   conv->charset == C_ISO_2022_JP_2 ||
376                   conv->charset == C_EUC_JP        ||
377                   conv->charset == C_SHIFT_JIS) &&
378                  conv_get_current_charset() == C_EUC_JP)
379                 parser->symbol_table = eucjp_symbol_table;
380         else
381                 parser->symbol_table = default_symbol_table;
382
383         return parser;
384 }
385
386 void html_parser_destroy(HTMLParser *parser)
387 {
388         g_string_free(parser->str, TRUE);
389         g_string_free(parser->buf, TRUE);
390         g_free(parser->href);
391         g_free(parser);
392 }
393
394 gchar *html_parse(HTMLParser *parser)
395 {
396         parser->state = HTML_NORMAL;
397         g_string_truncate(parser->str, 0);
398
399         if (*parser->bufp == '\0') {
400                 g_string_truncate(parser->buf, 0);
401                 parser->bufp = parser->buf->str;
402                 if (html_read_line(parser) == HTML_EOF)
403                         return NULL;
404         }
405
406         while (*parser->bufp != '\0') {
407                 switch (*parser->bufp) {
408                 case '<': {
409                         HTMLState st;
410                         st = html_parse_tag(parser);
411                         /* when we see an href, we need to flush the str
412                          * buffer.  Then collect all the chars until we
413                          * see the end anchor tag
414                          */
415                         if (HTML_HREF_BEG == st || HTML_HREF == st)
416                             return parser->str->str;
417                         } break;
418                 case '&':
419                         html_parse_special(parser);
420                         break;
421                 case ' ':
422                 case '\t':
423                 case '\r':
424                 case '\n':
425                         if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
426                                 parser->bufp++;
427
428                         if (!parser->pre) {
429                                 if (!parser->newline)
430                                         parser->space = TRUE;
431
432                                 parser->bufp++;
433                                 break;
434                         }
435                         /* fallthrough */
436                 default:
437                         html_append_char(parser, *parser->bufp++);
438                 }
439         }
440
441         return parser->str->str;
442 }
443
444 static HTMLState html_read_line(HTMLParser *parser)
445 {
446         gchar buf[HTMLBUFSIZE];
447         gchar buf2[HTMLBUFSIZE];
448         gint index;
449
450         if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
451                 parser->state = HTML_EOF;
452                 return HTML_EOF;
453         }
454
455         if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
456                 index = parser->bufp - parser->buf->str;
457
458                 conv_localetodisp(buf2, sizeof(buf2), buf);
459                 g_string_append(parser->buf, buf2);
460
461                 parser->bufp = parser->buf->str + index;
462
463                 return HTML_CONV_FAILED;
464         }
465
466         index = parser->bufp - parser->buf->str;
467
468         g_string_append(parser->buf, buf2);
469
470         parser->bufp = parser->buf->str + index;
471
472         return HTML_NORMAL;
473 }
474
475 static void html_append_char(HTMLParser *parser, gchar ch)
476 {
477         GString *str = parser->str;
478
479         if (!parser->pre && parser->space) {
480                 g_string_append_c(str, ' ');
481                 parser->space = FALSE;
482         }
483
484         g_string_append_c(str, ch);
485
486         parser->empty_line = FALSE;
487         if (ch == '\n') {
488                 parser->newline = TRUE;
489                 if (str->len > 1 && str->str[str->len - 2] == '\n')
490                         parser->empty_line = TRUE;
491         } else
492                 parser->newline = FALSE;
493 }
494
495 static void html_append_str(HTMLParser *parser, const gchar *str, gint len)
496 {
497         GString *string = parser->str;
498
499         if (!parser->pre && parser->space) {
500                 g_string_append_c(string, ' ');
501                 parser->space = FALSE;
502         }
503
504         if (len == 0) return;
505         if (len < 0)
506                 g_string_append(string, str);
507         else {
508                 gchar *s;
509                 Xstrndup_a(s, str, len, return);
510                 g_string_append(string, s);
511         }
512
513         parser->empty_line = FALSE;
514         if (string->len > 0 && string->str[string->len - 1] == '\n') {
515                 parser->newline = TRUE;
516                 if (string->len > 1 && string->str[string->len - 2] == '\n')
517                         parser->empty_line = TRUE;
518         } else
519                 parser->newline = FALSE;
520 }
521
522 static HTMLTag *html_get_tag(const gchar *str)
523 {
524         HTMLTag *tag;
525         gchar *tmp;
526         guchar *tmpp;
527
528         g_return_val_if_fail(str != NULL, NULL);
529
530         if (*str == '\0' || *str == '!') return NULL;
531
532         Xstrdup_a(tmp, str, return NULL);
533
534         tag = g_new0(HTMLTag, 1);
535
536         for (tmpp = tmp; *tmpp != '\0' && !isspace(*tmpp); tmpp++)
537                 ;
538
539         if (*tmpp == '\0') {
540                 g_strdown(tmp);
541                 tag->name = g_strdup(tmp);
542                 return tag;
543         } else {
544                 *tmpp++ = '\0';
545                 g_strdown(tmp);
546                 tag->name = g_strdup(tmp);
547         }
548
549         while (*tmpp != '\0') {
550                 HTMLAttr *attr;
551                 gchar *attr_name;
552                 gchar *attr_value;
553                 gchar *p;
554                 gchar quote;
555
556                 while (isspace(*tmpp)) tmpp++;
557                 attr_name = tmpp;
558
559                 while (*tmpp != '\0' && !isspace(*tmpp) && *tmpp != '=') tmpp++;
560                 if (*tmpp != '\0' && *tmpp != '=') {
561                         *tmpp++ = '\0';
562                         while (isspace(*tmpp)) tmpp++;
563                 }
564
565                 if (*tmpp == '=') {
566                         *tmpp++ = '\0';
567                         while (isspace(*tmpp)) tmpp++;
568
569                         if (*tmpp == '"' || *tmpp == '\'') {
570                                 /* name="value" */
571                                 quote = *tmpp;
572                                 tmpp++;
573                                 attr_value = tmpp;
574                                 if ((p = strchr(attr_value, quote)) == NULL) {
575                                         g_warning("html_get_tag(): syntax error in tag: '%s'\n", str);
576                                         return tag;
577                                 }
578                                 tmpp = p;
579                                 *tmpp++ = '\0';
580                                 while (isspace(*tmpp)) tmpp++;
581                         } else {
582                                 /* name=value */
583                                 attr_value = tmpp;
584                                 while (*tmpp != '\0' && !isspace(*tmpp)) tmpp++;
585                                 if (*tmpp != '\0')
586                                         *tmpp++ = '\0';
587                         }
588                 } else
589                         attr_value = "";
590
591                 g_strchomp(attr_name);
592                 g_strdown(attr_name);
593                 attr = g_new(HTMLAttr, 1);
594                 attr->name = g_strdup(attr_name);
595                 attr->value = g_strdup(attr_value);
596                 tag->attr = g_list_append(tag->attr, attr);
597         }
598
599         return tag;
600 }
601
602 static void html_free_tag(HTMLTag *tag)
603 {
604         if (!tag) return;
605
606         g_free(tag->name);
607         while (tag->attr != NULL) {
608                 HTMLAttr *attr = (HTMLAttr *)tag->attr->data;
609                 g_free(attr->name);
610                 g_free(attr->value);
611                 g_free(attr);
612                 tag->attr = g_list_remove(tag->attr, tag->attr->data);
613         }
614         g_free(tag);
615 }
616
617 static HTMLState html_parse_tag(HTMLParser *parser)
618 {
619         gchar buf[HTMLBUFSIZE];
620         HTMLTag *tag;
621
622         html_get_parenthesis(parser, buf, sizeof(buf));
623
624         tag = html_get_tag(buf);
625
626         parser->state = HTML_UNKNOWN;
627         if (!tag) return HTML_UNKNOWN;
628
629         if (!strcmp(tag->name, "br")) {
630                 parser->space = FALSE;
631                 html_append_char(parser, '\n');
632                 parser->state = HTML_BR;
633         } else if (!strcmp(tag->name, "a")) {
634                 if (tag->attr && tag->attr->data &&
635                     !strcmp(((HTMLAttr *)tag->attr->data)->name, "href")) {
636                         g_free(parser->href);
637                         parser->href =
638                                 g_strdup(((HTMLAttr *)tag->attr->data)->value);
639                         parser->state = HTML_HREF_BEG;
640                 }
641         } else if (!strcmp(tag->name, "/a")) {
642                 parser->state = HTML_HREF;
643         } else if (!strcmp(tag->name, "p")) {
644                 parser->space = FALSE;
645                 if (!parser->empty_line) {
646                         parser->space = FALSE;
647                         if (!parser->newline) html_append_char(parser, '\n');
648                         html_append_char(parser, '\n');
649                 }
650                 parser->state = HTML_PAR;
651         } else if (!strcmp(tag->name, "pre")) {
652                 parser->pre = TRUE;
653                 parser->state = HTML_PRE;
654         } else if (!strcmp(tag->name, "/pre")) {
655                 parser->pre = FALSE;
656                 parser->state = HTML_NORMAL;
657         } else if (!strcmp(tag->name, "hr")) {
658                 if (!parser->newline) {
659                         parser->space = FALSE;
660                         html_append_char(parser, '\n');
661                 }
662                 html_append_str(parser, HR_STR "\n", -1);
663                 parser->state = HTML_HR;
664         } else if (!strcmp(tag->name, "div")    ||
665                    !strcmp(tag->name, "ul")     ||
666                    !strcmp(tag->name, "li")     ||
667                    !strcmp(tag->name, "table")  ||
668                    !strcmp(tag->name, "tr")     ||
669                    (tag->name[0] == 'h' && isdigit((guchar)tag->name[1]))) {
670                 if (!parser->newline) {
671                         parser->space = FALSE;
672                         html_append_char(parser, '\n');
673                 }
674                 parser->state = HTML_NORMAL;
675         } else if (!strcmp(tag->name, "/table") ||
676                    (tag->name[0] == '/' &&
677                     tag->name[1] == 'h' &&
678                     isdigit((guchar)tag->name[1]))) {
679                 if (!parser->empty_line) {
680                         parser->space = FALSE;
681                         if (!parser->newline) html_append_char(parser, '\n');
682                         html_append_char(parser, '\n');
683                 }
684                 parser->state = HTML_NORMAL;
685         } else if (!strcmp(tag->name, "/div")   ||
686                    !strcmp(tag->name, "/ul")    ||
687                    !strcmp(tag->name, "/li")) {
688                 if (!parser->newline) {
689                         parser->space = FALSE;
690                         html_append_char(parser, '\n');
691                 }
692                 parser->state = HTML_NORMAL;
693                         }
694
695         html_free_tag(tag);
696
697         return parser->state;
698 }
699
700 static void html_parse_special(HTMLParser *parser)
701 {
702         gchar symbol_name[9];
703         gint n;
704         const gchar *val;
705
706         parser->state = HTML_UNKNOWN;
707         g_return_if_fail(*parser->bufp == '&');
708
709         /* &foo; */
710         for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++)
711                 ;
712         if (n > 7 || parser->bufp[n] != ';') {
713                 /* output literal `&' */
714                 html_append_char(parser, *parser->bufp++);
715                 parser->state = HTML_NORMAL;
716                 return;
717         }
718         strncpy2(symbol_name, parser->bufp, n + 2);
719         parser->bufp += n + 1;
720
721         if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
722             != NULL) {
723                 html_append_str(parser, val, -1);
724                 parser->state = HTML_NORMAL;
725                 return;
726         } else if (symbol_name[1] == '#' && isdigit((guchar)symbol_name[2])) {
727                 gint ch;
728
729                 ch = atoi(symbol_name + 2);
730                 if ((ch > 0 && ch <= 127) ||
731                     (ch >= 128 && ch <= 255 &&
732                      parser->conv->charset == C_ISO_8859_1)) {
733                         html_append_char(parser, ch);
734                         parser->state = HTML_NORMAL;
735                         return;
736                 } else {
737                     char *symb = NULL;
738                     switch (ch) {
739                     /* http://www.w3schools.com/html/html_entitiesref.asp */
740                     case 338:   /* capital ligature OE  &OElig;  */
741                         symb = "OE";  break;
742                     case 339:   /* small ligature OE    &oelig;  */
743                         symb = "oe";  break;
744                     case 352:   /* capital S w/caron    &Scaron; */
745                     case 353:   /* small S w/caron      &scaron; */
746                     case 376:   /* cap Y w/ diaeres     &Yuml;   */
747                         break;
748                     case 710:   /* circumflex accent    &circ;   */
749                         symb = "^";  break;
750                     case 732:   /* small tilde          &tilde;  */
751                         symb = "~";  break;
752                     case 8194:  /* en space             &ensp;   */
753                     case 8195:  /* em space             &emsp;   */
754                     case 8201:  /* thin space           &thinsp; */
755                         symb = " ";  break;
756                     case 8204:  /* zero width non-joiner &zwnj;  */
757                     case 8205:  /* zero width joiner    &zwj;    */
758                     case 8206:  /* l-t-r mark           &lrm;    */
759                     case 8207:  /* r-t-l mark           &rlm     */
760                         break;
761                     case 8211:  /* en dash              &ndash;  */
762                         symb = "-";  break;
763                     case 8212:  /* em dash              &mdash;  */
764                         symb = "--";  break;
765                     case 8216:  /* l single quot mark   &lsquo;  */
766                         symb = "`";  break;
767                     case 8217:  /* r single quot mark   &rsquo;  */
768                         symb = "'";  break;
769                     case 8218:  /* single low-9 quot    &sbquo;  */
770                         symb = ",";  break;
771                     case 8220:  /* l double quot mark   &ldquo;  */
772                         symb = "``";  break;
773                     case 8221:  /* r double quot mark   &rdquo;  */
774                         symb = "''";  break;
775                     case 8222:  /* double low-9 quot    &bdquo;  */
776                         symb = ",,";  break;
777                     case 8224:  /* dagger               &dagger; */
778                     case 8225:  /* double dagger        &Dagger; */
779                         break;
780                     case 8230:  /* horizontal ellipsis  &hellip; */
781                         symb = "...";  break;
782                     case 8240:  /* per mile             &permil; */
783                         symb = "\%o";  break;
784                     case 8249:  /* l-pointing angle quot &lsaquo; */
785                         symb = "<";  break;
786                     case 8250:  /* r-pointing angle quot &rsaquo; */
787                         symb = ">";  break;
788                     case 8364:  /* euro                 &euro;   */
789                         symb = "&euro";  break;
790                     case 8482:  /* trademark            &trade;  */
791                         symb  = "(TM)";  break;
792                     default: break;
793                     }
794                     if (symb) {
795                         html_append_str(parser, symb, -1);
796                         parser->state = HTML_NORMAL;
797                         return;
798                     }
799                 }
800         }
801
802         html_append_str(parser, symbol_name, -1);
803 }
804
805 static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len)
806 {
807         gchar *p;
808
809         buf[0] = '\0';
810         g_return_if_fail(*parser->bufp == '<');
811
812         /* ignore comment / CSS / script stuff */
813         if (!strncmp(parser->bufp, "<!--", 4)) {
814                 parser->bufp += 4;
815                 while ((p = strstr(parser->bufp, "-->")) == NULL)
816                         if (html_read_line(parser) == HTML_EOF) return;
817                 parser->bufp = p + 3;
818                 return;
819         }
820         if (!g_strncasecmp(parser->bufp, "<style", 6)) {
821                 parser->bufp += 6;
822                 while ((p = strcasestr(parser->bufp, "</style>")) == NULL)
823                         if (html_read_line(parser) == HTML_EOF) return;
824                 parser->bufp = p + 8;
825                 return;
826         }
827         if (!g_strncasecmp(parser->bufp, "<script", 7)) {
828                 parser->bufp += 7;
829                 while ((p = strcasestr(parser->bufp, "</script>")) == NULL)
830                         if (html_read_line(parser) == HTML_EOF) return;
831                 parser->bufp = p + 9;
832                 return;
833         }
834
835         parser->bufp++;
836         while ((p = strchr(parser->bufp, '>')) == NULL)
837                 if (html_read_line(parser) == HTML_EOF) return;
838
839         strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
840         g_strstrip(buf);
841         parser->bufp = p + 1;
842 }