From aad95e34130bbac9d773ca45f9593a90154ba8f3 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 23 Mar 2022 12:01:39 +0100 Subject: [PATCH] Introduce a results intermediate object in the template rendering. Url in href must not be html encoded. As we already url encode the path, it is ok to have `'` in the url. --- src/search_renderer.cpp | 33 +++++++++++------------------ static/templates/search_result.html | 18 ++++++++-------- test/server.cpp | 8 +++---- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/search_renderer.cpp b/src/search_renderer.cpp index 0381d424d..8f9685563 100644 --- a/src/search_renderer.cpp +++ b/src/search_renderer.cpp @@ -164,28 +164,28 @@ kainjow::mustache::data buildPagination( std::string SearchRenderer::getHtml() { // Build the results list - kainjow::mustache::data results{kainjow::mustache::data::type::list}; - + kainjow::mustache::data items{kainjow::mustache::data::type::list}; for (auto it = m_srs.begin(); it != m_srs.end(); it++) { kainjow::mustache::data result; - result.set("title", it.getTitle()); - result.set("url", it.getPath()); - result.set("snippet", it.getSnippet()); 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("title", it.getTitle()); + result.set("absolutePath", protocolPrefix + urlEncode(mp_nameMapper->getNameForId(zim_id), true) + "/" + urlEncode(it.getPath())); + result.set("snippet", it.getSnippet()); + if (mp_library) { result.set("bookTitle", mp_library->getBookById(zim_id).getTitle()); } - if (it.getWordCount() >= 0) { result.set("wordCount", kiwix::beautifyInteger(it.getWordCount())); } - results.push_back(result); + items.push_back(result); } - + kainjow::mustache::data results; + results.set("items", items); + results.set("count", kiwix::beautifyInteger(estimatedResultCount)); + results.set("hasResults", estimatedResultCount != 0); + results.set("start", kiwix::beautifyInteger(resultStart+1)); + results.set("end", kiwix::beautifyInteger(min(resultStart+pageLength, estimatedResultCount))); // pagination auto pagination = buildPagination( @@ -194,26 +194,17 @@ std::string SearchRenderer::getHtml() resultStart ); - auto resultEnd = min(resultStart+pageLength, estimatedResultCount); - kainjow::mustache::data query = buildQueryData( searchProtocolPrefix, searchPattern, searchContent ); - - std::string template_str = RESOURCE::templates::search_result_html; kainjow::mustache::mustache tmpl(template_str); kainjow::mustache::data allData; allData.set("results", results); - allData.set("hasResults", estimatedResultCount != 0); - allData.set("count", kiwix::beautifyInteger(estimatedResultCount)); - allData.set("resultStart", to_string(resultStart + 1)); - allData.set("resultEnd", to_string(resultEnd)); - allData.set("protocolPrefix", this->protocolPrefix); allData.set("pagination", pagination); allData.set("query", query); diff --git a/static/templates/search_result.html b/static/templates/search_result.html index cca32af1d..93133c584 100644 --- a/static/templates/search_result.html +++ b/static/templates/search_result.html @@ -106,26 +106,26 @@
- {{#hasResults}} + {{#results.hasResults}} Results - {{resultStart}}-{{resultEnd}} + {{results.start}}-{{results.end}} of - {{count}} + {{results.count}} for "{{{query.pattern}}}" - {{/hasResults}} - {{^hasResults}} + {{/results.hasResults}} + {{^results.hasResults}} No results were found for "{{{query.pattern}}}" - {{/hasResults}} + {{/results.hasResults}}
diff --git a/test/server.cpp b/test/server.cpp index 2a61db537..4054a49ba 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -1158,7 +1158,7 @@ R"SEARCHRESULT( )SEARCHRESULT", R"SEARCHRESULT( - + Catchin' Some Rays: The Music of Ray Charles ...jazz singer Roseanna Vitro, released in August 1997 on the Telarc Jazz label. Catchin' Some Rays: The Music of Ray Charles Studio album by Roseanna Vitro Released August 1997 Recorded March 26, 1997 at Sound on Sound, NYC April 4,1997 at Quad Recording Studios, NYC Genre Vocal jazz Length 61:00 Label Telarc Jazz CD-83419 Producer Paul Wickliffe Roseanna Vitro chronology Passion Dance (1996) Catchin' Some Rays: The Music of Ray Charles (1997) The Time of My Life: Roseanna Vitro Sings the Songs of...... @@ -1167,7 +1167,7 @@ R"SEARCHRESULT( )SEARCHRESULT", R"SEARCHRESULT( - + That's What I Say: John Scofield Plays the Music of Ray Charles That's What I Say: John Scofield Plays the Music of Ray Charles Studio album by John Scofield Released June 7, 2005 (2005-06-07) Recorded December 2004 Studio Avatar Studios, New York City Genre Jazz Length 65:21 Label Verve Producer Steve Jordan John Scofield chronology EnRoute: John Scofield Trio LIVE (2004) That's What I Say: John Scofield Plays the Music of Ray Charles (2005) Out Louder (2006) Professional ratings Review scores Source Rating Allmusic All About Jazz All About Jazz... @@ -1437,7 +1437,7 @@ R"SEARCHRESULT( )SEARCHRESULT", R"SEARCHRESULT( - + Don't Let the Sun Catch You Cryin' ...R&B Sides" and No. 95 on the Billboard Hot 100. It was also recorded by Jackie DeShannon on her 1965 album This is Jackie De Shannon, Paul McCartney on his 1990 live album Tripping the Live Fantastic, Jex Saarelaht and Kate Ceberano on their album Open the Door - Live at Mietta's (1992) and jazz singer Roseanna Vitro on her 1997 album Catchin’ Some Rays: The Music of Ray Charles. Karin Krog and Steve Kuhn include it on their 2005 album, Together Again. Steve Alaimo released a version in 1963... @@ -1446,7 +1446,7 @@ R"SEARCHRESULT( )SEARCHRESULT", R"SEARCHRESULT( - + I Don't Need No Doctor ...jazz guitar player John Scofield recorded a version for his album That's What I Say: John Scofield Plays the Music of Ray Charles in 2005, featuring the blues guitarist John Mayer on additional guitar and vocals. Mayer covered the song again with his band during his tour in summer 2007. A recorded live version from a Los Angeles show during that tour is available on Mayer's CD/DVD release Where the Light Is. A Ray Charles tribute album also provided the impetus for jazz singer Roseanna Vitro's......