From 56434de79ef1a9896d7b37f88ee33ed0f1aeb186 Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Sat, 8 May 2021 21:26:17 +0530 Subject: [PATCH 1/4] Set label to title snippet if present With openzim/libzim#545 we now support snippet generation of titles which can be used as the display label on the ui for highlighted titles via the "label" field. The old version used plain title which is still available in the value field. --- src/reader.cpp | 1 + src/server/internalServer.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/reader.cpp b/src/reader.cpp index 1abc9eadb..48b826500 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -510,6 +510,7 @@ bool Reader::searchSuggestionsSmart(const string& prefix, suggestion.push_back(current.getTitle()); suggestion.push_back(current.getPath()); suggestion.push_back(kiwix::normalize(current.getTitle())); + suggestion.push_back(current.getSnippet()); results.push_back(suggestion); } retVal = true; diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index f28c3df03..381a56c1d 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -415,6 +415,11 @@ std::unique_ptr InternalServer::handle_suggest(const RequestContext& r for(auto& suggestion:suggestions) { MustacheData result; result.set("label", suggestion[0]); + + if (!suggestion[3].empty()) { + result.set("label", suggestion[3]); + } + result.set("value", suggestion[0]); result.set("kind", "path"); result.set("path", suggestion[1]); From 3288cd80e56a8f5545bf825eb3f5c8553526556e Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Mon, 10 May 2021 12:51:01 +0530 Subject: [PATCH 2/4] Render suggestion snippet properly To render the snippets properly, we need to use the _renderItem property of the autocomple ui. --- static/skin/taskbar.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/static/skin/taskbar.js b/static/skin/taskbar.js index df91cfadf..d2d26fb85 100644 --- a/static/skin/taskbar.js +++ b/static/skin/taskbar.js @@ -34,7 +34,12 @@ jq(document).ready(() => { $( "#kiwixsearchform" ).submit(); } }, - }); + }).data( "ui-autocomplete" )._renderItem = function( ul, item ) { + return $( "
  • " ) + .data( "ui-autocomplete-item", item ) + .append( item.label ) + .appendTo( ul ); + }; /* cybook hack */ if (navigator.userAgent.indexOf("bookeen/cybook") != -1) { From 5315034afe82c607da990ad22a62e32eb8f87b7f Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Thu, 13 May 2021 11:38:11 +0530 Subject: [PATCH 3/4] Introduce `SuggestionItem` class This is a helper class that allows to create and manage individual suggestion item and their data. --- include/reader.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/include/reader.h b/include/reader.h index 94c108f9d..395da4ee9 100644 --- a/include/reader.h +++ b/include/reader.h @@ -37,6 +37,41 @@ using namespace std; namespace kiwix { +/** + * The SuggestionItem is a helper class that contains the info about a single + * suggestion item. + */ + +class SuggestionItem +{ + // Functions + private: + // Create a sugggestion item. + explicit SuggestionItem(std::string title, std::string normalizedTitle, + std::string path, std::string snippet = "") : + title(title), + normalizedTitle(normalizedTitle), + path(path), + snippet(snippet) {} + + public: + const std::string getTitle() {return title;} + const std::string getNormalizedTitle() {return normalizedTitle;} + const std::string getPath() {return path;} + const std::string getSnippet() {return snippet;} + + const bool hasSnippet() {return !snippet.empty();} + + // Data + private: + std::string title; + std::string normalizedTitle; + std::string path; + std::string snippet; + + friend class Reader; +}; + /** * The Reader class is the class who allow to get an entry content from a zim * file. From 5567d8ca49768bb84291ceab6fc83fd7e904d30c Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Thu, 13 May 2021 12:00:39 +0530 Subject: [PATCH 4/4] Replace std::vector with SuggestionItem Each sugestions used to be stored as vector of strings to hold various values such as title, path etc inside them. With this commit, we use the new dedicated class `SuggestionItem` to do the same. --- include/reader.h | 2 +- src/reader.cpp | 24 +++++++++--------------- src/server/internalServer.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/include/reader.h b/include/reader.h index 395da4ee9..4001a5ff7 100644 --- a/include/reader.h +++ b/include/reader.h @@ -77,7 +77,7 @@ class SuggestionItem * file. */ -using SuggestionsList_t = std::vector>; +using SuggestionsList_t = std::vector; class Reader { public: diff --git a/src/reader.cpp b/src/reader.cpp index 48b826500..ded00f287 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -432,12 +432,12 @@ bool Reader::searchSuggestions(const string& prefix, article is already in the suggestions list (with an other title) */ bool insert = true; - std::vector>::iterator suggestionItr; + std::vector::iterator suggestionItr; for (suggestionItr = results.begin(); suggestionItr != results.end(); suggestionItr++) { - int result = normalizedArticleTitle.compare((*suggestionItr)[2]); - if (result == 0 && articleFinalUrl.compare((*suggestionItr)[1]) == 0) { + int result = normalizedArticleTitle.compare((*suggestionItr).getNormalizedTitle()); + if (result == 0 && articleFinalUrl.compare((*suggestionItr).getPath()) == 0) { insert = false; break; } else if (result < 0) { @@ -447,10 +447,7 @@ bool Reader::searchSuggestions(const string& prefix, /* Insert if possible */ if (insert) { - std::vector suggestion; - suggestion.push_back(entry.getTitle()); - suggestion.push_back(articleFinalUrl); - suggestion.push_back(normalizedArticleTitle); + SuggestionItem suggestion(entry.getTitle(), normalizedArticleTitle, articleFinalUrl); results.insert(suggestionItr, suggestion); } @@ -506,11 +503,8 @@ bool Reader::searchSuggestionsSmart(const string& prefix, for (auto current = suggestions.begin(); current != suggestions.end(); current++) { - std::vector suggestion; - suggestion.push_back(current.getTitle()); - suggestion.push_back(current.getPath()); - suggestion.push_back(kiwix::normalize(current.getTitle())); - suggestion.push_back(current.getSnippet()); + SuggestionItem suggestion(current.getTitle(), kiwix::normalize(current.getTitle()), + current.getPath(), current.getSnippet()); results.push_back(suggestion); } retVal = true; @@ -531,7 +525,7 @@ bool Reader::getNextSuggestion(string& title) { if (this->suggestionsOffset != this->suggestions.end()) { /* title */ - title = (*(this->suggestionsOffset))[0]; + title = (*(this->suggestionsOffset)).getTitle(); /* increment the cursor for the next call */ this->suggestionsOffset++; @@ -546,8 +540,8 @@ bool Reader::getNextSuggestion(string& title, string& url) { if (this->suggestionsOffset != this->suggestions.end()) { /* title */ - title = (*(this->suggestionsOffset))[0]; - url = (*(this->suggestionsOffset))[1]; + title = (*(this->suggestionsOffset)).getTitle(); + url = (*(this->suggestionsOffset)).getPath(); /* increment the cursor for the next call */ this->suggestionsOffset++; diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 381a56c1d..e248de055 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -414,15 +414,15 @@ std::unique_ptr InternalServer::handle_suggest(const RequestContext& r reader->searchSuggestionsSmart(term, maxSuggestionCount, suggestions); for(auto& suggestion:suggestions) { MustacheData result; - result.set("label", suggestion[0]); + result.set("label", suggestion.getTitle()); - if (!suggestion[3].empty()) { - result.set("label", suggestion[3]); + if (suggestion.hasSnippet()) { + result.set("label", suggestion.getSnippet()); } - result.set("value", suggestion[0]); + result.set("value", suggestion.getTitle()); result.set("kind", "path"); - result.set("path", suggestion[1]); + result.set("path", suggestion.getPath()); result.set("first", first); first = false; results.push_back(result);