Add Pango parsing.

This commit is contained in:
Přemysl Eric Janouch 2013-05-15 20:58:40 +02:00
parent b8c4d29233
commit 11c16cc173
2 changed files with 29 additions and 6 deletions

View File

@ -1,6 +1,6 @@
SHELL = /bin/sh SHELL = /bin/sh
pkgs = ncursesw glib-2.0 gio-2.0 pkgs = ncursesw glib-2.0 gio-2.0 pango
tests = test-stardict tests = test-stardict
targets = sdtui add-pronunciation $(tests) targets = sdtui add-pronunciation $(tests)

View File

@ -29,6 +29,7 @@
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <pango/pango.h>
#include <ncurses.h> #include <ncurses.h>
#include <unistd.h> #include <unistd.h>
@ -99,6 +100,17 @@ struct application
}; };
/** Splits the entry and adds it to a pointer array. */
static void
view_entry_split_add (GPtrArray *out, const gchar *text)
{
gchar **it, **tmp = g_strsplit (text, "\n", -1);
for (it = tmp; *it; it++)
if (**it)
g_ptr_array_add (out, g_strdup (*it));
g_strfreev (tmp);
}
/** Decomposes a dictionary entry into the format we want. */ /** Decomposes a dictionary entry into the format we want. */
static ViewEntry * static ViewEntry *
view_entry_new (StardictIterator *iterator) view_entry_new (StardictIterator *iterator)
@ -113,18 +125,25 @@ view_entry_new (StardictIterator *iterator)
GPtrArray *definitions = g_ptr_array_new (); GPtrArray *definitions = g_ptr_array_new ();
const GList *fields = stardict_entry_get_fields (entry); const GList *fields = stardict_entry_get_fields (entry);
gboolean found_anything_displayable = FALSE;
while (fields) while (fields)
{ {
const StardictEntryField *field = fields->data; const StardictEntryField *field = fields->data;
switch (field->type) switch (field->type)
{ {
case STARDICT_FIELD_MEANING: case STARDICT_FIELD_MEANING:
view_entry_split_add (definitions, field->data);
found_anything_displayable = TRUE;
break;
case STARDICT_FIELD_PANGO:
{ {
gchar **it, **tmp = g_strsplit (field->data, "\n", -1); char *text;
for (it = tmp; *it; it++) if (!pango_parse_markup (field->data, -1,
if (**it) 0, NULL, &text, NULL, NULL))
g_ptr_array_add (definitions, g_strdup (*it)); text = g_strdup_printf ("<%s>", _("error in entry"));
g_strfreev (tmp); view_entry_split_add (definitions, text);
found_anything_displayable = TRUE;
g_free (text);
break; break;
} }
case STARDICT_FIELD_PHONETIC: case STARDICT_FIELD_PHONETIC:
@ -138,6 +157,10 @@ view_entry_new (StardictIterator *iterator)
} }
g_object_unref (entry); g_object_unref (entry);
if (!found_anything_displayable)
g_ptr_array_add (definitions,
g_strdup_printf ("<%s>", _("no usable field found")));
ve->word = g_string_free (word, FALSE); ve->word = g_string_free (word, FALSE);
ve->definitions_length = definitions->len; ve->definitions_length = definitions->len;
g_ptr_array_add (definitions, NULL); g_ptr_array_add (definitions, NULL);