Merge pull request #505 from kiwix/suggestion_snippets

This commit is contained in:
Matthieu Gautier 2021-05-26 11:04:52 +02:00 committed by GitHub
commit 7f0d3004c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 19 deletions

View File

@ -37,12 +37,47 @@ using namespace std;
namespace kiwix 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 * The Reader class is the class who allow to get an entry content from a zim
* file. * file.
*/ */
using SuggestionsList_t = std::vector<std::vector<std::string>>; using SuggestionsList_t = std::vector<SuggestionItem>;
class Reader class Reader
{ {
public: public:

View File

@ -432,12 +432,12 @@ bool Reader::searchSuggestions(const string& prefix,
article is already in the suggestions list (with an other article is already in the suggestions list (with an other
title) */ title) */
bool insert = true; bool insert = true;
std::vector<std::vector<std::string>>::iterator suggestionItr; std::vector<SuggestionItem>::iterator suggestionItr;
for (suggestionItr = results.begin(); for (suggestionItr = results.begin();
suggestionItr != results.end(); suggestionItr != results.end();
suggestionItr++) { suggestionItr++) {
int result = normalizedArticleTitle.compare((*suggestionItr)[2]); int result = normalizedArticleTitle.compare((*suggestionItr).getNormalizedTitle());
if (result == 0 && articleFinalUrl.compare((*suggestionItr)[1]) == 0) { if (result == 0 && articleFinalUrl.compare((*suggestionItr).getPath()) == 0) {
insert = false; insert = false;
break; break;
} else if (result < 0) { } else if (result < 0) {
@ -447,10 +447,7 @@ bool Reader::searchSuggestions(const string& prefix,
/* Insert if possible */ /* Insert if possible */
if (insert) { if (insert) {
std::vector<std::string> suggestion; SuggestionItem suggestion(entry.getTitle(), normalizedArticleTitle, articleFinalUrl);
suggestion.push_back(entry.getTitle());
suggestion.push_back(articleFinalUrl);
suggestion.push_back(normalizedArticleTitle);
results.insert(suggestionItr, suggestion); results.insert(suggestionItr, suggestion);
} }
@ -506,10 +503,8 @@ bool Reader::searchSuggestionsSmart(const string& prefix,
for (auto current = suggestions.begin(); for (auto current = suggestions.begin();
current != suggestions.end(); current != suggestions.end();
current++) { current++) {
std::vector<std::string> suggestion; SuggestionItem suggestion(current.getTitle(), kiwix::normalize(current.getTitle()),
suggestion.push_back(current.getTitle()); current.getPath(), current.getSnippet());
suggestion.push_back(current.getPath());
suggestion.push_back(kiwix::normalize(current.getTitle()));
results.push_back(suggestion); results.push_back(suggestion);
} }
retVal = true; retVal = true;
@ -530,7 +525,7 @@ bool Reader::getNextSuggestion(string& title)
{ {
if (this->suggestionsOffset != this->suggestions.end()) { if (this->suggestionsOffset != this->suggestions.end()) {
/* title */ /* title */
title = (*(this->suggestionsOffset))[0]; title = (*(this->suggestionsOffset)).getTitle();
/* increment the cursor for the next call */ /* increment the cursor for the next call */
this->suggestionsOffset++; this->suggestionsOffset++;
@ -545,8 +540,8 @@ bool Reader::getNextSuggestion(string& title, string& url)
{ {
if (this->suggestionsOffset != this->suggestions.end()) { if (this->suggestionsOffset != this->suggestions.end()) {
/* title */ /* title */
title = (*(this->suggestionsOffset))[0]; title = (*(this->suggestionsOffset)).getTitle();
url = (*(this->suggestionsOffset))[1]; url = (*(this->suggestionsOffset)).getPath();
/* increment the cursor for the next call */ /* increment the cursor for the next call */
this->suggestionsOffset++; this->suggestionsOffset++;

View File

@ -414,10 +414,15 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
reader->searchSuggestionsSmart(term, maxSuggestionCount, suggestions); reader->searchSuggestionsSmart(term, maxSuggestionCount, suggestions);
for(auto& suggestion:suggestions) { for(auto& suggestion:suggestions) {
MustacheData result; MustacheData result;
result.set("label", suggestion[0]); result.set("label", suggestion.getTitle());
result.set("value", suggestion[0]);
if (suggestion.hasSnippet()) {
result.set("label", suggestion.getSnippet());
}
result.set("value", suggestion.getTitle());
result.set("kind", "path"); result.set("kind", "path");
result.set("path", suggestion[1]); result.set("path", suggestion.getPath());
result.set("first", first); result.set("first", first);
first = false; first = false;
results.push_back(result); results.push_back(result);

View File

@ -34,7 +34,12 @@ jq(document).ready(() => {
$( "#kiwixsearchform" ).submit(); $( "#kiwixsearchform" ).submit();
} }
}, },
}); }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.data( "ui-autocomplete-item", item )
.append( item.label )
.appendTo( ul );
};
/* cybook hack */ /* cybook hack */
if (navigator.userAgent.indexOf("bookeen/cybook") != -1) { if (navigator.userAgent.indexOf("bookeen/cybook") != -1) {