diff --git a/include/search_renderer.h b/include/search_renderer.h index 673e31ebe..627699a69 100644 --- a/include/search_renderer.h +++ b/include/search_renderer.h @@ -49,10 +49,24 @@ class SearchRenderer /** * Construct a SearchRenderer from a SearchResultSet. * + * The constructed version of the SearchRenderer will not introduce + * the book name for each result. It is better to use the other constructor + * with a Library pointer to have a better html page. + * * @param srs The `SearchResultSet` to render. * @param mapper The `NameMapper` to use to do the rendering. - * @param library The `Library` to use to look up book details for search - * results + * @param start The start offset used for the srs. + * @param estimatedResultCount The estimatedResultCount of the whole search + */ + SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, + unsigned int start, unsigned int estimatedResultCount); + + /** + * Construct a SearchRenderer from a SearchResultSet. + * + * @param srs The `SearchResultSet` to render. + * @param mapper The `NameMapper` to use to do the rendering. + * @param library The `Library` to use to look up book details for search results. * @param start The start offset used for the srs. * @param estimatedResultCount The estimatedResultCount of the whole search */ diff --git a/src/search_renderer.cpp b/src/search_renderer.cpp index 4b6b8e90b..a899a1704 100644 --- a/src/search_renderer.cpp +++ b/src/search_renderer.cpp @@ -39,12 +39,17 @@ namespace kiwix /* Constructor */ SearchRenderer::SearchRenderer(Searcher* searcher, NameMapper* mapper) - : m_srs(searcher->getSearchResultSet()), - mp_nameMapper(mapper), - protocolPrefix("zim://"), - searchProtocolPrefix("search://?"), - estimatedResultCount(searcher->getEstimatedResultCount()), - resultStart(searcher->getResultStart()) + : SearchRenderer( + searcher->getSearchResultSet(), + mapper, + nullptr, + searcher->getEstimatedResultCount(), + searcher->getResultStart()) +{} + +SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, + unsigned int start, unsigned int estimatedResultCount) + : SearchRenderer(srs, mapper, nullptr, start, estimatedResultCount) {} SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, Library* library, @@ -90,13 +95,13 @@ std::string SearchRenderer::getHtml() result.set("title", it.getTitle()); result.set("url", it.getPath()); result.set("snippet", it.getSnippet()); - std::ostringstream s; - s << it.getZimId(); - result.set("resultContentId", mp_nameMapper->getNameForId(s.str())); - std::shared_ptr archive; - try { - result.set("bookTitle", mp_library->getBookById(s.str()).getTitle()); - } catch (const std::out_of_range& e) {} + std::string zim_id(it.getZimId()); + result.set("resultContentId", mp_nameMapper->getNameForId(zim_id)); + if (!mp_library) { + result.set("bookTitle", kainjow::mustache::data(false)); + } else { + result.set("bookTitle", mp_library->getBookById(zim_id).getTitle()); + } if (it.getWordCount() >= 0) { result.set("wordCount", kiwix::beautifyInteger(it.getWordCount())); diff --git a/static/templates/search_result.html b/static/templates/search_result.html index 132348223..b52c9c3d3 100644 --- a/static/templates/search_result.html +++ b/static/templates/search_result.html @@ -125,9 +125,9 @@ {{#snippet}} {{>snippet}}... {{/snippet}} - {{#bookTitle}} -
from {{bookTitle}}
- {{/bookTitle}} + {{#bookTitle}} +
from {{bookTitle}}
+ {{/bookTitle}} {{#wordCount}}
{{wordCount}} words
{{/wordCount}}