49a81f6a9c
* src/account.c * src/action.c * src/addr_compl.c * src/addrbook.c * src/addrcache.c * src/addrcindex.c * src/addrclip.c * src/addrduplicates.c * src/addressadd.c * src/addressbook.c * src/addressbook_foldersel.c * src/addrharvest.c * src/addrindex.c * src/addritem.c * src/addrquery.c * src/addrselect.c * src/codeconv.c * src/compose.c * src/customheader.c * src/editaddress.c * src/editaddress_other_attributes_ldap.c * src/editgroup.c * src/editldap.c * src/enriched.c * src/exporthtml.c * src/exportldif.c * src/filtering.c * src/folder.c * src/foldersel.c * src/folderutils.c * src/folderview.c * src/gedit-print.c * src/html.c * src/imap.c * src/imap_gtk.c * src/inc.c * src/jpilot.c * src/ldapctrl.c * src/ldapquery.c * src/ldapserver.c * src/ldapupdate.c * src/ldif.c * src/localfolder.c * src/main.c * src/mainwindow.c * src/matcher.c * src/mbox.c * src/messageview.c * src/mh.c * src/mh_gtk.c * src/mimeview.c * src/msgcache.c * src/mutt.c * src/news.c * src/news_gtk.c * src/noticeview.c * src/pine.c * src/pop.c * src/prefs_account.c * src/prefs_actions.c * src/prefs_common.c * src/prefs_filtering.c * src/prefs_filtering_action.c * src/prefs_folder_column.c * src/prefs_folder_item.c * src/prefs_gtk.c * src/prefs_matcher.c * src/prefs_quote.c * src/prefs_send.c * src/prefs_summaries.c * src/prefs_summary_column.c * src/prefs_template.c * src/prefs_themes.c * src/prefs_toolbar.c * src/printing.c * src/privacy.c * src/procheader.c * src/procmime.c * src/procmsg.c * src/quote_fmt.c * src/remotefolder.c * src/send_message.c * src/sourcewindow.c * src/statusbar.c * src/stock_pixmap.c * src/summary_search.c * src/summaryview.c * src/textview.c * src/toolbar.c * src/undo.c * src/uri_opener.c * src/vcard.c * src/common/base64.c * src/common/claws.c * src/common/hooks.c * src/common/plugin.c * src/common/prefs.c * src/common/session.c * src/common/smtp.c * src/common/socket.c * src/common/string_match.c * src/common/stringtable.c * src/common/utils.c * src/common/utils.h * src/common/xml.c * src/common/xmlprops.c * src/gtk/colorlabel.c * src/gtk/combobox.c * src/gtk/foldersort.c * src/gtk/gtkaspell.c * src/gtk/gtkcmclist.c * src/gtk/gtkcmctree.c * src/gtk/gtkcmoptionmenu.c * src/gtk/gtksctree.c * src/gtk/gtkshruler.c * src/gtk/gtksourceprintjob.c * src/gtk/gtkutils.c * src/gtk/gtkvscrollbutton.c * src/gtk/logwindow.c * src/gtk/menu.c * src/gtk/pluginwindow.c * src/gtk/prefswindow.c * src/gtk/quicksearch.c * src/gtk/spell_entry.c * src/plugins/pgpcore/select-keys.c * src/plugins/pgpinline/pgpinline.c * src/plugins/pgpmime/pgpmime.c * src/plugins/smime/smime.c * src/plugins/spamassassin/spamassassin_gtk.c Mass commit changing g_return_* to cm_return_* and making these more interesting
785 lines
19 KiB
C
785 lines
19 KiB
C
/*
|
|
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
|
|
* Copyright (C) 1999-2009 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
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
#include <glib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#include "html.h"
|
|
#include "codeconv.h"
|
|
#include "utils.h"
|
|
|
|
#define SC_HTMLBUFSIZE 8192
|
|
#define HR_STR "------------------------------------------------"
|
|
|
|
typedef struct _SC_HTMLSymbol SC_HTMLSymbol;
|
|
|
|
struct _SC_HTMLSymbol
|
|
{
|
|
gchar *const key;
|
|
gchar *const val;
|
|
};
|
|
|
|
static SC_HTMLSymbol symbol_list[] = {
|
|
{""", "\42"},
|
|
{"&", "\46"},
|
|
{"'", "\47"},
|
|
{"<", "\74"},
|
|
{">", "\76"},
|
|
{""", "\42"},
|
|
{"&", "\46"},
|
|
{"'", "\47"},
|
|
{"<", "\74"},
|
|
{">", "\76"},
|
|
{"’", "\47"},
|
|
{"™", "\342\204\242"},
|
|
{" ", "\40"},
|
|
{"¡", "\302\241"},
|
|
{"¢", "\302\242"},
|
|
{"£", "\302\243"},
|
|
{"¤", "\302\244"},
|
|
{"¥", "\302\245"},
|
|
{"¦", "\302\246"},
|
|
{"§", "\302\247"},
|
|
{"¨", "\302\250"},
|
|
{"©", "\302\251"},
|
|
{"ª", "\302\252"},
|
|
{"«", "\302\253"},
|
|
{"¬", "\302\254"},
|
|
{"­", "\302\255"},
|
|
{"®", "\302\256"},
|
|
{"¯", "\302\257"},
|
|
{"°", "\302\260"},
|
|
{"±", "\302\261"},
|
|
{"²", "\302\262"},
|
|
{"³", "\302\263"},
|
|
{"´", "\302\264"},
|
|
{"µ", "\302\265"},
|
|
{"¶", "\302\266"},
|
|
{"·", "\302\267"},
|
|
{"¸", "\302\270"},
|
|
{"¹", "\302\271"},
|
|
{"º", "\302\272"},
|
|
{"»", "\302\273"},
|
|
{"¼", "\302\274"},
|
|
{"½", "\302\275"},
|
|
{"¾", "\302\276"},
|
|
{"¿", "\302\277"},
|
|
{"À", "\303\200"},
|
|
{"Á", "\303\201"},
|
|
{"Â", "\303\202"},
|
|
{"Ã", "\303\203"},
|
|
{"Ä", "\303\204"},
|
|
{"Å", "\303\205"},
|
|
{"Æ", "\303\206"},
|
|
{"Ç", "\303\207"},
|
|
{"È", "\303\210"},
|
|
{"É", "\303\211"},
|
|
{"Ê", "\303\212"},
|
|
{"Ë", "\303\213"},
|
|
{"Ì", "\303\214"},
|
|
{"Í", "\303\215"},
|
|
{"Î", "\303\216"},
|
|
{"Ï", "\303\217"},
|
|
{"Ð", "\303\220"},
|
|
{"Ñ", "\303\221"},
|
|
{"Ò", "\303\222"},
|
|
{"Ó", "\303\223"},
|
|
{"Ô", "\303\224"},
|
|
{"Õ", "\303\225"},
|
|
{"Ö", "\303\226"},
|
|
{"×", "\303\227"},
|
|
{"Ø", "\303\230"},
|
|
{"Ù", "\303\231"},
|
|
{"Ú", "\303\232"},
|
|
{"Û", "\303\233"},
|
|
{"Ü", "\303\234"},
|
|
{"Ý", "\303\235"},
|
|
{"Þ", "\303\236"},
|
|
{"ß", "\303\237"},
|
|
{"à", "\303\240"},
|
|
{"á", "\303\241"},
|
|
{"â", "\303\242"},
|
|
{"ã", "\303\243"},
|
|
{"ä", "\303\244"},
|
|
{"å", "\303\245"},
|
|
{"æ", "\303\246"},
|
|
{"ç", "\303\247"},
|
|
{"è", "\303\250"},
|
|
{"é", "\303\251"},
|
|
{"ê", "\303\252"},
|
|
{"ë", "\303\253"},
|
|
{"ì", "\303\254"},
|
|
{"í", "\303\255"},
|
|
{"î", "\303\256"},
|
|
{"ï", "\303\257"},
|
|
{"ð", "\303\260"},
|
|
{"ñ", "\303\261"},
|
|
{"ò", "\303\262"},
|
|
{"ó", "\303\263"},
|
|
{"ô", "\303\264"},
|
|
{"õ", "\303\265"},
|
|
{"ö", "\303\266"},
|
|
{"÷", "\303\267"},
|
|
{"ø", "\303\270"},
|
|
{"ù", "\303\271"},
|
|
{"ú", "\303\272"},
|
|
{"û", "\303\273"},
|
|
{"ü", "\303\274"},
|
|
{"ý", "\303\275"},
|
|
{"þ", "\303\276"},
|
|
{"ÿ", "\303\277"},
|
|
{"Œ", "\305\222"},
|
|
{"œ", "\305\223"},
|
|
{"Š", "\305\240"},
|
|
{"š", "\305\241"},
|
|
{"Ÿ", "\305\270"},
|
|
{"ˆ", "\313\206"},
|
|
{"˜", "\313\234"},
|
|
{" ", "\342\200\202"},
|
|
{" ", "\342\200\203"},
|
|
{" ", "\342\200\211"},
|
|
{"–", "\342\200\223"},
|
|
{"—", "\342\200\224"},
|
|
{"‘", "\342\200\230"},
|
|
{"’", "\342\200\231"},
|
|
{"‚", "\342\200\232"},
|
|
{"“", "\342\200\234"},
|
|
{"”", "\342\200\235"},
|
|
{"„", "\342\200\236"},
|
|
{"†", "\342\200\240"},
|
|
{"‡", "\342\200\241"},
|
|
{"•", "\342\200\242"},
|
|
{"…", "\342\200\246"},
|
|
{"‰", "\342\200\260"},
|
|
{"‹", "\342\200\271"},
|
|
{"›", "\342\200\272"},
|
|
{"€", "\342\202\254"},
|
|
{"™", "\342\204\242"},
|
|
{""", "\42"},
|
|
{"&", "\46"},
|
|
{"'", "\47"},
|
|
{"<", "\74"},
|
|
{">", "\76"},
|
|
{"&squot;", "\47"},
|
|
{" ", "\40"},
|
|
{"¡", "\302\241"},
|
|
{"¢", "\302\242"},
|
|
{"£", "\302\243"},
|
|
{"¤", "\302\244"},
|
|
{"¥", "\302\245"},
|
|
{"¦", "\302\246"},
|
|
{"§", "\302\247"},
|
|
{"¨", "\302\250"},
|
|
{"©", "\302\251"},
|
|
{"ª", "\302\252"},
|
|
{"«", "\302\253"},
|
|
{"¬", "\302\254"},
|
|
{"­", "\302\255"},
|
|
{"®", "\302\256"},
|
|
{"¯", "\302\257"},
|
|
{"°", "\302\260"},
|
|
{"±", "\302\261"},
|
|
{"²", "\302\262"},
|
|
{"³", "\302\263"},
|
|
{"´", "\302\264"},
|
|
{"µ", "\302\265"},
|
|
{"¶", "\302\266"},
|
|
{"·", "\302\267"},
|
|
{"¸", "\302\270"},
|
|
{"¹", "\302\271"},
|
|
{"º", "\302\272"},
|
|
{"»", "\302\273"},
|
|
{"¼", "\302\274"},
|
|
{"½", "\302\275"},
|
|
{"¾", "\302\276"},
|
|
{"¿", "\302\277"},
|
|
{"À", "\303\200"},
|
|
{"Á", "\303\201"},
|
|
{"Â", "\303\202"},
|
|
{"Ã", "\303\203"},
|
|
{"Ä", "\303\204"},
|
|
{"Å", "\303\205"},
|
|
{"Æ", "\303\206"},
|
|
{"Ç", "\303\207"},
|
|
{"È", "\303\210"},
|
|
{"É", "\303\211"},
|
|
{"Ê", "\303\212"},
|
|
{"Ë", "\303\213"},
|
|
{"Ì", "\303\214"},
|
|
{"Í", "\303\215"},
|
|
{"Î", "\303\216"},
|
|
{"Ï", "\303\217"},
|
|
{"Ð", "\303\220"},
|
|
{"Ñ", "\303\221"},
|
|
{"Ò", "\303\222"},
|
|
{"Ó", "\303\223"},
|
|
{"Ô", "\303\224"},
|
|
{"Õ", "\303\225"},
|
|
{"Ö", "\303\226"},
|
|
{"×", "\303\227"},
|
|
{"Ø", "\303\230"},
|
|
{"Ù", "\303\231"},
|
|
{"Ú", "\303\232"},
|
|
{"Û", "\303\233"},
|
|
{"Ü", "\303\234"},
|
|
{"Ý", "\303\235"},
|
|
{"Þ", "\303\236"},
|
|
{"ß", "\303\237"},
|
|
{"à", "\303\240"},
|
|
{"á", "\303\241"},
|
|
{"â", "\303\242"},
|
|
{"ã", "\303\243"},
|
|
{"ä", "\303\244"},
|
|
{"å", "\303\245"},
|
|
{"æ", "\303\246"},
|
|
{"ç", "\303\247"},
|
|
{"è", "\303\250"},
|
|
{"é", "\303\251"},
|
|
{"ê", "\303\252"},
|
|
{"ë", "\303\253"},
|
|
{"ì", "\303\254"},
|
|
{"í", "\303\255"},
|
|
{"î", "\303\256"},
|
|
{"ï", "\303\257"},
|
|
{"ð", "\303\260"},
|
|
{"ñ", "\303\261"},
|
|
{"ò", "\303\262"},
|
|
{"ó", "\303\263"},
|
|
{"ô", "\303\264"},
|
|
{"õ", "\303\265"},
|
|
{"ö", "\303\266"},
|
|
{"÷", "\303\267"},
|
|
{"ø", "\303\270"},
|
|
{"ù", "\303\271"},
|
|
{"ú", "\303\272"},
|
|
{"û", "\303\273"},
|
|
{"ü", "\303\274"},
|
|
{"ý", "\303\275"},
|
|
{"þ", "\303\276"},
|
|
{"ÿ", "\303\277"},
|
|
{"Œ", "\305\222"},
|
|
{"œ", "\305\223"},
|
|
{"Š", "\305\240"},
|
|
{"š", "\305\241"},
|
|
{"Ÿ", "\305\270"},
|
|
{"ˆ", "\313\206"},
|
|
{"˜", "\313\234"},
|
|
{" ", "\342\200\202"},
|
|
{" ", "\342\200\203"},
|
|
{" ", "\342\200\211"},
|
|
{"–", "\342\200\223"},
|
|
{"—", "\342\200\224"},
|
|
{"‘", "\342\200\230"},
|
|
{"’", "\342\200\231"},
|
|
{"‚", "\342\200\232"},
|
|
{"“", "\342\200\234"},
|
|
{"”", "\342\200\235"},
|
|
{"„", "\342\200\236"},
|
|
{"†", "\342\200\240"},
|
|
{"‡", "\342\200\241"},
|
|
{"•", "\342\200\242"},
|
|
{"…", "\342\200\246"},
|
|
{"‰", "\342\200\260"},
|
|
{"‹", "\342\200\271"},
|
|
{"›", "\342\200\272"},
|
|
{"€", "\342\202\254"},
|
|
{"™", "\342\204\242"}
|
|
};
|
|
|
|
typedef struct _SC_HTMLAltSymbol SC_HTMLAltSymbol;
|
|
|
|
struct _SC_HTMLAltSymbol
|
|
{
|
|
gint key;
|
|
gchar *const val;
|
|
};
|
|
|
|
static GHashTable *default_symbol_table;
|
|
|
|
static SC_HTMLState sc_html_read_line (SC_HTMLParser *parser);
|
|
static void sc_html_append_char (SC_HTMLParser *parser,
|
|
gchar ch);
|
|
static void sc_html_append_str (SC_HTMLParser *parser,
|
|
const gchar *str,
|
|
gint len);
|
|
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);
|
|
|
|
|
|
SC_HTMLParser *sc_html_parser_new(FILE *fp, CodeConverter *conv)
|
|
{
|
|
SC_HTMLParser *parser;
|
|
|
|
cm_return_val_if_fail(fp != NULL, NULL);
|
|
cm_return_val_if_fail(conv != NULL, NULL);
|
|
|
|
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;
|
|
parser->pre = FALSE;
|
|
|
|
#define SYMBOL_TABLE_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); \
|
|
}
|
|
#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 =
|
|
g_hash_table_new(g_str_hash, g_str_equal);
|
|
SYMBOL_TABLE_ADD(default_symbol_table, symbol_list);
|
|
}
|
|
|
|
#undef SYMBOL_TABLE_ADD
|
|
#undef SYMBOL_TABLE_REF_ADD
|
|
|
|
parser->symbol_table = default_symbol_table;
|
|
|
|
return 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 *sc_html_parse(SC_HTMLParser *parser)
|
|
{
|
|
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 (sc_html_read_line(parser) == SC_HTML_EOF)
|
|
return NULL;
|
|
}
|
|
|
|
while (*parser->bufp != '\0') {
|
|
switch (*parser->bufp) {
|
|
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 '&':
|
|
sc_html_parse_special(parser);
|
|
break;
|
|
case ' ':
|
|
case '\t':
|
|
case '\r':
|
|
case '\n':
|
|
if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
|
|
parser->bufp++;
|
|
|
|
if (!parser->pre) {
|
|
if (!parser->newline)
|
|
parser->space = TRUE;
|
|
|
|
parser->bufp++;
|
|
break;
|
|
}
|
|
/* fallthrough */
|
|
default:
|
|
sc_html_append_char(parser, *parser->bufp++);
|
|
}
|
|
}
|
|
|
|
return parser->str->str;
|
|
}
|
|
|
|
static SC_HTMLState sc_html_read_line(SC_HTMLParser *parser)
|
|
{
|
|
gchar buf[SC_HTMLBUFSIZE];
|
|
gchar buf2[SC_HTMLBUFSIZE];
|
|
gint index;
|
|
|
|
if (parser->fp == NULL)
|
|
return SC_HTML_EOF;
|
|
|
|
if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
|
|
parser->state = SC_HTML_EOF;
|
|
return SC_HTML_EOF;
|
|
}
|
|
|
|
if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
|
|
index = parser->bufp - parser->buf->str;
|
|
|
|
conv_utf8todisp(buf2, sizeof(buf2), buf);
|
|
g_string_append(parser->buf, buf2);
|
|
|
|
parser->bufp = parser->buf->str + index;
|
|
|
|
return SC_HTML_CONV_FAILED;
|
|
}
|
|
|
|
index = parser->bufp - parser->buf->str;
|
|
|
|
g_string_append(parser->buf, buf2);
|
|
|
|
parser->bufp = parser->buf->str + index;
|
|
|
|
return SC_HTML_NORMAL;
|
|
}
|
|
|
|
static void sc_html_append_char(SC_HTMLParser *parser, gchar ch)
|
|
{
|
|
GString *str = parser->str;
|
|
|
|
if (!parser->pre && parser->space) {
|
|
g_string_append_c(str, ' ');
|
|
parser->space = FALSE;
|
|
}
|
|
|
|
g_string_append_c(str, ch);
|
|
|
|
parser->empty_line = FALSE;
|
|
if (ch == '\n') {
|
|
parser->newline = TRUE;
|
|
if (str->len > 1 && str->str[str->len - 2] == '\n')
|
|
parser->empty_line = TRUE;
|
|
} else
|
|
parser->newline = FALSE;
|
|
}
|
|
|
|
static void sc_html_append_str(SC_HTMLParser *parser, const gchar *str, gint len)
|
|
{
|
|
GString *string = parser->str;
|
|
|
|
if (!parser->pre && parser->space) {
|
|
g_string_append_c(string, ' ');
|
|
parser->space = FALSE;
|
|
}
|
|
|
|
if (len == 0) return;
|
|
if (len < 0)
|
|
g_string_append(string, str);
|
|
else {
|
|
gchar *s;
|
|
Xstrndup_a(s, str, len, return);
|
|
g_string_append(string, s);
|
|
}
|
|
|
|
parser->empty_line = FALSE;
|
|
if (string->len > 0 && string->str[string->len - 1] == '\n') {
|
|
parser->newline = TRUE;
|
|
if (string->len > 1 && string->str[string->len - 2] == '\n')
|
|
parser->empty_line = TRUE;
|
|
} else
|
|
parser->newline = FALSE;
|
|
}
|
|
|
|
static SC_HTMLTag *sc_html_get_tag(const gchar *str)
|
|
{
|
|
SC_HTMLTag *tag;
|
|
gchar *tmp;
|
|
guchar *tmpp;
|
|
|
|
cm_return_val_if_fail(str != NULL, NULL);
|
|
|
|
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') {
|
|
tag->name = g_utf8_strdown(tmp, -1);
|
|
return tag;
|
|
} else {
|
|
*tmpp++ = '\0';
|
|
tag->name = g_utf8_strdown(tmp, -1);
|
|
}
|
|
|
|
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);
|
|
attr = g_new(SC_HTMLAttr, 1);
|
|
attr->name = g_utf8_strdown(attr_name, -1);
|
|
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);
|
|
}
|
|
|
|
static void decode_href(SC_HTMLParser *parser)
|
|
{
|
|
gchar *tmp;
|
|
SC_HTMLParser *tparser = g_new0(SC_HTMLParser, 1);
|
|
|
|
tparser->str = g_string_new(NULL);
|
|
tparser->buf = g_string_new(parser->href);
|
|
tparser->bufp = tparser->buf->str;
|
|
tparser->symbol_table = default_symbol_table;
|
|
|
|
tmp = sc_html_parse(tparser);
|
|
|
|
g_free(parser->href);
|
|
parser->href = g_strdup(tmp);
|
|
|
|
sc_html_parser_destroy(tparser);
|
|
}
|
|
|
|
static SC_HTMLState sc_html_parse_tag(SC_HTMLParser *parser)
|
|
{
|
|
gchar buf[SC_HTMLBUFSIZE];
|
|
SC_HTMLTag *tag;
|
|
|
|
sc_html_get_parenthesis(parser, buf, sizeof(buf));
|
|
|
|
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;
|
|
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);
|
|
decode_href(parser);
|
|
parser->state = SC_HTML_HREF_BEG;
|
|
break;
|
|
}
|
|
}
|
|
} 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) sc_html_append_char(parser, '\n');
|
|
sc_html_append_char(parser, '\n');
|
|
}
|
|
parser->state = SC_HTML_PAR;
|
|
} else if (!strcmp(tag->name, "pre")) {
|
|
parser->pre = TRUE;
|
|
parser->state = SC_HTML_PRE;
|
|
} else if (!strcmp(tag->name, "/pre")) {
|
|
parser->pre = FALSE;
|
|
parser->state = SC_HTML_NORMAL;
|
|
} else if (!strcmp(tag->name, "hr")) {
|
|
if (!parser->newline) {
|
|
parser->space = FALSE;
|
|
sc_html_append_char(parser, '\n');
|
|
}
|
|
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;
|
|
sc_html_append_char(parser, '\n');
|
|
}
|
|
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) sc_html_append_char(parser, '\n');
|
|
sc_html_append_char(parser, '\n');
|
|
}
|
|
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;
|
|
sc_html_append_char(parser, '\n');
|
|
}
|
|
parser->state = SC_HTML_NORMAL;
|
|
}
|
|
|
|
sc_html_free_tag(tag);
|
|
|
|
return parser->state;
|
|
}
|
|
|
|
static void sc_html_parse_special(SC_HTMLParser *parser)
|
|
{
|
|
gchar symbol_name[9];
|
|
gint n;
|
|
const gchar *val;
|
|
|
|
parser->state = SC_HTML_UNKNOWN;
|
|
cm_return_if_fail(*parser->bufp == '&');
|
|
|
|
/* &foo; */
|
|
for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++)
|
|
;
|
|
if (n > 7 || parser->bufp[n] != ';') {
|
|
/* output literal `&' */
|
|
sc_html_append_char(parser, *parser->bufp++);
|
|
parser->state = SC_HTML_NORMAL;
|
|
return;
|
|
}
|
|
strncpy2(symbol_name, parser->bufp, n + 2);
|
|
parser->bufp += n + 1;
|
|
|
|
if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name))
|
|
!= NULL) {
|
|
sc_html_append_str(parser, val, -1);
|
|
parser->state = SC_HTML_NORMAL;
|
|
return;
|
|
}
|
|
|
|
sc_html_append_str(parser, symbol_name, -1);
|
|
}
|
|
|
|
static void sc_html_get_parenthesis(SC_HTMLParser *parser, gchar *buf, gint len)
|
|
{
|
|
gchar *p;
|
|
|
|
buf[0] = '\0';
|
|
cm_return_if_fail(*parser->bufp == '<');
|
|
|
|
/* ignore comment / CSS / script stuff */
|
|
if (!strncmp(parser->bufp, "<!--", 4)) {
|
|
parser->bufp += 4;
|
|
while ((p = strstr(parser->bufp, "-->")) == NULL)
|
|
if (sc_html_read_line(parser) == SC_HTML_EOF) return;
|
|
parser->bufp = p + 3;
|
|
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;
|
|
}
|
|
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 (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;
|
|
}
|