From e5df5e936f2c5127ca9ee76fcf4dafb0263928a4 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 4 May 2022 18:39:54 +0200 Subject: [PATCH] Render the search result using (opensearch/atom) xml format. --- include/search_renderer.h | 8 +++++++ src/search_renderer.cpp | 18 ++++++++++++--- src/server/internalServer.cpp | 5 ++++ static/resources_list.txt | 1 + static/templates/search_result.xml | 37 ++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 static/templates/search_result.xml diff --git a/include/search_renderer.h b/include/search_renderer.h index 22dccef1f..1e1fa9596 100644 --- a/include/search_renderer.h +++ b/include/search_renderer.h @@ -102,11 +102,19 @@ class SearchRenderer this->pageLength = pageLength; } + std::string renderTemplate(const std::string& tmpl_str); + /** * Generate the html page with the resutls of the search. */ std::string getHtml(); + /** + * Generate the xml page with the resutls of the search. + */ + std::string getXml(); + + protected: std::string beautifyInteger(const unsigned int number); zim::SearchResultSet m_srs; diff --git a/src/search_renderer.cpp b/src/search_renderer.cpp index f5bf15447..5526d9162 100644 --- a/src/search_renderer.cpp +++ b/src/search_renderer.cpp @@ -162,7 +162,7 @@ kainjow::mustache::data buildPagination( return pagination; } -std::string SearchRenderer::getHtml() +std::string SearchRenderer::renderTemplate(const std::string& tmpl_str) { // Build the results list kainjow::mustache::data items{kainjow::mustache::data::type::list}; @@ -201,14 +201,15 @@ std::string SearchRenderer::getHtml() searchBookQuery ); - std::string template_str = RESOURCE::templates::search_result_html; - kainjow::mustache::mustache tmpl(template_str); kainjow::mustache::data allData; + allData.set("protocolPrefix", protocolPrefix); allData.set("results", results); allData.set("pagination", pagination); allData.set("query", query); + kainjow::mustache::mustache tmpl(tmpl_str); + std::stringstream ss; tmpl.render(allData, [&ss](const std::string& str) { ss << str; }); if (!tmpl.is_valid()) { @@ -217,4 +218,15 @@ std::string SearchRenderer::getHtml() return ss.str(); } +std::string SearchRenderer::getHtml() +{ + return renderTemplate(RESOURCE::templates::search_result_html); +} + +std::string SearchRenderer::getXml() +{ + return renderTemplate(RESOURCE::templates::search_result_xml); +} + + } diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 772f6a4da..97fdae056 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -769,6 +769,11 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re renderer.setProtocolPrefix(m_root + "/"); renderer.setSearchProtocolPrefix(m_root + "/search"); renderer.setPageLength(pageLength); + if (request.get_optional_param("format", "") == "xml") { + return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8", + /*isHomePage =*/false, + /*raw =*/true); + } auto response = ContentResponse::build(*this, renderer.getHtml(), "text/html; charset=utf-8"); if(bookIds.size() == 1) { auto bookId = *bookIds.begin(); diff --git a/static/resources_list.txt b/static/resources_list.txt index bc1eb1af3..3b7506c06 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -34,6 +34,7 @@ skin/fonts/Roboto.ttf skin/block_external.js skin/search_results.css templates/search_result.html +templates/search_result.xml templates/error.html templates/index.html templates/suggestion.json diff --git a/static/templates/search_result.xml b/static/templates/search_result.xml new file mode 100644 index 000000000..d418a0a0d --- /dev/null +++ b/static/templates/search_result.xml @@ -0,0 +1,37 @@ + + + + Search: {{query.pattern}} + {{query.unpaginatedQuery}}&format=xml&start={{results.start}}&pageLength={{pagination.itemsPerPage}} + Search result for {{query.pattern}} + {{results.count}} + {{results.start}} + {{pagination.itemsPerPage}} + +