[nautilus/wip/antoniof/search-list-redesign: 20/20] search-engine-tracker: Elaborate snippet




commit bd163a52ba2ae503d1a5390b4bfc0447149fa2d3
Author: António Fernandes <antoniof gnome org>
Date:   Wed Aug 3 10:03:05 2022 +0100

    search-engine-tracker: Elaborate snippet
    
    Extend snippet up to 20 words ("tokens" in database parlance).
    
    Highlight matching word using pango markup.

 src/nautilus-name-cell.c             |  2 +-
 src/nautilus-search-engine-tracker.c | 20 ++++++++++++++++++--
 2 files changed, 19 insertions(+), 3 deletions(-)
---
diff --git a/src/nautilus-name-cell.c b/src/nautilus-name-cell.c
index 553628bd4..cf46e17e6 100644
--- a/src/nautilus-name-cell.c
+++ b/src/nautilus-name-cell.c
@@ -113,7 +113,7 @@ update_labels (NautilusNameCell *self)
 
     gtk_label_set_text (GTK_LABEL (self->label), display_name);
     gtk_label_set_text (GTK_LABEL (self->path), path_text);
-    gtk_label_set_text (GTK_LABEL (self->snippet), fts_snippet);
+    gtk_label_set_markup (GTK_LABEL (self->snippet), fts_snippet);
 
     gtk_widget_set_visible (self->path, (path_text != NULL));
     gtk_widget_set_visible (self->snippet_button, (fts_snippet != NULL));
diff --git a/src/nautilus-search-engine-tracker.c b/src/nautilus-search-engine-tracker.c
index e0c2c8c8d..b4ae92ad4 100644
--- a/src/nautilus-search-engine-tracker.c
+++ b/src/nautilus-search-engine-tracker.c
@@ -217,7 +217,18 @@ cursor_callback (GObject      *object,
     if (tracker->fts_enabled)
     {
         snippet = tracker_sparql_cursor_get_string (cursor, 5, NULL);
-        nautilus_search_hit_set_fts_snippet (hit, snippet);
+        if (snippet != NULL)
+        {
+            g_autofree gchar *escaped = NULL;
+            g_autoptr (GString) buffer = NULL;
+            /* Escape for markup, before adding our own markup. */
+            escaped = g_markup_escape_text (snippet, -1);
+            buffer = g_string_new (escaped);
+            g_string_replace (buffer, "_NAUTILUS_SNIPPET_DELIM_START_", "<b>", 0);
+            g_string_replace (buffer, "_NAUTILUS_SNIPPET_DELIM_END_", "</b>", 0);
+
+            nautilus_search_hit_set_fts_snippet (hit, buffer->str);
+        }
     }
 
     if (g_time_val_from_iso8601 (mtime_str, &tv))
@@ -357,7 +368,12 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
 
     if (tracker->fts_enabled && *search_text)
     {
-        g_string_append (sparql, " fts:snippet(?content)");
+        g_string_append (sparql,
+                         "fts:snippet(?content,"
+                         "            '_NAUTILUS_SNIPPET_DELIM_START_',"
+                         "            '_NAUTILUS_SNIPPET_DELIM_END_', "
+                         "            '…',"
+                         "            20)");
     }
 
     g_string_append (sparql, "FROM tracker:FileSystem ");


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