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.
This commit is contained in:
Matthieu Gautier 2022-03-23 12:01:39 +01:00
parent f0dd34b6db
commit aad95e3413
3 changed files with 25 additions and 34 deletions

View File

@ -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);

View File

@ -106,26 +106,26 @@
</head>
<body bgcolor="white">
<div class="header">
{{#hasResults}}
{{#results.hasResults}}
Results
<b>
{{resultStart}}-{{resultEnd}}
{{results.start}}-{{results.end}}
</b> of <b>
{{count}}
{{results.count}}
</b> for <b>
"{{{query.pattern}}}"
</b>
{{/hasResults}}
{{^hasResults}}
{{/results.hasResults}}
{{^results.hasResults}}
No results were found for <b>"{{{query.pattern}}}"</b>
{{/hasResults}}
{{/results.hasResults}}
</div>
<div class="results">
<ul>
{{#results}}
{{#results.items}}
<li>
<a href="{{protocolPrefix}}{{resultContentId}}/{{url}}">
<a href="{{{absolutePath}}}">
{{title}}
</a>
{{#snippet}}
@ -138,7 +138,7 @@
<div class="informations">{{wordCount}} words</div>
{{/wordCount}}
</li>
{{/results}}
{{/results.items}}
</ul>
</div>

View File

@ -1158,7 +1158,7 @@ R"SEARCHRESULT(
)SEARCHRESULT",
R"SEARCHRESULT(
<a href="/ROOT/zimfile/A/Catchin&apos;_Some_Rays:_The_Music_of_Ray_Charles">
<a href="/ROOT/zimfile/A/Catchin'_Some_Rays:_The_Music_of_Ray_Charles">
Catchin&apos; Some Rays: The Music of Ray Charles
</a>
<cite>...<b>jazz</b> singer Roseanna Vitro, released in August 1997 on the Telarc <b>Jazz</b> 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 <b>jazz</b> Length 61:00 Label Telarc <b>Jazz</b> 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......</cite>
@ -1167,7 +1167,7 @@ R"SEARCHRESULT(
)SEARCHRESULT",
R"SEARCHRESULT(
<a href="/ROOT/zimfile/A/That&apos;s_What_I_Say:_John_Scofield_Plays_the_Music_of_Ray_Charles">
<a href="/ROOT/zimfile/A/That's_What_I_Say:_John_Scofield_Plays_the_Music_of_Ray_Charles">
That&apos;s What I Say: John Scofield Plays the Music of Ray Charles
</a>
<cite>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 <b>Jazz</b> 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 <b>Jazz</b> All About <b>Jazz</b>...</cite>
@ -1437,7 +1437,7 @@ R"SEARCHRESULT(
)SEARCHRESULT",
R"SEARCHRESULT(
<a href="/ROOT/zimfile/A/Don&apos;t_Let_the_Sun_Catch_You_Cryin&apos;">
<a href="/ROOT/zimfile/A/Don't_Let_the_Sun_Catch_You_Cryin'">
Don&apos;t Let the Sun Catch You Cryin&apos;
</a>
<cite>...R&amp;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 <b>jazz</b> 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...</cite>
@ -1446,7 +1446,7 @@ R"SEARCHRESULT(
)SEARCHRESULT",
R"SEARCHRESULT(
<a href="/ROOT/zimfile/A/I_Don&apos;t_Need_No_Doctor">
<a href="/ROOT/zimfile/A/I_Don't_Need_No_Doctor">
I Don&apos;t Need No Doctor
</a>
<cite>...<b>jazz</b> 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 <b>jazz</b> singer Roseanna Vitro's......</cite>