Improve title and URI display in the feed panel

We strip HTML from "title" elements when required now. And we
always try to show at least the URI for feeds in the WebKit view.
And we fallback to using the URI in the treeview if the title/
description is empty.
This commit is contained in:
Dale Whittaker 2009-05-09 14:08:06 +02:00 committed by Christian Dywan
parent 68bfa4331b
commit ec44108653
5 changed files with 87 additions and 17 deletions

View file

@ -138,6 +138,31 @@ atom_get_link (KatzeItem* item,
} }
} }
static gchar*
atom_get_title (FeedParser* fparser)
{
if (!katze_item_get_name (fparser->item))
{
gchar* type;
type = (gchar*)xmlGetProp (fparser->node, BAD_CAST "type");
if (type)
{
gchar* content = NULL;
if (g_str_equal (type, "html") ||
g_str_equal (type, "xhtml"))
content = feed_get_element_markup (fparser);
xmlFree (type);
if (content)
return content;
}
}
return feed_get_element_string (fparser);
}
static void static void
atom_preparse_entry (FeedParser* fparser) atom_preparse_entry (FeedParser* fparser)
{ {
@ -161,7 +186,7 @@ atom_parse_entry (FeedParser* fparser)
} }
else if (!xmlStrcmp (node->name, BAD_CAST "title")) else if (!xmlStrcmp (node->name, BAD_CAST "title"))
{ {
content = feed_get_element_string (fparser); content = atom_get_title (fparser);
katze_item_set_name (fparser->item, content); katze_item_set_name (fparser->item, content);
} }
else if (!xmlStrcmp (node->name, BAD_CAST "summary")) else if (!xmlStrcmp (node->name, BAD_CAST "summary"))
@ -249,7 +274,7 @@ atom_parse_feed (FeedParser* fparser)
} }
else if (!xmlStrcmp (node->name, BAD_CAST "title")) else if (!xmlStrcmp (node->name, BAD_CAST "title"))
{ {
content = feed_get_element_string (fparser); content = atom_get_title (fparser);
katze_item_set_name (fparser->item, content); katze_item_set_name (fparser->item, content);
} }
else if (!xmlStrcmp (node->name, BAD_CAST "subtitle")) else if (!xmlStrcmp (node->name, BAD_CAST "subtitle"))

View file

@ -121,9 +121,9 @@ feed_panel_treeview_render_text_cb (GtkTreeViewColumn* column,
g_assert (KATZE_IS_ITEM (item)); g_assert (KATZE_IS_ITEM (item));
title = katze_item_get_name (item); title = katze_item_get_name (item);
if (!title) if (!title || !*title || g_str_equal (title, " "))
title = katze_item_get_text (item); title = katze_item_get_text (item);
if (!title) if (!title || !*title || g_str_equal (title, " "))
title = katze_item_get_uri (item); title = katze_item_get_uri (item);
g_object_set (renderer, "text", title, NULL); g_object_set (renderer, "text", title, NULL);
@ -377,23 +377,24 @@ feed_panel_cursor_or_row_changed_cb (GtkTreeView* treeview,
text = g_strdup (katze_item_get_text (KATZE_ITEM (item))); text = g_strdup (katze_item_get_text (KATZE_ITEM (item)));
else else
{ {
KatzeItem* parent;
const gchar* puri;
parent = katze_item_get_parent (item);
g_assert (KATZE_IS_ARRAY (parent));
date = katze_item_get_added (item); date = katze_item_get_added (item);
puri = katze_item_get_uri (parent);
if (date) if (date)
{ {
time_t date_t; time_t date_t;
const struct tm* tm; const struct tm* tm;
static gchar date_format[512]; static gchar date_format[512];
const gchar* puri;
KatzeItem* parent;
gchar* last_updated; gchar* last_updated;
date_t = (time_t)date; date_t = (time_t)date;
tm = localtime (&date_t); tm = localtime (&date_t);
/* Some gcc versions complain about "%c" for no reason */ /* Some gcc versions complain about "%c" for no reason */
strftime (date_format, sizeof (date_format), "%c", tm); strftime (date_format, sizeof (date_format), "%c", tm);
parent = katze_item_get_parent (item);
g_assert (KATZE_IS_ARRAY (parent));
puri = katze_item_get_uri (parent);
/* i18n: The local date a feed was last updated */ /* i18n: The local date a feed was last updated */
last_updated = g_strdup_printf (C_("Feed", "Last updated: %s."), last_updated = g_strdup_printf (C_("Feed", "Last updated: %s."),
date_format); date_format);
@ -403,6 +404,12 @@ feed_panel_cursor_or_row_changed_cb (GtkTreeView* treeview,
puri, last_updated); puri, last_updated);
g_free (last_updated); g_free (last_updated);
} }
else
{
text = g_strdup_printf (
"<html><head><title>feed</title></head>"
"<body><h3>%s</h3></body></html>", puri);
}
} }
webkit_web_view_load_html_string ( webkit_web_view_load_html_string (
WEBKIT_WEB_VIEW (panel->webview), text ? text : "", uri); WEBKIT_WEB_VIEW (panel->webview), text ? text : "", uri);
@ -413,11 +420,8 @@ feed_panel_cursor_or_row_changed_cb (GtkTreeView* treeview,
else else
{ {
text = katze_item_get_text (item); text = katze_item_get_text (item);
if (text) webkit_web_view_load_html_string (
{ WEBKIT_WEB_VIEW (panel->webview), text ? text : "", uri);
webkit_web_view_load_html_string (
WEBKIT_WEB_VIEW (panel->webview), text, uri);
}
} }
g_object_unref (item); g_object_unref (item);
} }

