# HG changeset patch # User Sadrul Habib Chowdhury # Date 1187848766 0 # Node ID 7df6301c28ed5c10cf9a7d0205f1a82f58c7f75a # Parent ec0ef7f0a12d80e41fb267a2ffa2304fc43e3f3f# Parent 9dc79e0a8d6c0c59693231e8294cfe6dbda5681c merge of '90df17ab44d01f7f8f963d36fefb9eae3ec868cf' and 'c7f08defa414414e054c80b1e6fab4a50be7207e' diff -r ec0ef7f0a12d -r 7df6301c28ed finch/libgnt/gntutils.c --- a/finch/libgnt/gntutils.c Thu Aug 23 04:43:42 2007 +0000 +++ b/finch/libgnt/gntutils.c Thu Aug 23 05:59:26 2007 +0000 @@ -376,6 +376,82 @@ #endif } +#ifndef NO_LIBXML +static void +util_parse_html_to_tv(xmlNode *node, GntTextView *tv, GntTextFormatFlags flag) +{ + const char *name; + char *content; + xmlNode *ch; + gboolean processed = FALSE; + char *url = NULL; + + if (node == NULL || node->name == NULL || node->type != XML_ELEMENT_NODE) + return; + + name = (char*)node->name; + if (g_ascii_strcasecmp(name, "b") == 0 || + g_ascii_strcasecmp(name, "strong") == 0 || + g_ascii_strcasecmp(name, "i") == 0 || + g_ascii_strcasecmp(name, "blockquote") == 0) { + flag |= GNT_TEXT_FLAG_BOLD; + } else if (g_ascii_strcasecmp(name, "u") == 0) { + flag |= GNT_TEXT_FLAG_UNDERLINE; + } else if (g_ascii_strcasecmp(name, "br") == 0) { + gnt_text_view_append_text_with_flags(tv, "\n", flag); + } else if (g_ascii_strcasecmp(name, "a") == 0) { + flag |= GNT_TEXT_FLAG_UNDERLINE; + url = (char *)xmlGetProp(node, (xmlChar*)"href"); + } else { + /* XXX: Process other possible tags */ + } + + for (ch = node->children; ch; ch = ch->next) { + if (ch->type == XML_ELEMENT_NODE) { + processed = TRUE; + } + util_parse_html_to_tv(ch, tv, flag); + } + + if (!processed) { + content = (char*)xmlNodeGetContent(node); + gnt_text_view_append_text_with_flags(tv, content, flag); + xmlFree(content); + } + + if (url) { + char *href = g_strdup_printf(" (%s)", url); + gnt_text_view_append_text_with_flags(tv, href, flag); + g_free(href); + xmlFree(url); + } +} +#endif + +gboolean gnt_util_parse_xhtml_to_textview(const char *string, GntTextView *tv) +{ +#ifdef NO_LIBXML + return FALSE; +#else + xmlParserCtxtPtr ctxt; + xmlDocPtr doc; + xmlNodePtr node; + GntTextFormatFlags flag = GNT_TEXT_FLAG_NORMAL; + gboolean ret = FALSE; + + ctxt = xmlNewParserCtxt(); + doc = xmlCtxtReadDoc(ctxt, (xmlChar*)string, NULL, NULL, XML_PARSE_NOBLANKS | XML_PARSE_RECOVER); + if (doc) { + node = xmlDocGetRootElement(doc); + util_parse_html_to_tv(node, tv, flag); + xmlFreeDoc(doc); + ret = TRUE; + } + xmlCleanupParser(); + return ret; +#endif +} + /* Setup trigger widget */ typedef struct { char *text; @@ -408,4 +484,3 @@ g_signal_connect(G_OBJECT(wid), "key_pressed", G_CALLBACK(key_pressed), tb); g_signal_connect_swapped(G_OBJECT(button), "destroy", G_CALLBACK(free_trigger_button), tb); } - diff -r ec0ef7f0a12d -r 7df6301c28ed finch/libgnt/gntutils.h --- a/finch/libgnt/gntutils.h Thu Aug 23 04:43:42 2007 +0000 +++ b/finch/libgnt/gntutils.h Thu Aug 23 05:59:26 2007 +0000 @@ -27,6 +27,7 @@ #include #include "gnt.h" +#include "gnttextview.h" #include "gntwidget.h" typedef gpointer (*GDupFunc)(gconstpointer data); @@ -132,6 +133,16 @@ void gnt_util_parse_widgets(const char *string, int num, ...); /** + * Parse an XHTML string and add it in a GntTextView with + * appropriate text flags. + * + * @param string The XHTML string + * @param tv The GntTextView + * @return @c TRUE if the string was added to the textview properly, @c FALSE otherwise. + */ +gboolean gnt_util_parse_xhtml_to_textview(const char *string, GntTextView *tv); + +/** * Make some keypress activate a button when some key is pressed with 'wid' in focus. * * @param widget The widget diff -r ec0ef7f0a12d -r 7df6301c28ed finch/libgnt/test/tv.c --- a/finch/libgnt/test/tv.c Thu Aug 23 04:43:42 2007 +0000 +++ b/finch/libgnt/test/tv.c Thu Aug 23 05:59:26 2007 +0000 @@ -5,6 +5,7 @@ #include "gntbox.h" #include "gntentry.h" #include "gnttextview.h" +#include "gntutils.h" static gboolean key_pressed(GntWidget *w, const char *key, GntWidget *view) @@ -117,6 +118,8 @@ gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(view), "plugins: ", GNT_TEXT_FLAG_BOLD); gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(view), "this is the 4th line\n", GNT_TEXT_FLAG_NORMAL); + gnt_util_parse_xhtml_to_textview("

Ohoy hoy!!

I think this is going to

WORK!!! check this out!!

", GNT_TEXT_VIEW(view)); + #ifdef STANDALONE gnt_main();