[libgepub] Helper functions to convert from res to chapter
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgepub] Helper functions to convert from res to chapter
- Date: Fri, 21 Sep 2018 09:53:47 +0000 (UTC)
commit 021364bc2f62dc38fa6629f44995a394038306c5
Author: Daniel GarcĂa Moreno <danigm wadobo com>
Date: Fri Sep 21 11:50:58 2018 +0200
Helper functions to convert from res to chapter
The `gepub_doc_resource_uri_to_chapter` and
`gepub_doc_resource_id_to_chapter` methods are useful to convert a
resource ID or URI to a chapter number and then use in the
`gepub_doc_set_chapter` method, to navigate.
This new helper functions can be combined with the new
`gepub_doc_get_toc` to navigate in the document.
See #6
libgepub/gepub-doc.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++--
libgepub/gepub-doc.h | 4 +++
tests/test-gepub.c | 1 +
3 files changed, 89 insertions(+), 3 deletions(-)
---
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index ea8dbba..4059ac5 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -410,10 +410,18 @@ gepub_doc_fill_toc (GepubDoc *doc, gchar *toc_id)
}
if (!g_strcmp0 ((const gchar *)navchilds->name, "content")) {
- gchar *uri, *tmpuri;
+ gchar **split;
+ gchar *tmpuri;
tmpuri = gepub_utils_get_prop (navchilds, "src");
- uri = g_strdup_printf ("%s%s", doc->content_base, tmpuri);
- navpoint->content = uri;
+ // removing # params. Maybe we should store the # params in the
+ // navpoint to use in the future if the doc references to a position
+ // inside the chapter
+ split = g_strsplit (tmpuri, "#", -1);
+
+ // adding the base path
+ navpoint->content = g_strdup_printf ("%s%s", doc->content_base, split[0]);
+
+ g_strfreev (split);
g_free (tmpuri);
}
@@ -921,3 +929,76 @@ gepub_doc_get_toc (GepubDoc *doc)
return doc->toc;
}
+/**
+ * gepub_doc_resource_uri_to_chapter:
+ * @doc: a #GepubDoc
+ * @uri: The resource path
+ *
+ * This method tries to find the resource by path in the doc spine and
+ * will return the index in that list. If the resourse isn't there this method
+ * will return -1.
+
+ * Returns: the chapter index to use with gepub_doc_set_chapter or -1 if the
+ * resource isn't found
+ */
+gint
+gepub_doc_resource_uri_to_chapter (GepubDoc *doc,
+ const gchar *uri)
+{
+ GHashTableIter iter;
+ gchar *key;
+ GepubResource *res;
+ gchar *id = NULL;
+
+ g_return_val_if_fail (GEPUB_IS_DOC (doc), -1);
+ g_return_val_if_fail (doc->spine != NULL, -1);
+
+ g_hash_table_iter_init (&iter, doc->resources);
+ while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&res)) {
+ if (!g_strcmp0 (res->uri, uri)) {
+ id = key;
+ break;
+ }
+ }
+
+ if (!id) {
+ return -1;
+ }
+
+ return gepub_doc_resource_id_to_chapter (doc, id);
+}
+
+/**
+ * gepub_doc_resource_id_to_chapter:
+ * @doc: a #GepubDoc
+ * @id: The resource id
+ *
+ * This method tries to find the resource by id in the doc spine and
+ * will return the index in that list. If the resourse isn't there this method
+ * will return -1.
+
+ * Returns: the chapter index to use with gepub_doc_set_chapter or -1 if the
+ * resource isn't found
+ */
+gint
+gepub_doc_resource_id_to_chapter (GepubDoc *doc,
+ const gchar *id)
+{
+ GList *spine;
+ gint chapter = 0;
+
+ g_return_val_if_fail (GEPUB_IS_DOC (doc), -1);
+ g_return_val_if_fail (doc->spine != NULL, -1);
+
+ spine = g_list_first (doc->spine);
+ while (spine && spine->data) {
+ if (!g_strcmp0 (spine->data, id)) {
+ return chapter;
+ }
+ chapter++;
+ spine = spine->next;
+ }
+
+ return -1;
+}
+
diff --git a/libgepub/gepub-doc.h b/libgepub/gepub-doc.h
index 4290d92..b24a058 100644
--- a/libgepub/gepub-doc.h
+++ b/libgepub/gepub-doc.h
@@ -77,6 +77,10 @@ void gepub_doc_set_chapter (GepubDoc *doc,
gint index);
GList *gepub_doc_get_toc (GepubDoc *doc);
+gint gepub_doc_resource_uri_to_chapter (GepubDoc *doc,
+ const gchar *uri);
+gint gepub_doc_resource_id_to_chapter (GepubDoc *doc,
+ const gchar *id);
G_END_DECLS
diff --git a/tests/test-gepub.c b/tests/test-gepub.c
index afdf714..55ab1a2 100644
--- a/tests/test-gepub.c
+++ b/tests/test-gepub.c
@@ -282,6 +282,7 @@ test_doc_toc (const char *path)
while (nav && nav->data) {
GepubNavPoint *point = (GepubNavPoint*)nav->data;
PTEST ("%02d: %s -> %s\n", (gint)point->playorder, point->label, point->content);
+ PTEST (" -> Chapter: %d\n", gepub_doc_resource_uri_to_chapter (doc, point->content));
nav = nav->next;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]