claws-mail/src/html.c
Paul Mangan 9e08c2369a 2012-05-27 [paul] 3.8.0cvs48
* src/account.c
	* src/account.h
	* src/action.c
	* src/action.h
	* src/adbookbase.h
	* src/addr_compl.c
	* src/addr_compl.h
	* src/addrbook.c
	* src/addrcache.c
	* src/addrcache.h
	* src/addrclip.c
	* src/addrclip.h
	* src/addrcustomattr.c
	* src/addrcustomattr.h
	* src/addrdefs.h
	* src/addrduplicates.c
	* src/addrduplicates.h
	* src/addressadd.c
	* src/addressadd.h
	* src/addressbook-dbus.c
	* src/addressbook-dbus.h
	* src/addressbook.c
	* src/addressbook.h
	* src/addressbook_foldersel.c
	* src/addressbook_foldersel.h
	* src/addressitem.h
	* src/addrgather.c
	* src/addrgather.h
	* src/addrharvest.c
	* src/addrharvest.h
	* src/addrindex.c
	* src/addrindex.h
	* src/addritem.c
	* src/addritem.h
	* src/addrquery.c
	* src/addrquery.h
	* src/addrselect.c
	* src/addrselect.h
	* src/alertpanel.c
	* src/alertpanel.h
	* src/autofaces.c
	* src/autofaces.h
	* src/browseldap.c
	* src/browseldap.h
	* src/codeconv.c
	* src/codeconv.h
	* src/compose.c
	* src/compose.h
	* src/crash.c
	* src/crash.h
	* src/customheader.c
	* src/customheader.h
	* src/displayheader.c
	* src/displayheader.h
	* src/editaddress.c
	* src/editaddress.h
	* src/editaddress_other_attributes_ldap.c
	* src/editaddress_other_attributes_ldap.h
	* src/editbook.c
	* src/editbook.h
	* src/editgroup.c
	* src/editgroup.h
	* src/editjpilot.c
	* src/editjpilot.h
	* src/editldap.c
	* src/editldap.h
	* src/editldap_basedn.c
	* src/editldap_basedn.h
	* src/edittags.c
	* src/edittags.h
	* src/editvcard.c
	* src/editvcard.h
	* src/exphtmldlg.c
	* src/exphtmldlg.h
	* src/expldifdlg.c
	* src/expldifdlg.h
	* src/export.c
	* src/export.h
	* src/exporthtml.c
	* src/exporthtml.h
	* src/exportldif.c
	* src/exportldif.h
	* src/filtering.c
	* src/filtering.h
	* src/folder.c
	* src/folder.h
	* src/folder_item_prefs.c
	* src/folder_item_prefs.h
	* src/foldersel.c
	* src/foldersel.h
	* src/folderutils.c
	* src/folderutils.h
	* src/folderview.c
	* src/folderview.h
	* src/grouplistdialog.c
	* src/grouplistdialog.h
	* src/headerview.c
	* src/headerview.h
	* src/html.c
	* src/html.h
	* src/image_viewer.c
	* src/image_viewer.h
	* src/imap.c
	* src/imap.h
	* src/imap_gtk.c
	* src/imap_gtk.h
	* src/import.c
	* src/import.h
	* src/importldif.c
	* src/importldif.h
	* src/importmutt.c
	* src/importmutt.h
	* src/importpine.c
	* src/importpine.h
	* src/inc.c
	* src/inc.h
	* src/jpilot.c
	* src/jpilot.h
	* src/ldapctrl.c
	* src/ldapctrl.h
	* src/ldaplocate.c
	* src/ldaplocate.h
	* src/ldapquery.c
	* src/ldapquery.h
	* src/ldapserver.c
	* src/ldapserver.h
	* src/ldapupdate.c
	* src/ldapupdate.h
	* src/ldaputil.c
	* src/ldaputil.h
	* src/ldif.c
	* src/ldif.h
	* src/localfolder.c
	* src/localfolder.h
	* src/main.c
	* src/main.h
	* src/mainwindow.c
	* src/mainwindow.h
	* src/manual.c
	* src/manual.h
	* src/matcher.c
	* src/matcher.h
	* src/mbox.c
	* src/mbox.h
	* src/message_search.c
	* src/message_search.h
	* src/messageview.c
	* src/messageview.h
	* src/mh.c
	* src/mh.h
	* src/mh_gtk.c
	* src/mh_gtk.h
	* src/mimeview.c
	* src/mimeview.h
	* src/msgcache.c
	* src/msgcache.h
	* src/mutt.c
	* src/mutt.h
	* src/news.c
	* src/news.h
	* src/news_gtk.c
	* src/news_gtk.h
	* src/noticeview.c
	* src/noticeview.h
	* src/partial_download.c
	* src/partial_download.h
	* src/pine.c
	* src/pine.h
	* src/pop.c
	* src/pop.h
	* src/prefs_account.c
	* src/prefs_account.h
	* src/prefs_actions.c
	* src/prefs_actions.h
	* src/prefs_common.c
	* src/prefs_common.h
	* src/prefs_compose_writing.c
	* src/prefs_compose_writing.h
	* src/prefs_customheader.c
	* src/prefs_customheader.h
	* src/prefs_display_header.c
	* src/prefs_display_header.h
	* src/prefs_ext_prog.c
	* src/prefs_ext_prog.h
	* src/prefs_filtering.c
	* src/prefs_filtering.h
	* src/prefs_filtering_action.c
	* src/prefs_filtering_action.h
	* src/prefs_folder_column.c
	* src/prefs_folder_column.h
	* src/prefs_folder_item.c
	* src/prefs_folder_item.h
	* src/prefs_fonts.c
	* src/prefs_fonts.h
	* src/prefs_gtk.c
	* src/prefs_gtk.h
	* src/prefs_image_viewer.c
	* src/prefs_image_viewer.h
	* src/prefs_logging.c
	* src/prefs_logging.h
	* src/prefs_matcher.c
	* src/prefs_matcher.h
	* src/prefs_message.c
	* src/prefs_message.h
	* src/prefs_msg_colors.c
	* src/prefs_msg_colors.h
	* src/prefs_other.c
	* src/prefs_other.h
	* src/prefs_quote.c
	* src/prefs_quote.h
	* src/prefs_receive.c
	* src/prefs_receive.h
	* src/prefs_send.c
	* src/prefs_send.h
	* src/prefs_spelling.c
	* src/prefs_spelling.h
	* src/prefs_summaries.c
	* src/prefs_summaries.h
	* src/prefs_summary_column.c
	* src/prefs_summary_column.h
	* src/prefs_summary_open.c
	* src/prefs_summary_open.h
	* src/prefs_template.c
	* src/prefs_template.h
	* src/prefs_themes.c
	* src/prefs_themes.h
	* src/prefs_toolbar.c
	* src/prefs_toolbar.h
	* src/prefs_wrapping.c
	* src/prefs_wrapping.h
	* src/printing.c
	* src/printing.h
	* src/privacy.c
	* src/privacy.h
	* src/procheader.c
	* src/procheader.h
	* src/procmime.c
	* src/procmime.h
	* src/procmsg.c
	* src/procmsg.h
	* src/quote_fmt.c
	* src/recv.c
	* src/recv.h
	* src/remotefolder.c
	* src/remotefolder.h
	* src/send_message.c
	* src/send_message.h
	* src/setup.c
	* src/setup.h
	* src/sourcewindow.c
	* src/sourcewindow.h
	* src/ssl_manager.c
	* src/ssl_manager.h
	* src/statusbar.c
	* src/statusbar.h
	* src/stock_pixmap.c
	* src/stock_pixmap.h
	* src/summary_search.c
	* src/summary_search.h
	* src/summaryview.c
	* src/summaryview.h
	* src/textview.c
	* src/textview.h
	* src/toolbar.c
	* src/toolbar.h
	* src/undo.c
	* src/undo.h
	* src/unmime.c
	* src/unmime.h
	* src/uri_opener.c
	* src/uri_opener.h
	* src/vcard.c
	* src/vcard.h
	* src/wizard.c
	* src/wizard.h
	* src/common/base64.c
	* src/common/base64.h
	* src/common/claws.c
	* src/common/defs.h
	* src/common/hooks.c
	* src/common/hooks.h
	* src/common/log.c
	* src/common/log.h
	* src/common/mgutils.c
	* src/common/mgutils.h
	* src/common/passcrypt.c
	* src/common/plugin.c
	* src/common/plugin.h
	* src/common/prefs.c
	* src/common/prefs.h
	* src/common/progressindicator.c
	* src/common/progressindicator.h
	* src/common/quoted-printable.c
	* src/common/quoted-printable.h
	* src/common/session.c
	* src/common/session.h
	* src/common/smtp.c
	* src/common/smtp.h
	* src/common/socket.c
	* src/common/socket.h
	* src/common/ssl.c
	* src/common/ssl.h
	* src/common/ssl_certificate.c
	* src/common/ssl_certificate.h
	* src/common/stringtable.c
	* src/common/stringtable.h
	* src/common/tags.c
	* src/common/tags.h
	* src/common/template.c
	* src/common/template.h
	* src/common/timing.h
	* src/common/utils.c
	* src/common/utils.h
	* src/common/version.h.in
	* src/common/xml.c
	* src/common/xml.h
	* src/common/xmlprops.c
	* src/common/xmlprops.h
	* src/etpan/etpan-errors.h
	* src/etpan/etpan-thread-manager-types.h
	* src/etpan/etpan-thread-manager.c
	* src/etpan/etpan-thread-manager.h
	* src/etpan/imap-thread.c
	* src/etpan/imap-thread.h
	* src/etpan/nntp-thread.c
	* src/etpan/nntp-thread.h
	* src/gtk/about.c
	* src/gtk/about.h
	* src/gtk/colorlabel.c
	* src/gtk/colorlabel.h
	* src/gtk/colorsel.c
	* src/gtk/colorsel.h
	* src/gtk/combobox.c
	* src/gtk/combobox.h
	* src/gtk/description_window.c
	* src/gtk/description_window.h
	* src/gtk/filesel.c
	* src/gtk/filesel.h
	* src/gtk/foldersort.c
	* src/gtk/foldersort.h
	* src/gtk/gtksctree.c
	* src/gtk/gtkutils.c
	* src/gtk/gtkutils.h
	* src/gtk/icon_legend.c
	* src/gtk/icon_legend.h
	* src/gtk/inputdialog.c
	* src/gtk/inputdialog.h
	* src/gtk/logwindow.c
	* src/gtk/logwindow.h
	* src/gtk/manage_window.c
	* src/gtk/manage_window.h
	* src/gtk/menu.c
	* src/gtk/menu.h
	* src/gtk/pluginwindow.c
	* src/gtk/pluginwindow.h
	* src/gtk/prefswindow.c
	* src/gtk/prefswindow.h
	* src/gtk/progressdialog.c
	* src/gtk/progressdialog.h
	* src/gtk/quicksearch.c
	* src/gtk/quicksearch.h
	* src/gtk/spell_entry.c
	* src/gtk/spell_entry.h
	* src/gtk/sslcertwindow.c
	* src/gtk/sslcertwindow.h
	* src/plugins/bogofilter/bogofilter.c
	* src/plugins/bogofilter/bogofilter.h
	* src/plugins/bogofilter/bogofilter_gtk.c
	* src/plugins/demo/demo.c
	* src/plugins/dillo_viewer/dillo_prefs.c
	* src/plugins/dillo_viewer/dillo_prefs.h
	* src/plugins/dillo_viewer/dillo_viewer.c
	* src/plugins/pgpcore/passphrase.c
	* src/plugins/pgpcore/passphrase.h
	* src/plugins/pgpcore/pgp_viewer.c
	* src/plugins/pgpcore/pgp_viewer.h
	* src/plugins/pgpcore/plugin.c
	* src/plugins/pgpcore/prefs_gpg.c
	* src/plugins/pgpcore/prefs_gpg.h
	* src/plugins/pgpcore/select-keys.c
	* src/plugins/pgpcore/select-keys.h
	* src/plugins/pgpcore/sgpgme.c
	* src/plugins/pgpcore/sgpgme.h
	* src/plugins/pgpinline/pgpinline.c
	* src/plugins/pgpinline/pgpinline.h
	* src/plugins/pgpinline/plugin.c
	* src/plugins/pgpmime/pgpmime.c
	* src/plugins/pgpmime/pgpmime.h
	* src/plugins/pgpmime/plugin.c
	* src/plugins/smime/plugin.c
	* src/plugins/smime/smime.c
	* src/plugins/smime/smime.h
	* src/plugins/spamassassin/spamassassin.c
	* src/plugins/spamassassin/spamassassin.h
	* src/plugins/spamassassin/spamassassin_gtk.c
	* src/plugins/trayicon/trayicon.c
	* src/plugins/trayicon/trayicon_prefs.c
	* src/plugins/trayicon/trayicon_prefs.h
		update copyright headers to 2012
