/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <glib.h>
#include "codeconv.h"
#include "utils.h"
-#define HTMLBUFSIZE 8192
+#define SC_HTMLBUFSIZE 8192
#define HR_STR "------------------------------------------------"
-typedef struct _HTMLSymbol HTMLSymbol;
+typedef struct _SC_HTMLSymbol SC_HTMLSymbol;
-struct _HTMLSymbol
+struct _SC_HTMLSymbol
{
gchar *const key;
gchar *const val;
};
-static HTMLSymbol symbol_list[] = {
- {"<" , "<"},
- {">" , ">"},
- {"&" , "&"},
- {""" , "\""},
- {" " , " "},
- {"™" , "(TM)"},
-
- {"™", "(TM)"},
+static SC_HTMLSymbol symbol_list[] = {
+ {"<", "\74"},
+ {">", "\76"},
+ {"&", "\46"},
+ {""", "\42"},
+ {"‘", "\47"},
+ {"’", "\47"},
+ {"“", "\42"},
+ {"”", "\42"},
+ {"«", "\302\253"},
+ {"»", "\302\273"},
+ {" ", "\40"},
+ {"™", "\50\124\115\51"},
+ {"…", "\56\56\56"},
+ {"•", "\52"},
+ {"–", "\55"},
+ {"—", "\55\55"},
+ {"€", "\105\125\122"},
+ {"¢", "\302\242"},
+ {"£", "\302\243"},
+ {"¤", "\302\244"},
+ {"¥", "\302\245"},
+ {"©", "\302\251"},
+ {"®", "\302\256"},
+ {"¿", "\302\277"},
+ {"¡", "\302\241"}
};
-static HTMLSymbol ascii_symbol_list[] = {
- {"¡" , "^!"},
- {"¦", "|"},
- {"©" , "(C)"},
- {"«" , "<<"},
- {"®" , "(R)"},
-
- {"²" , "^2"},
- {"³" , "^3"},
- {"´" , "'"},
- {"¸" , ","},
- {"¹" , "^1"},
- {"»" , ">>"},
- {"¼", "1/4"},
- {"½", "1/2"},
- {"¾", "3/4"},
- {"¿", "^?"},
-
- {"À", "A`"},
- {"Á", "A'"},
- {"Â" , "A^"},
- {"Ã", "A~"},
- {"Æ" , "AE"},
- {"È", "E`"},
- {"É", "E'"},
- {"Ê" , "E^"},
- {"Ì", "I`"},
- {"Í", "I'"},
- {"Î" , "I^"},
-
- {"Ñ", "N~"},
- {"Ò", "O`"},
- {"Ó", "O'"},
- {"Ô" , "O^"},
- {"Õ", "O~"},
- {"Ù", "U`"},
- {"Ú", "U'"},
- {"Û" , "U^"},
- {"Ý", "Y'"},
-
- {"à", "a`"},
- {"á", "a'"},
- {"â" , "a^"},
- {"ã", "a~"},
- {"æ" , "ae"},
- {"è", "e`"},
- {"é", "e'"},
- {"ê" , "e^"},
- {"ì", "i`"},
- {"í", "i'"},
- {"î" , "i^"},
-
- {"ñ", "n~"},
- {"ò", "o`"},
- {"ó", "o'"},
- {"ô" , "o^"},
- {"õ", "o~"},
- {"ù", "u`"},
- {"ú", "u'"},
- {"û" , "u^"},
- {"ý", "y'"},
+static SC_HTMLSymbol ascii_symbol_list[] = {
+ {"¡" , "\302\241"},
+ {"¦", "\302\246"},
+ {"©" , "\302\251"},
+ {"«" , "\302\253"},
+ {"®" , "\302\256"},
+
+ {"²" , "\302\262"},
+ {"³" , "\302\263"},
+ {"´" , "\302\264"},
+ {"¸" , "\302\270"},
+ {"¹" , "\302\271"},
+ {"»" , "\302\273"},
+ {"¼", "\302\274"},
+ {"½", "\302\275"},
+ {"¾", "\302\276"},
+ {"¿", "\302\277"},
+
+ {"À", "\303\200"},
+ {"Á", "\303\201"},
+ {"Â" , "\303\202"},
+ {"Ã", "\303\203"},
+ {"Æ" , "\303\206"},
+ {"È", "\303\210"},
+ {"É", "\303\211"},
+ {"Ê" , "\303\212"},
+ {"Ì", "\303\214"},
+ {"Í", "\303\215"},
+ {"Î" , "\303\216"},
+
+ {"Ñ", "\303\221"},
+ {"Ò", "\303\222"},
+ {"Ó", "\303\223"},
+ {"Ô" , "\303\224"},
+ {"Õ", "\303\225"},
+ {"Ù", "\303\231"},
+ {"Ú", "\303\232"},
+ {"Û" , "\303\233"},
+ {"Ý", "\303\235"},
+
+ {"à", "\303\240"},
+ {"á", "\303\241"},
+ {"â" , "\303\242"},
+ {"ã", "\303\243"},
+ {"æ" , "\303\246"},
+ {"è", "\303\250"},
+ {"é", "\303\251"},
+ {"ê" , "\303\252"},
+ {"ì", "\303\254"},
+ {"í", "\303\255"},
+ {"î" , "\303\256"},
+
+ {"ñ", "\303\261"},
+ {"ò", "\303\262"},
+ {"ó", "\303\263"},
+ {"ô" , "\303\264"},
+ {"õ", "\303\265"},
+ {"ù", "\303\271"},
+ {"ú", "\303\272"},
+ {"û" , "\303\273"},
+ {"ý", "\303\275"}
};
-static HTMLSymbol eucjp_symbol_list[] = {
- {"¡" , "^!"},
- {"¢" , "\xa1\xf1"},
- {"£" , "\xa1\xf2"},
- {"¥" , "\xa1\xef"},
- {"¦", "|"},
- {"§" , "\xa1\xf8"},
- {"¨" , "\xa1\xaf"},
- {"©" , "(C)"},
- {"«" , "<<"},
- {"®" , "(R)"},
-
- {"°" , "\xa1\xeb"},
- {"±", "\xa1\xde"},
- {"²" , "^2"},
- {"³" , "^3"},
- {"´" , "'"},
- {"µ" , "\xa6\xcc"},
- {"¶" , "\xa2\xf9"},
- {"·", "\xa1\xa6"},
- {"¸" , ","},
- {"¹" , "^1"},
- {"»" , ">>"},
- {"¼", "1/4"},
- {"½", "1/2"},
- {"¾", "3/4"},
- {"¿", "^?"},
-
- {"À", "A`"},
- {"Á", "A'"},
- {"Â" , "A^"},
- {"Ã", "A~"},
- {"Ä" , "A\xa1\xaf"},
- {"Å" , "A\xa1\xeb"},
- {"Æ" , "AE"},
- {"È", "E`"},
- {"É", "E'"},
- {"Ê" , "E^"},
- {"Ë" , "E\xa1\xaf"},
- {"Ì", "I`"},
- {"Í", "I'"},
- {"Î" , "I^"},
- {"Ï" , "I\xa1\xaf"},
-
- {"Ñ", "N~"},
- {"Ò", "O`"},
- {"Ó", "O'"},
- {"Ô" , "O^"},
- {"Õ", "O~"},
- {"Ö" , "O\xa1\xaf"},
- {"×" , "\xa1\xdf"},
- {"Ù", "U`"},
- {"Ú", "U'"},
- {"Û" , "U^"},
- {"Ü" , "U\xa1\xaf"},
- {"Ý", "Y'"},
-
- {"à", "a`"},
- {"á", "a'"},
- {"â" , "a^"},
- {"ã", "a~"},
- {"ä" , "a\xa1\xaf"},
- {"å" , "a\xa1\xeb"},
- {"æ" , "ae"},
- {"è", "e`"},
- {"é", "e'"},
- {"ê" , "e^"},
- {"ë" , "e\xa1\xaf"},
- {"ì", "i`"},
- {"í", "i'"},
- {"î" , "i^"},
- {"ï" , "i\xa1\xaf"},
-
- {"ð" , "\xa2\xdf"},
- {"ñ", "n~"},
- {"ò", "o`"},
- {"ó", "o'"},
- {"ô" , "o^"},
- {"õ", "o~"},
- {"ö" , "o\xa1\xaf"},
- {"÷", "\xa1\xe0"},
- {"ù", "u`"},
- {"ú", "u'"},
- {"û" , "u^"},
- {"ü" , "u\xa1\xaf"},
- {"ý", "y'"},
- {"ÿ" , "y\xa1\xaf"},
+typedef struct _SC_HTMLAltSymbol SC_HTMLAltSymbol;
+
+struct _SC_HTMLAltSymbol
+{
+ gint key;
+ gchar *const val;
};
-static HTMLSymbol latin_symbol_list[] = {
- {"¡" , "\xa1"},
- {"¢" , "\xa2"},
- {"£" , "\xa3"},
- {"¤", "\xa4"},
- {"¥" , "\xa5"},
- {"¦", "\xa6"},
- {"§" , "\xa7"},
- {"¨" , "\xa8"},
- {"©" , "\xa9"},
- {"ª" , "\xaa"},
- {"«" , "\xab"},
- {"¬" , "\xac"},
- {"­" , "\xad"},
- {"®" , "\xae"},
- {"¯" , "\xaf"},
-
- {"°" , "\xb0"},
- {"±", "\xb1"},
- {"²" , "\xb2"},
- {"³" , "\xb3"},
- {"´" , "\xb4"},
- {"µ" , "\xb5"},
- {"¶" , "\xb6"},
- {"·", "\xb7"},
- {"¸" , "\xb8"},
- {"¹" , "\xb9"},
- {"º" , "\xba"},
- {"»" , "\xbb"},
- {"¼", "\xbc"},
- {"½", "\xbd"},
- {"¾", "\xbe"},
- {"¿", "\xbf"},
-
- {"À", "\xc0"},
- {"Á", "\xc1"},
- {"Â" , "\xc2"},
- {"Ã", "\xc3"},
- {"Ä" , "\xc4"},
- {"Å" , "\xc5"},
- {"Æ" , "\xc6"},
- {"Ç", "\xc7"},
- {"È", "\xc8"},
- {"É", "\xc9"},
- {"Ê" , "\xca"},
- {"Ë" , "\xcb"},
- {"Ì", "\xcc"},
- {"Í", "\xcd"},
- {"Î" , "\xce"},
- {"Ï" , "\xcf"},
-
- {"Ð" , "\xd0"},
- {"Ñ", "\xd1"},
- {"Ò", "\xd2"},
- {"Ó", "\xd3"},
- {"Ô" , "\xd4"},
- {"Õ", "\xd5"},
- {"Ö" , "\xd6"},
- {"×" , "\xd7"},
- {"Ø", "\xd8"},
- {"Ù", "\xd9"},
- {"Ú", "\xda"},
- {"Û" , "\xdb"},
- {"Ü" , "\xdc"},
- {"Ý", "\xdd"},
- {"Þ" , "\xde"},
- {"ß" , "\xdf"},
-
- {"à", "\xe0"},
- {"á", "\xe1"},
- {"â" , "\xe2"},
- {"ã", "\xe3"},
- {"ä" , "\xe4"},
- {"å" , "\xe5"},
- {"æ" , "\xe6"},
- {"ç", "\xe7"},
- {"è", "\xe8"},
- {"é", "\xe9"},
- {"ê" , "\xea"},
- {"ë" , "\xeb"},
- {"ì", "\xec"},
- {"í", "\xed"},
- {"î" , "\xee"},
- {"ï" , "\xef"},
-
- {"ð" , "\xf0"},
- {"ñ", "\xf1"},
- {"ò", "\xf2"},
- {"ó", "\xf3"},
- {"ô" , "\xf4"},
- {"õ", "\xf5"},
- {"ö" , "\xf6"},
- {"÷", "\xf7"},
- {"ø", "\xf8"},
- {"ù", "\xf9"},
- {"ú", "\xfa"},
- {"û" , "\xfb"},
- {"ü" , "\xfc"},
- {"ý", "\xfd"},
- {"þ" , "\xfe"},
- {"ÿ" , "\xff"},
+/* http://www.w3schools.com/html/html_entitiesref.asp */
+static SC_HTMLAltSymbol alternate_symbol_list[] = {
+ { 96, "\140"}, /* backtick */
+ { 153, "\50\124\115\51"}, /* trademark */
+ { 161, "\302\241"}, /* inverted exclamation mark ¡ */
+ { 162, "\302\242"}, /* cent (currency) ¢ */
+ { 163, "\302\243"}, /* pound (currency) £ */
+ { 164, "\342\202\254"}, /* currency sign ¤ */
+ { 165, "\302\245"}, /* yen (currency) ¥ */
+ { 169, "\302\251"}, /* copyright sign © */
+ { 174, "\302\256"}, /* registered sign ® */
+ { 191, "\302\277"}, /* inverted question mark ¿ */
+ { 338, "\117\105"}, /* capital ligature OE &OElig */
+ { 339, "\157\145"}, /* small ligature OE &oelig */
+ { 352, NULL}, /* capital S w/caron &Scaron */
+ { 353, NULL}, /* small S w/caron &scaron */
+ { 376, NULL}, /* cap Y w/ diaeres &Yuml */
+ { 710, "\136"}, /* circumflex accent &circ */
+ { 732, "\176"}, /* small tilde &tilde */
+ {8194, "\40"}, /* en space &ensp */
+ {8195, "\40"}, /* em space &emsp */
+ {8201, "\40"}, /* thin space &thinsp */
+ {8204, NULL}, /* zero width non-joiner &zwnj */
+ {8205, NULL}, /* zero width joiner &zwj */
+ {8206, NULL}, /* l-t-r mark &lrm */
+ {8207, NULL}, /* r-t-l mark &rlm */
+ {8211, "\55"}, /* en dash &ndash */
+ {8212, "\55\55"}, /* em dash &mdash */
+ {8216, "\47"}, /* l single quot mark &lsquo */
+ {8217, "\47"}, /* r single quot mark &rsquo */
+ {8218, "\54"}, /* single low-9 quot &sbquo */
+ {8220, "\134"}, /* l double quot mark &ldquo */
+ {8221, "\134"}, /* r double quot mark &rdquo */
+ {8222, "\42"}, /* double low-9 quot &bdquo */
+ {8224, NULL}, /* dagger &dagger */
+ {8225, NULL}, /* double dagger &Dagger */
+ {8226, "\52"}, /* bullet &bull */
+ {8230, "\56\56\56"}, /* horizontal ellipsis &hellip */
+ {8240, "\45\157"}, /* per mile &permil */
+ {8249, "\74"}, /* l-pointing angle quot &lsaquo */
+ {8250, "\76"}, /* r-pointing angle quot &rsaquo */
+ {8364, "\105\125\122"}, /* euro &euro */
+ {8482, "\50\124\115\51"} /* trademark &trade */
};
static GHashTable *default_symbol_table;
-static GHashTable *eucjp_symbol_table;
-static GHashTable *latin_symbol_table;
+static GHashTable *alternate_symbol_table;
-static HTMLState html_read_line (HTMLParser *parser);
-static void html_append_char (HTMLParser *parser,
+static SC_HTMLState sc_html_read_line (SC_HTMLParser *parser);
+static void sc_html_append_char (SC_HTMLParser *parser,
gchar ch);
-static void html_append_str (HTMLParser *parser,
+static void sc_html_append_str (SC_HTMLParser *parser,
const gchar *str,
gint len);
-static HTMLState html_parse_tag (HTMLParser *parser);
-static void html_parse_special (HTMLParser *parser);
-static void html_get_parenthesis (HTMLParser *parser,
+static SC_HTMLState sc_html_parse_tag (SC_HTMLParser *parser);
+static void sc_html_parse_special (SC_HTMLParser *parser);
+static void sc_html_get_parenthesis (SC_HTMLParser *parser,
gchar *buf,
gint len);
-#if 0
-static gint g_str_case_equal (gconstpointer v,
- gconstpointer v2);
-static guint g_str_case_hash (gconstpointer key);
-#endif
-HTMLParser *html_parser_new(FILE *fp, CodeConverter *conv)
+SC_HTMLParser *sc_html_parser_new(FILE *fp, CodeConverter *conv)
{
- HTMLParser *parser;
+ SC_HTMLParser *parser;
g_return_val_if_fail(fp != NULL, NULL);
g_return_val_if_fail(conv != NULL, NULL);
- parser = g_new0(HTMLParser, 1);
+ parser = g_new0(SC_HTMLParser, 1);
parser->fp = fp;
parser->conv = conv;
parser->str = g_string_new(NULL);
parser->buf = g_string_new(NULL);
parser->bufp = parser->buf->str;
+ parser->state = SC_HTML_NORMAL;
+ parser->href = NULL;
parser->newline = TRUE;
parser->empty_line = TRUE;
parser->space = FALSE;
for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
g_hash_table_insert(table, list[i].key, list[i].val); \
}
+#define SYMBOL_TABLE_REF_ADD(table, list) \
+{ \
+ gint i; \
+ \
+ for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) \
+ g_hash_table_insert(table, &list[i].key, list[i].val); \
+}
if (!default_symbol_table) {
default_symbol_table =
SYMBOL_TABLE_ADD(default_symbol_table, symbol_list);
SYMBOL_TABLE_ADD(default_symbol_table, ascii_symbol_list);
}
- if (!eucjp_symbol_table) {
- eucjp_symbol_table =
- g_hash_table_new(g_str_hash, g_str_equal);
- SYMBOL_TABLE_ADD(eucjp_symbol_table, symbol_list);
- SYMBOL_TABLE_ADD(eucjp_symbol_table, eucjp_symbol_list);
- }
- if (!latin_symbol_table) {
- latin_symbol_table =
- g_hash_table_new(g_str_hash, g_str_equal);
- SYMBOL_TABLE_ADD(latin_symbol_table, symbol_list);
- SYMBOL_TABLE_ADD(latin_symbol_table, latin_symbol_list);
+ if (!alternate_symbol_table) {
+ alternate_symbol_table =
+ g_hash_table_new(g_int_hash, g_int_equal);
+ SYMBOL_TABLE_REF_ADD(alternate_symbol_table, alternate_symbol_list);
}
#undef SYMBOL_TABLE_ADD
+#undef SYMBOL_TABLE_REF_ADD
- if (conv->charset == C_ISO_8859_1)
- parser->symbol_table = latin_symbol_table;
- else if ((conv->charset == C_ISO_2022_JP ||
- conv->charset == C_ISO_2022_JP_2 ||
- conv->charset == C_EUC_JP ||
- conv->charset == C_SHIFT_JIS) &&
- conv_get_current_charset() == C_EUC_JP)
- parser->symbol_table = eucjp_symbol_table;
- else
- parser->symbol_table = default_symbol_table;
+ parser->symbol_table = default_symbol_table;
+ parser->alt_symbol_table = alternate_symbol_table;
return parser;
}
-void html_parser_destroy(HTMLParser *parser)
+void sc_html_parser_destroy(SC_HTMLParser *parser)
{
g_string_free(parser->str, TRUE);
g_string_free(parser->buf, TRUE);
+ g_free(parser->href);
g_free(parser);
}
-gchar *html_parse(HTMLParser *parser)
+gchar *sc_html_parse(SC_HTMLParser *parser)
{
- parser->state = HTML_NORMAL;
+ parser->state = SC_HTML_NORMAL;
g_string_truncate(parser->str, 0);
if (*parser->bufp == '\0') {
g_string_truncate(parser->buf, 0);
parser->bufp = parser->buf->str;
- if (html_read_line(parser) == HTML_EOF)
+ if (sc_html_read_line(parser) == SC_HTML_EOF)
return NULL;
}
while (*parser->bufp != '\0') {
switch (*parser->bufp) {
- case '<':
- if (parser->str->len == 0)
- html_parse_tag(parser);
- else
+ case '<': {
+ SC_HTMLState st;
+ st = sc_html_parse_tag(parser);
+ /* when we see an href, we need to flush the str
+ * buffer. Then collect all the chars until we
+ * see the end anchor tag
+ */
+ if (SC_HTML_HREF_BEG == st || SC_HTML_HREF == st)
return parser->str->str;
+ }
break;
case '&':
- html_parse_special(parser);
+ sc_html_parse_special(parser);
break;
case ' ':
case '\t':
}
/* fallthrough */
default:
- html_append_char(parser, *parser->bufp++);
+ sc_html_append_char(parser, *parser->bufp++);
}
}
return parser->str->str;
}
-static HTMLState html_read_line(HTMLParser *parser)
+static SC_HTMLState sc_html_read_line(SC_HTMLParser *parser)
{
- gchar buf[HTMLBUFSIZE];
- gchar buf2[HTMLBUFSIZE];
+ gchar buf[SC_HTMLBUFSIZE];
+ gchar buf2[SC_HTMLBUFSIZE];
gint index;
if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
- parser->state = HTML_EOF;
- return HTML_EOF;
+ parser->state = SC_HTML_EOF;
+ return SC_HTML_EOF;
}
if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
- g_warning("html_read_line(): code conversion failed\n");
-
index = parser->bufp - parser->buf->str;
- g_string_append(parser->buf, buf);
+ conv_utf8todisp(buf2, sizeof(buf2), buf);
+ g_string_append(parser->buf, buf2);
parser->bufp = parser->buf->str + index;
- return HTML_ERR;
+ return SC_HTML_CONV_FAILED;
}
index = parser->bufp - parser->buf->str;
parser->bufp = parser->buf->str + index;
- return HTML_NORMAL;
+ return SC_HTML_NORMAL;
}
-static void html_append_char(HTMLParser *parser, gchar ch)
+static void sc_html_append_char(SC_HTMLParser *parser, gchar ch)
{
GString *str = parser->str;
parser->newline = FALSE;
}
-static void html_append_str(HTMLParser *parser, const gchar *str, gint len)
+static void sc_html_append_str(SC_HTMLParser *parser, const gchar *str, gint len)
{
GString *string = parser->str;
parser->newline = FALSE;
}
-static HTMLState html_parse_tag(HTMLParser *parser)
+static SC_HTMLTag *sc_html_get_tag(const gchar *str)
{
- gchar buf[HTMLBUFSIZE];
- gchar *p;
- static gboolean is_in_href = FALSE;
+ SC_HTMLTag *tag;
+ gchar *tmp;
+ guchar *tmpp;
- html_get_parenthesis(parser, buf, sizeof(buf));
+ g_return_val_if_fail(str != NULL, NULL);
- for (p = buf; *p != '\0'; p++) {
- if (isspace(*p)) {
- *p = '\0';
- break;
+ if (*str == '\0' || *str == '!') return NULL;
+
+ Xstrdup_a(tmp, str, return NULL);
+
+ tag = g_new0(SC_HTMLTag, 1);
+
+ for (tmpp = tmp; *tmpp != '\0' && !g_ascii_isspace(*tmpp); tmpp++)
+ ;
+
+ if (*tmpp == '\0') {
+ g_strdown(tmp);
+ tag->name = g_strdup(tmp);
+ return tag;
+ } else {
+ *tmpp++ = '\0';
+ g_strdown(tmp);
+ tag->name = g_strdup(tmp);
+ }
+
+ while (*tmpp != '\0') {
+ SC_HTMLAttr *attr;
+ gchar *attr_name;
+ gchar *attr_value;
+ gchar *p;
+ gchar quote;
+
+ while (g_ascii_isspace(*tmpp)) tmpp++;
+ attr_name = tmpp;
+
+ while (*tmpp != '\0' && !g_ascii_isspace(*tmpp) &&
+ *tmpp != '=')
+ tmpp++;
+ if (*tmpp != '\0' && *tmpp != '=') {
+ *tmpp++ = '\0';
+ while (g_ascii_isspace(*tmpp)) tmpp++;
}
+
+ if (*tmpp == '=') {
+ *tmpp++ = '\0';
+ while (g_ascii_isspace(*tmpp)) tmpp++;
+
+ if (*tmpp == '"' || *tmpp == '\'') {
+ /* name="value" */
+ quote = *tmpp;
+ tmpp++;
+ attr_value = tmpp;
+ if ((p = strchr(attr_value, quote)) == NULL) {
+ g_warning("sc_html_get_tag(): syntax error in tag: '%s'\n", str);
+ return tag;
+ }
+ tmpp = p;
+ *tmpp++ = '\0';
+ while (g_ascii_isspace(*tmpp)) tmpp++;
+ } else {
+ /* name=value */
+ attr_value = tmpp;
+ while (*tmpp != '\0' && !g_ascii_isspace(*tmpp)) tmpp++;
+ if (*tmpp != '\0')
+ *tmpp++ = '\0';
+ }
+ } else
+ attr_value = "";
+
+ g_strchomp(attr_name);
+ g_strdown(attr_name);
+ attr = g_new(SC_HTMLAttr, 1);
+ attr->name = g_strdup(attr_name);
+ attr->value = g_strdup(attr_value);
+ tag->attr = g_list_append(tag->attr, attr);
+ }
+
+ return tag;
+}
+
+static void sc_html_free_tag(SC_HTMLTag *tag)
+{
+ if (!tag) return;
+
+ g_free(tag->name);
+ while (tag->attr != NULL) {
+ SC_HTMLAttr *attr = (SC_HTMLAttr *)tag->attr->data;
+ g_free(attr->name);
+ g_free(attr->value);
+ g_free(attr);
+ tag->attr = g_list_remove(tag->attr, tag->attr->data);
}
+ g_free(tag);
+}
- parser->state = HTML_UNKNOWN;
- if (buf[0] == '\0') return parser->state;
+static SC_HTMLState sc_html_parse_tag(SC_HTMLParser *parser)
+{
+ gchar buf[SC_HTMLBUFSIZE];
+ SC_HTMLTag *tag;
- g_strdown(buf);
+ sc_html_get_parenthesis(parser, buf, sizeof(buf));
- if (!strcmp(buf, "br")) {
+ tag = sc_html_get_tag(buf);
+
+ parser->state = SC_HTML_UNKNOWN;
+ if (!tag) return SC_HTML_UNKNOWN;
+
+ if (!strcmp(tag->name, "br")) {
parser->space = FALSE;
- html_append_char(parser, '\n');
- parser->state = HTML_BR;
- } else if (!strcmp(buf, "a")) {
- /* look for tokens separated by space or = */
- char* href_token = strtok(++p, " =");
- parser->state = HTML_NORMAL;
- while (href_token != NULL) {
- /* look for href */
- if (!strcmp(href_token, "href")) {
- /* the next token is the url, between double
- * quotes */
- char* url = strtok(NULL, "\"");
- html_append_str(parser, url, strlen(url));
- html_append_char(parser, ' ');
- /* start enforcing html link */
- parser->state = HTML_HREF;
- is_in_href = TRUE;
+ sc_html_append_char(parser, '\n');
+ parser->state = SC_HTML_BR;
+ } else if (!strcmp(tag->name, "a")) {
+ GList *cur;
+ for (cur = tag->attr; cur != NULL; cur = cur->next) {
+ if (cur->data && !strcmp(((SC_HTMLAttr *)cur->data)->name, "href")) {
+ g_free(parser->href);
+ parser->href = g_strdup(((SC_HTMLAttr *)cur->data)->value);
+ parser->state = SC_HTML_HREF_BEG;
break;
}
- /* or get next token */
- href_token = strtok(NULL, " =");
}
- } else if (!strcmp(buf, "/a")) {
- /* stop enforcing html link */
- parser->state = HTML_NORMAL;
- is_in_href = FALSE;
- } else if (!strcmp(buf, "p")) {
+ } else if (!strcmp(tag->name, "/a")) {
+ parser->state = SC_HTML_HREF;
+ } else if (!strcmp(tag->name, "p")) {
parser->space = FALSE;
if (!parser->empty_line) {
parser->space = FALSE;
- if (!parser->newline) html_append_char(parser, '\n');
- html_append_char(parser, '\n');
+ if (!parser->newline) sc_html_append_char(parser, '\n');
+ sc_html_append_char(parser, '\n');
}
- parser->state = HTML_PAR;
- } else if (!strcmp(buf, "pre")) {
+ parser->state = SC_HTML_PAR;
+ } else if (!strcmp(tag->name, "pre")) {
parser->pre = TRUE;
- parser->state = HTML_PRE;
- } else if (!strcmp(buf, "/pre")) {
+ parser->state = SC_HTML_PRE;
+ } else if (!strcmp(tag->name, "/pre")) {
parser->pre = FALSE;
- parser->state = HTML_NORMAL;
- } else if (!strcmp(buf, "hr")) {
+ parser->state = SC_HTML_NORMAL;
+ } else if (!strcmp(tag->name, "hr")) {
if (!parser->newline) {
parser->space = FALSE;
- html_append_char(parser, '\n');
+ sc_html_append_char(parser, '\n');
}
- html_append_str(parser, HR_STR "\n", -1);
- parser->state = HTML_HR;
- } else if (!strcmp(buf, "div") ||
- !strcmp(buf, "ul") ||
- !strcmp(buf, "li") ||
- !strcmp(buf, "table") ||
- !strcmp(buf, "tr") ||
- (buf[0] == 'h' && isdigit(buf[1]))) {
+ sc_html_append_str(parser, HR_STR "\n", -1);
+ parser->state = SC_HTML_HR;
+ } else if (!strcmp(tag->name, "div") ||
+ !strcmp(tag->name, "ul") ||
+ !strcmp(tag->name, "li") ||
+ !strcmp(tag->name, "table") ||
+ !strcmp(tag->name, "tr") ||
+ (tag->name[0] == 'h' && g_ascii_isdigit(tag->name[1]))) {
if (!parser->newline) {
parser->space = FALSE;
- html_append_char(parser, '\n');
+ sc_html_append_char(parser, '\n');
}
- parser->state = HTML_NORMAL;
- } else if (!strcmp(buf, "/table") ||
- (buf[0] == '/' && buf[1] == 'h' && isdigit(buf[1]))) {
+ parser->state = SC_HTML_NORMAL;
+ } else if (!strcmp(tag->name, "/table") ||
+ (tag->name[0] == '/' &&
+ tag->name[1] == 'h' &&
+ g_ascii_isdigit(tag->name[1]))) {
if (!parser->empty_line) {
parser->space = FALSE;
- if (!parser->newline) html_append_char(parser, '\n');
- html_append_char(parser, '\n');
+ if (!parser->newline) sc_html_append_char(parser, '\n');
+ sc_html_append_char(parser, '\n');
}
- parser->state = HTML_NORMAL;
- } else if (!strcmp(buf, "/div") ||
- !strcmp(buf, "/ul") ||
- !strcmp(buf, "/li")) {
+ parser->state = SC_HTML_NORMAL;
+ } else if (!strcmp(tag->name, "/div") ||
+ !strcmp(tag->name, "/ul") ||
+ !strcmp(tag->name, "/li")) {
if (!parser->newline) {
parser->space = FALSE;
- html_append_char(parser, '\n');
+ sc_html_append_char(parser, '\n');
}
- parser->state = HTML_NORMAL;
- }
-
- if (is_in_href == TRUE) {
- /* when inside a link, everything will be written as
- * clickable (see textview_show_thml in textview.c) */
- parser->state = HTML_HREF;
- }
+ parser->state = SC_HTML_NORMAL;
+ }
+
+ sc_html_free_tag(tag);
return parser->state;
}
-static void html_parse_special(HTMLParser *parser)
+static void sc_html_parse_special(SC_HTMLParser *parser)
{
gchar symbol_name[9];
gint n;
const gchar *val;
- parser->state = HTML_UNKNOWN;
+ parser->state = SC_HTML_UNKNOWN;
g_return_if_fail(*parser->bufp == '&');
/* &foo; */
;
if (n > 7 || parser->bufp[n] != ';') {
/* output literal `&' */
- html_append_char(parser, *parser->bufp++);
- parser->state = HTML_NORMAL;
+ sc_html_append_char(parser, *parser->bufp++);
+ parser->state = SC_HTML_NORMAL;
return;
}
strncpy2(symbol_name, parser->bufp, n + 2);
if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
!= NULL) {
- html_append_str(parser, val, -1);
- parser->state = HTML_NORMAL;
+ sc_html_append_str(parser, val, -1);
+ parser->state = SC_HTML_NORMAL;
return;
- } else if (symbol_name[1] == '#' && isdigit(symbol_name[2])) {
+ } else if (symbol_name[1] == '#' && g_ascii_isdigit(symbol_name[2])) {
gint ch;
ch = atoi(symbol_name + 2);
if ((ch > 0 && ch <= 127) ||
(ch >= 128 && ch <= 255 &&
parser->conv->charset == C_ISO_8859_1)) {
- html_append_char(parser, ch);
- parser->state = HTML_NORMAL;
+ sc_html_append_char(parser, ch);
+ parser->state = SC_HTML_NORMAL;
return;
+ } else {
+ const gchar *symb = g_hash_table_lookup(parser->alt_symbol_table, &ch);
+ if (symb) {
+ sc_html_append_str(parser, symb, -1);
+ parser->state = SC_HTML_NORMAL;
+ return;
+ }
}
}
- html_append_str(parser, symbol_name, -1);
+ sc_html_append_str(parser, symbol_name, -1);
}
-static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len)
+static void sc_html_get_parenthesis(SC_HTMLParser *parser, gchar *buf, gint len)
{
gchar *p;
buf[0] = '\0';
g_return_if_fail(*parser->bufp == '<');
- /* ignore comments */
+ /* ignore comment / CSS / script stuff */
if (!strncmp(parser->bufp, "<!--", 4)) {
parser->bufp += 4;
while ((p = strstr(parser->bufp, "-->")) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
+ if (sc_html_read_line(parser) == SC_HTML_EOF) return;
parser->bufp = p + 3;
return;
}
- /* because html is not strict regarding case and double-quoting of
- tags we have to check for both */
- /* ignore css stuff */
- if (!g_strncasecmp(parser->bufp, "<STYLE type=text/css>", 21)) {
- parser->bufp += 21;
- while ((p = strcasestr(parser->bufp, "</STYLE>")) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
+ if (!g_ascii_strncasecmp(parser->bufp, "<style", 6)) {
+ parser->bufp += 6;
+ while ((p = strcasestr(parser->bufp, "</style>")) == NULL)
+ if (sc_html_read_line(parser) == SC_HTML_EOF) return;
parser->bufp = p + 8;
return;
}
- /* ignore css stuff with double quotes*/
- if (!g_strncasecmp(parser->bufp, "<STYLE type=\"text/css\">", 23)) {
- parser->bufp += 23;
- while ((p = strcasestr(parser->bufp, "</STYLE>")) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
- parser->bufp = p + 8;
- return;
- }
- /* ignore javascipt stuff */
- if (!g_strncasecmp(parser->bufp, "<SCRIPT language=javascript>", 28)) {
- parser->bufp += 28;
- while ((p = strcasestr(parser->bufp, "</SCRIPT>")) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
- parser->bufp = p + 8;
- return;
- }
- /* ignore javascipt stuff with double-quotes */
- if (!g_strncasecmp(parser->bufp, "<SCRIPT language=\"javascript\">", 30)) {
- parser->bufp += 30;
- while ((p = strcasestr(parser->bufp, "</SCRIPT>")) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
- parser->bufp = p + 8;
+ if (!g_ascii_strncasecmp(parser->bufp, "<script", 7)) {
+ parser->bufp += 7;
+ while ((p = strcasestr(parser->bufp, "</script>")) == NULL)
+ if (sc_html_read_line(parser) == SC_HTML_EOF) return;
+ parser->bufp = p + 9;
return;
}
parser->bufp++;
while ((p = strchr(parser->bufp, '>')) == NULL)
- if (html_read_line(parser) == HTML_EOF) return;
+ if (sc_html_read_line(parser) == SC_HTML_EOF) return;
strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
+ g_strstrip(buf);
parser->bufp = p + 1;
}
-
-/* these hash functions were taken from gstring.c in glib */
-#if 0
-static gint g_str_case_equal(gconstpointer v, gconstpointer v2)
-{
- return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
-}
-
-static guint g_str_case_hash(gconstpointer key)
-{
- const gchar *p = key;
- guint h = *p;
-
- if (h) {
- h = tolower(h);
- for (p += 1; *p != '\0'; p++)
- h = (h << 5) - h + tolower(*p);
- }
-
- return h;
-}
-#endif