diff options
author | Shaun McCance <shaunm@gnome.org> | 2011-10-21 17:35:14 -0400 |
---|---|---|
committer | Shaun McCance <shaunm@gnome.org> | 2011-10-21 17:35:14 -0400 |
commit | 1df7292299d68b71f467d3900803a6a5bf5b1e14 (patch) | |
tree | 76070aad63b79fa2fb529df59c5ad2c23ee86898 | |
parent | 96d73923b175054f98caeeef8ad74727ac2704a0 (diff) | |
download | yelp-1df7292299d68b71f467d3900803a6a5bf5b1e14.tar.gz |
Link to full text search when terms is in URI query string
-rw-r--r-- | data/xslt/mal2html.xsl.in | 5 | ||||
-rw-r--r-- | libyelp/yelp-uri.c | 41 | ||||
-rw-r--r-- | libyelp/yelp-uri.h | 2 | ||||
-rw-r--r-- | libyelp/yelp-view.c | 39 |
4 files changed, 84 insertions, 3 deletions
diff --git a/data/xslt/mal2html.xsl.in b/data/xslt/mal2html.xsl.in index 8b9238c1..928ded8d 100644 --- a/data/xslt/mal2html.xsl.in +++ b/data/xslt/mal2html.xsl.in @@ -55,6 +55,11 @@ a.linkdiv:hover { <xsl:value-of select="$color.blue_background"/><xsl:text>), to(</xsl:text> <xsl:value-of select="$color.background"/><xsl:text>)); } +div.fullsearch { + text-align: center; + max-width: 20em; + margin: 0 auto 1em auto; +} </xsl:text> <xsl:if test="$yelp.editor_mode"> <xsl:text> diff --git a/libyelp/yelp-uri.c b/libyelp/yelp-uri.c index 63b550ed..e21f8939 100644 --- a/libyelp/yelp-uri.c +++ b/libyelp/yelp-uri.c @@ -83,6 +83,8 @@ struct _YelpUriPrivate { gchar *page_id; gchar *frag_id; + GHashTable *query; + /* Unresolved */ YelpUri *res_base; gchar *res_arg; @@ -144,6 +146,10 @@ yelp_uri_class_init (YelpUriClass *klass) static void yelp_uri_init (YelpUri *uri) { + YelpUriPrivate *priv = GET_PRIV (uri); + + priv->query = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + return; } @@ -162,6 +168,11 @@ yelp_uri_dispose (GObject *object) priv->res_base = NULL; } + if (priv->query) { + g_hash_table_destroy (priv->query); + priv->query = NULL; + } + G_OBJECT_CLASS (yelp_uri_parent_class)->dispose (object); } @@ -484,6 +495,18 @@ yelp_uri_get_frag_id (YelpUri *uri) return g_strdup (priv->frag_id); } +gchar * +yelp_uri_get_query (YelpUri *uri, + const gchar *key) +{ + YelpUriPrivate *priv = GET_PRIV (uri); + const gchar *ret = g_hash_table_lookup (priv->query, key); + if (ret) + return g_strdup (ret); + else + return NULL; +} + /******************************************************************************/ gchar * @@ -827,6 +850,24 @@ resolve_help_uri (YelpUri *uri) else if (query) query = g_strdup (query + 1); + if (query) { + gchar **keyvals = g_strsplit (query, "&", 0); + gint i; + + for (i = 0; keyvals[i]; i++) { + gchar *key, *val; + val = strchr (keyvals[i], '='); + if (val == NULL) + continue; + key = g_uri_unescape_segment (keyvals[i], val, NULL); + val = g_uri_unescape_string (val + 1, NULL); + + g_hash_table_insert (priv->query, key, val); + } + + g_strfreev (keyvals); + } + if (hash) hash = g_strdup (hash + 1); diff --git a/libyelp/yelp-uri.h b/libyelp/yelp-uri.h index 5ebbd4a9..7067ad45 100644 --- a/libyelp/yelp-uri.h +++ b/libyelp/yelp-uri.h @@ -85,6 +85,8 @@ GFile * yelp_uri_get_file (YelpUri *uri); gchar ** yelp_uri_get_search_path (YelpUri *uri); gchar * yelp_uri_get_page_id (YelpUri *uri); gchar * yelp_uri_get_frag_id (YelpUri *uri); +gchar * yelp_uri_get_query (YelpUri *uri, + const gchar *key); gchar * yelp_uri_locate_file_uri (YelpUri *uri, const gchar *filename); diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c index 24af96bb..2e5e0c6d 100644 --- a/libyelp/yelp-view.c +++ b/libyelp/yelp-view.c @@ -2058,7 +2058,7 @@ document_callback (YelpDocument *document, else if (signal == YELP_DOCUMENT_SIGNAL_CONTENTS) { YelpUriDocumentType doctype; const gchar *contents; - gchar *mime_type, *page_id, *frag_id, *full_uri; + gchar *mime_type, *page_id, *frag_id, *full_uri, *search_terms; page_id = yelp_uri_get_page_id (priv->uri); debug_print (DB_ARG, " document.uri.page_id=\"%s\"\n", page_id); mime_type = yelp_document_get_mime_type (document, page_id); @@ -2112,17 +2112,20 @@ document_callback (YelpDocument *document, g_signal_handler_unblock (view, priv->navigation_requested); g_object_set (view, "state", YELP_VIEW_STATE_LOADED, NULL); + search_terms = yelp_uri_get_query (priv->uri, "terms"); + /* If we need to set the GtkAdjustment or trigger the page title * from what WebKit thinks it is (see comment below), we need to * let the main loop run through. */ - if (priv->vadjust > 0 || priv->hadjust > 0 || priv->page_title == NULL) + if (priv->vadjust > 0 || priv->hadjust > 0 || + priv->page_title == NULL || search_terms != NULL) while (g_main_context_pending (NULL)) { WebKitLoadStatus status; status = webkit_web_view_get_load_status (WEBKIT_WEB_VIEW (view)); g_main_context_iteration (NULL, FALSE); /* Sometimes some runaway JavaScript causes there to always - * be pending sources. Break out of the document is loaded. + * be pending sources. Break out if the document is loaded. */ if (status == WEBKIT_LOAD_FINISHED || status == WEBKIT_LOAD_FAILED) @@ -2158,6 +2161,36 @@ document_callback (YelpDocument *document, g_signal_emit_by_name (view, "notify::page-title", spec); } + if (search_terms) { + WebKitDOMDocument *doc; + WebKitDOMElement *body, *div, *link; + doc = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view)); + body = webkit_dom_document_query_selector (doc, "div.body", NULL); + if (body) { + gchar *tmp, *uri, *txt; + div = webkit_dom_document_create_element (doc, "div", NULL); + webkit_dom_element_set_attribute (div, "class", "fullsearch", NULL); + link = webkit_dom_document_create_element (doc, "a", NULL); + tmp = g_uri_escape_string (search_terms, NULL, FALSE); + uri = g_strconcat ("xref:search=", tmp, NULL); + webkit_dom_element_set_attribute (link, "href", uri, NULL); + g_free (tmp); + g_free (uri); + txt = g_strdup_printf (_("See all search results for ā%sā"), + search_terms); + webkit_dom_node_set_text_content (WEBKIT_DOM_NODE (link), txt, NULL); + g_free (txt); + webkit_dom_node_append_child (WEBKIT_DOM_NODE (div), + WEBKIT_DOM_NODE (link), + NULL); + webkit_dom_node_insert_before (WEBKIT_DOM_NODE (body), + WEBKIT_DOM_NODE (div), + webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)), + NULL); + } + g_free (search_terms); + } + g_free (frag_id); g_free (page_id); g_free (mime_type); |