View file

@ -30,7 +30,43 @@ feed_get_element_string (FeedParser* fparser)
return g_strdup (" "); return g_strdup (" ");
} }
return (gchar* )xmlNodeListGetString (fparser->doc, node->children, 1); return (gchar*)xmlNodeListGetString (fparser->doc, node->children, 1);
}
void
handle_markup_chars (void* user_data,
const xmlChar* ch,
int len)
{
gchar** markup;
gchar* temp;
markup = (gchar**)user_data;
temp = g_strndup ((gchar*)ch, len);
*markup = (*markup) ? g_strconcat (*markup, temp, NULL) : g_strdup (temp);
g_free (temp);
}
gchar*
feed_get_element_markup (FeedParser* fparser)
{
gchar* markup;
const xmlChar* stag;
markup = feed_get_element_string (fparser);
if ((stag = xmlStrchr (BAD_CAST markup, '<')) && xmlStrchr (stag, '>'))
{
gchar* text = NULL;
htmlSAXHandlerPtr psax;
psax = g_new0 (htmlSAXHandler, 1);
psax->characters = handle_markup_chars;
htmlSAXParseDoc (BAD_CAST markup, NULL, psax, &text);
g_free (psax);
g_free (markup);
return text;
}
return markup;
} }
gint64 gint64

View file

@ -20,6 +20,7 @@
#include <libsoup/soup.h> #include <libsoup/soup.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/HTMLparser.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -52,9 +53,13 @@ typedef struct _FeedParser
#define feed_parser_set_error(fparser, err, msg) \ #define feed_parser_set_error(fparser, err, msg) \
*(fparser)->error = g_error_new ( \ *(fparser)->error = g_error_new ( \
FEED_PARSE_ERROR, (err), (msg)) FEED_PARSE_ERROR, (err), (msg))
gchar* gchar*
feed_get_element_string (FeedParser* fparser); feed_get_element_string (FeedParser* fparser);
gchar*
feed_get_element_markup (FeedParser* fparser);
gint64 gint64
feed_get_element_date (FeedParser* fparser); feed_get_element_date (FeedParser* fparser);

View file

@ -107,7 +107,7 @@ rss_parse_item (FeedParser* fparser)
} }
else if (!xmlStrcmp (node->name, BAD_CAST "title")) else if (!xmlStrcmp (node->name, BAD_CAST "title"))
{ {
content = feed_get_element_string (fparser); content = feed_get_element_markup (fparser);
katze_item_set_name (fparser->item, content); katze_item_set_name (fparser->item, content);
} }
else if (!xmlStrcmp (node->name, BAD_CAST "description")) else if (!xmlStrcmp (node->name, BAD_CAST "description"))
@ -165,7 +165,7 @@ rss_parse_channel (FeedParser* fparser)
if (!xmlStrcmp (node->name, BAD_CAST "title")) if (!xmlStrcmp (node->name, BAD_CAST "title"))
{ {
content = feed_get_element_string (fparser); content = feed_get_element_markup (fparser);
katze_item_set_name (fparser->item, content); katze_item_set_name (fparser->item, content);
} }
else if (!xmlStrcmp (node->name, BAD_CAST "description")) else if (!xmlStrcmp (node->name, BAD_CAST "description"))