[devhelp] DhLink: add belongs_to_page()



commit c4e366293929edb7501801d042a0c86c4620a29a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Dec 6 16:52:17 2017 +0100

    DhLink: add belongs_to_page()
    
    This will be used in DhKeywordModel instead of calling
    dh_link_get_file_name().

 docs/reference/devhelp-sections.txt |    1 +
 src/dh-link.c                       |   49 +++++++++++++++++++++++++++++++++++
 src/dh-link.h                       |    4 +++
 3 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/devhelp-sections.txt b/docs/reference/devhelp-sections.txt
index 15f1bdf..93ae521 100644
--- a/docs/reference/devhelp-sections.txt
+++ b/docs/reference/devhelp-sections.txt
@@ -118,6 +118,7 @@ dh_link_set_flags
 dh_link_get_name
 dh_link_get_file_name
 dh_link_match_relative_url
+dh_link_belongs_to_page
 dh_link_get_uri
 dh_link_get_book_name
 dh_link_get_book_id
diff --git a/src/dh-link.c b/src/dh-link.c
index b1710b4..40e61e3 100644
--- a/src/dh-link.c
+++ b/src/dh-link.c
@@ -304,6 +304,55 @@ dh_link_match_relative_url (DhLink      *link,
 }
 
 /**
+ * dh_link_belongs_to_page:
+ * @link: a #DhLink.
+ * @page_id: a page ID, i.e. the filename without its extension.
+ * @case_sensitive: whether @page_id is case sensitive.
+ *
+ * This function permits to know if @link belongs to a certain page.
+ *
+ * @page_id is usually the HTML filename without the `.html` extension. More
+ * generally, @page_id must be a relative URL (relative to the book base path),
+ * without the anchor nor the file extension.
+ *
+ * For example if @link has the relative URL `"DhLink.html#dh-link-ref"`, then
+ * this function will return %TRUE if the @page_id is `"DhLink"`.
+ *
+ * Returns: whether @link belongs to @page_id.
+ * Since: 3.28
+ */
+gboolean
+dh_link_belongs_to_page (DhLink      *link,
+                         const gchar *page_id,
+                         gboolean     case_sensitive)
+{
+        const gchar *relative_url;
+        gsize page_id_len;
+        gboolean has_prefix;
+
+        g_return_val_if_fail (link != NULL, FALSE);
+        g_return_val_if_fail (link->relative_url != NULL, FALSE);
+        g_return_val_if_fail (page_id != NULL, FALSE);
+
+        relative_url = link->relative_url;
+        if (relative_url[0] == '\0')
+                relative_url = "index.html";
+
+        page_id_len = strlen (page_id);
+
+        if (case_sensitive)
+                has_prefix = strncmp (relative_url, page_id, page_id_len) == 0;
+        else
+                has_prefix = g_ascii_strncasecmp (relative_url, page_id, page_id_len) == 0;
+
+        /* Check that a file extension follows. */
+        if (has_prefix)
+                return relative_url[page_id_len] == '.';
+
+        return FALSE;
+}
+
+/**
  * dh_link_get_uri:
  * @link: a #DhLink.
  *
diff --git a/src/dh-link.h b/src/dh-link.h
index 51093a0..69e9ad2 100644
--- a/src/dh-link.h
+++ b/src/dh-link.h
@@ -96,6 +96,10 @@ const gchar *dh_link_get_file_name      (DhLink        *link);
 gboolean     dh_link_match_relative_url (DhLink        *link,
                                          const gchar   *relative_url);
 
+gboolean     dh_link_belongs_to_page    (DhLink        *link,
+                                         const gchar   *page_id,
+                                         gboolean       case_sensitive);
+
 gchar *      dh_link_get_uri            (DhLink        *link);
 
 const gchar *dh_link_get_book_name      (DhLink        *link);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]