Merge pull request #718 from kiwix/fix_booktitle_renderer

Fix search rendered for book title
This commit is contained in:
Kelson 2022-03-04 21:30:37 +01:00 committed by GitHub
commit cfab560d74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 18 deletions

View File

@ -49,10 +49,24 @@ class SearchRenderer
/** /**
* Construct a SearchRenderer from a SearchResultSet. * 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 srs The `SearchResultSet` to render.
* @param mapper The `NameMapper` to use to do the rendering. * @param mapper The `NameMapper` to use to do the rendering.
* @param library The `Library` to use to look up book details for search * @param start The start offset used for the srs.
* results * @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 start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search * @param estimatedResultCount The estimatedResultCount of the whole search
*/ */

View File

@ -39,12 +39,17 @@ namespace kiwix
/* Constructor */ /* Constructor */
SearchRenderer::SearchRenderer(Searcher* searcher, NameMapper* mapper) SearchRenderer::SearchRenderer(Searcher* searcher, NameMapper* mapper)
: m_srs(searcher->getSearchResultSet()), : SearchRenderer(
mp_nameMapper(mapper), searcher->getSearchResultSet(),
protocolPrefix("zim://"), mapper,
searchProtocolPrefix("search://?"), nullptr,
estimatedResultCount(searcher->getEstimatedResultCount()), searcher->getEstimatedResultCount(),
resultStart(searcher->getResultStart()) 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, SearchRenderer::SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, Library* library,
@ -90,13 +95,13 @@ std::string SearchRenderer::getHtml()
result.set("title", it.getTitle()); result.set("title", it.getTitle());
result.set("url", it.getPath()); result.set("url", it.getPath());
result.set("snippet", it.getSnippet()); result.set("snippet", it.getSnippet());
std::ostringstream s; std::string zim_id(it.getZimId());
s << it.getZimId(); result.set("resultContentId", mp_nameMapper->getNameForId(zim_id));
result.set("resultContentId", mp_nameMapper->getNameForId(s.str())); if (!mp_library) {
std::shared_ptr<zim::Archive> archive; result.set("bookTitle", kainjow::mustache::data(false));
try { } else {
result.set("bookTitle", mp_library->getBookById(s.str()).getTitle()); result.set("bookTitle", mp_library->getBookById(zim_id).getTitle());
} catch (const std::out_of_range& e) {} }
if (it.getWordCount() >= 0) { if (it.getWordCount() >= 0) {
result.set("wordCount", kiwix::beautifyInteger(it.getWordCount())); result.set("wordCount", kiwix::beautifyInteger(it.getWordCount()));