2012-05-27 17:31:10 +00:00

785 lines
19 KiB
C

/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 1999-2012 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[] = {
{"&#34;", "\42"},
{"&#38;", "\46"},
{"&#39;", "\47"},
{"&#60;", "\74"},
{"&#62;", "\76"},
{"&#034;", "\42"},
{"&#038;", "\46"},
{"&#039;", "\47"},
{"&#060;", "\74"},
{"&#062;", "\76"},
{"&#146;", "\47"},
{"&#153;", "\342\204\242"},
{"&#160;", "\40"},
{"&#161;", "\302\241"},
{"&#162;", "\302\242"},
{"&#163;", "\302\243"},
{"&#164;", "\302\244"},
{"&#165;", "\302\245"},
{"&#166;", "\302\246"},
{"&#167;", "\302\247"},
{"&#168;", "\302\250"},
{"&#169;", "\302\251"},
{"&#170;", "\302\252"},
{"&#171;", "\302\253"},
{"&#172;", "\302\254"},
{"&#173;", "\302\255"},
{"&#174;", "\302\256"},
{"&#175;", "\302\257"},
{"&#176;", "\302\260"},
{"&#177;", "\302\261"},
{"&#178;", "\302\262"},
{"&#179;", "\302\263"},
{"&#180;", "\302\264"},
{"&#181;", "\302\265"},
{"&#182;", "\302\266"},
{"&#183;", "\302\267"},
{"&#184;", "\302\270"},
{"&#185;", "\302\271"},
{"&#186;", "\302\272"},
{"&#187;", "\302\273"},
{"&#188;", "\302\274"},
{"&#189;", "\302\275"},
{"&#190;", "\302\276"},
{"&#191;", "\302\277"},
{"&#192;", "\303\200"},
{"&#193;", "\303\201"},
{"&#194;", "\303\202"},
{"&#195;", "\303\203"},
{"&#196;", "\303\204"},
{"&#197;", "\303\205"},
{"&#198;", "\303\206"},
{"&#199;", "\303\207"},
{"&#200;", "\303\210"},
{"&#201;", "\303\211"},
{"&#202;", "\303\212"},
{"&#203;", "\303\213"},
{"&#204;", "\303\214"},
{"&#205;", "\303\215"},
{"&#206;", "\303\216"},
{"&#207;", "\303\217"},
{"&#208;", "\303\220"},
{"&#209;", "\303\221"},
{"&#210;", "\303\222"},
{"&#211;", "\303\223"},
{"&#212;", "\303\224"},
{"&#213;", "\303\225"},
{"&#214;", "\303\226"},
{"&#215;", "\303\227"},
{"&#216;", "\303\230"},
{"&#217;", "\303\231"},
{"&#218;", "\303\232"},
{"&#219;", "\303\233"},
{"&#220;", "\303\234"},
{"&#221;", "\303\235"},
{"&#222;", "\303\236"},
{"&#223;", "\303\237"},
{"&#224;", "\303\240"},
{"&#225;", "\303\241"},
{"&#226;", "\303\242"},
{"&#227;", "\303\243"},
{"&#228;", "\303\244"},
{"&#229;", "\303\245"},
{"&#230;", "\303\246"},
{"&#231;", "\303\247"},
{"&#232;", "\303\250"},
{"&#233;", "\303\251"},
{"&#234;", "\303\252"},
{"&#235;", "\303\253"},
{"&#236;", "\303\254"},
{"&#237;", "\303\255"},
{"&#238;", "\303\256"},
{"&#239;", "\303\257"},
{"&#240;", "\303\260"},
{"&#241;", "\303\261"},
{"&#242;", "\303\262"},
{"&#243;", "\303\263"},
{"&#244;", "\303\264"},
{"&#245;", "\303\265"},
{"&#246;", "\303\266"},
{"&#247;", "\303\267"},
{"&#248;", "\303\270"},
{"&#249;", "\303\271"},
{"&#250;", "\303\272"},
{"&#251;", "\303\273"},
{"&#252;", "\303\274"},
{"&#253;", "\303\275"},
{"&#254;", "\303\276"},
{"&#255;", "\303\277"},
{"&#338;", "\305\222"},
{"&#339;", "\305\223"},
{"&#352;", "\305\240"},
{"&#353;", "\305\241"},
{"&#376;", "\305\270"},
{"&#710;", "\313\206"},
{"&#732;", "\313\234"},
{"&#8194;", "\342\200\202"},
{"&#8195;", "\342\200\203"},
{"&#8201;", "\342\200\211"},
{"&#8211;", "\342\200\223"},
{"&#8212;", "\342\200\224"},
{"&#8216;", "\342\200\230"},
{"&#8217;", "\342\200\231"},
{"&#8218;", "\342\200\232"},
{"&#8220;", "\342\200\234"},
{"&#8221;", "\342\200\235"},
{"&#8222;", "\342\200\236"},
{"&#8224;", "\342\200\240"},
{"&#8225;", "\342\200\241"},
{"&#8226;", "\342\200\242"},
{"&#8230;", "\342\200\246"},
{"&#8240;", "\342\200\260"},
{"&#8249;", "\342\200\271"},
{"&#8250;", "\342\200\272"},
{"&#8364;", "\342\202\254"},
{"&#8482;", "\342\204\242"},
{"&quot;", "\42"},
{"&amp;", "\46"},
{"&apos;", "\47"},
{"&lt;", "\74"},
{"&gt;", "\76"},
{"&squot;", "\47"},
{"&nbsp;", "\40"},
{"&iexcl;", "\302\241"},
{"&cent;", "\302\242"},
{"&pound;", "\302\243"},
{"&curren;", "\302\244"},
{"&yen;", "\302\245"},
{"&brvbar;", "\302\246"},
{"&sect;", "\302\247"},
{"&uml;", "\302\250"},
{"&copy;", "\302\251"},
{"&ordf;", "\302\252"},
{"&laquo;", "\302\253"},
{"&not;", "\302\254"},
{"&shy;", "\302\255"},
{"&reg;", "\302\256"},
{"&macr;", "\302\257"},
{"&deg;", "\302\260"},
{"&plusmn;", "\302\261"},
{"&sup2;", "\302\262"},
{"&sup3;", "\302\263"},
{"&acute;", "\302\264"},
{"&micro;", "\302\265"},
{"&para;", "\302\266"},
{"&middot;", "\302\267"},
{"&cedil;", "\302\270"},
{"&sup1;", "\302\271"},
{"&ordm;", "\302\272"},
{"&raquo;", "\302\273"},
{"&frac14;", "\302\274"},
{"&frac12;", "\302\275"},
{"&frac34;", "\302\276"},
{"&iquest;", "\302\277"},
{"&Agrave;", "\303\200"},
{"&Aacute;", "\303\201"},
{"&Acirc;", "\303\202"},
{"&Atilde;", "\303\203"},
{"&Auml;", "\303\204"},
{"&Aring;", "\303\205"},
{"&AElig;", "\303\206"},
{"&Ccedil;", "\303\207"},
{"&Egrave;", "\303\210"},
{"&Eacute;", "\303\211"},
{"&Ecirc;", "\303\212"},
{"&Euml;", "\303\213"},
{"&Igrave;", "\303\214"},
{"&Iacute;", "\303\215"},
{"&Icirc;", "\303\216"},
{"&Iuml;", "\303\217"},
{"&ETH;", "\303\220"},
{"&Ntilde;", "\303\221"},
{"&Ograve;", "\303\222"},
{"&Oacute;", "\303\223"},
{"&Ocirc;", "\303\224"},
{"&Otilde;", "\303\225"},
{"&Ouml;", "\303\226"},
{"&times;", "\303\227"},
{"&Oslash;", "\303\230"},
{"&Ugrave;", "\303\231"},
{"&Uacute;", "\303\232"},
{"&Ucirc;", "\303\233"},
{"&Uuml;", "\303\234"},
{"&Yacute;", "\303\235"},
{"&THORN;", "\303\236"},
{"&szlig;", "\303\237"},
{"&agrave;", "\303\240"},
{"&aacute;", "\303\241"},
{"&acirc;", "\303\242"},
{"&atilde;", "\303\243"},
{"&auml;", "\303\244"},
{"&aring;", "\303\245"},
{"&aelig;", "\303\246"},
{"&ccedil;", "\303\247"},
{"&egrave;", "\303\250"},
{"&eacute;", "\303\251"},
{"&ecirc;", "\303\252"},
{"&euml;", "\303\253"},
{"&igrave;", "\303\254"},
{"&iacute;", "\303\255"},
{"&icirc;", "\303\256"},
{"&iuml;", "\303\257"},
{"&eth;", "\303\260"},
{"&ntilde;", "\303\261"},
{"&ograve;", "\303\262"},
{"&oacute;", "\303\263"},
{"&ocirc;", "\303\264"},
{"&otilde;", "\303\265"},
{"&ouml;", "\303\266"},
{"&divide;", "\303\267"},
{"&oslash;", "\303\270"},
{"&ugrave;", "\303\271"},
{"&uacute;", "\303\272"},
{"&ucirc;", "\303\273"},
{"&uuml;", "\303\274"},
{"&yacute;", "\303\275"},
{"&thorn;", "\303\276"},
{"&yuml;", "\303\277"},
{"&OElig;", "\305\222"},
{"&oelig;", "\305\223"},
{"&Scaron;", "\305\240"},
{"&scaron;", "\305\241"},
{"&Yuml;", "\305\270"},
{"&circ;", "\313\206"},
{"&tilde;", "\313\234"},
{"&ensp;", "\342\200\202"},
{"&emsp;", "\342\200\203"},
{"&thinsp;", "\342\200\211"},
{"&ndash;", "\342\200\223"},
{"&mdash;", "\342\200\224"},
{"&lsquo;", "\342\200\230"},
{"&rsquo;", "\342\200\231"},
{"&sbquo;", "\342\200\232"},
{"&ldquo;", "\342\200\234"},
{"&rdquo;", "\342\200\235"},
{"&bdquo;", "\342\200\236"},
{"&dagger;", "\342\200\240"},
{"&Dagger;", "\342\200\241"},
{"&bull;", "\342\200\242"},
{"&hellip;", "\342\200\246"},
{"&permil;", "\342\200\260"},
{"&lsaquo;", "\342\200\271"},
{"&rsaquo;", "\342\200\272"},
{"&euro;", "\342\202\254"},
{"&trade;", "\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;
}