cavepedia/src/templates/results.html.tmpl

68 lines
2.3 KiB
Cheetah

<!DOCTYPE html>
<html>
<head>
<title>Cavepedia</title>
<link rel="stylesheet" href="/static/cavepedia.css">
</head>
<body>
<a href="/search?placeholder={{.Term}}">&lt-- Back to Search</a>
<br>
{{$numResults := .Results.Len}}
{{$term := .Term}}
<h2 id="results">{{$numResults}} results for {{$term}}</h2>
<h4>(Only exact matches are highlighted)</h4>
{{if not .Results}}
<h3>No Results :(</h3>
{{end}}
{{range $i, $r := .Results}}
{{$group := index .Fields "group"}}
{{$category := index .Fields "category"}}
{{$filename := index .Fields "file"}}
{{$pageNum := index .Fields "pageNum"}}
{{$season := index .Fields "season"}}
<div id="{{$i}}">
<h3><a id="pdf_{{$i}}" href="/pdf?file=/files/{{$group}}/{{$category}}/{{$filename}}&category={{$category}}&filename={{$filename}}&season={{$season}}&pageNum={{$pageNum}}&term={{$term}}">
{{$filename}}</a>
</h3>
<strong id="id_{{$i}}">Result {{$i}} of {{$numResults}}</strong> |
<strong>Category:</strong> {{index .Fields "category"}} |
<strong>Season:</strong> {{index .Fields "season"}} |
<strong>Page Number:</strong> {{index .Fields "pageNum"}}
<p id="text_{{$i}}">{{index .Fields "text"}}<p>
</div>
{{end}}
<script>
const numResults = parseInt({{$numResults}});
const term = {{$term}};
const lower = term.toLowerCase();
const upper = term.toUpperCase();
const capitalize = term.charAt(0).toUpperCase() + term.slice(1);
for (i = 0; i < numResults; i++) {
// Highlight text
let newText = document.getElementById(`text_${i}`).innerHTML;
newText = newText.replace(lower, `<mark>${lower}</mark>`)
newText = newText.replace(upper, `<mark>${upper}</mark>`)
newText = newText.replace(capitalize, `<mark>${capitalize}</mark>`)
document.getElementById(`text_${i}`).innerHTML = newText;
// Set result number
document.getElementById(`id_${i}`).innerHTML = `Result ${i+1} of {{$numResults}}`;
// Link directly to PDF if mobile (doesn't support PDF natively)
if (window.matchMedia("(any-pointer: coarse)").matches) {
document.getElementById(`pdf_${i}`).href = document.getElementById(`pdf_${i}`).href.split('?')[1].split('&')[0].split('=')[1];
}
}
</script>
</body>
</html>