Repository navigation
Expand file tree
/
Copy pathsearch.html
More file actions
85 lines (79 loc) · 3.1 KB
/
Copy pathsearch.html
File metadata and controls
85 lines (79 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
layout: default
title: Search
permalink: /search/
sitemap: false
---
<div class="container">
<h1>Search</h1>
<p>Search the user guide, the tutorials and the V&V pages. All terms of two or more characters must appear on the page; shorter terms are ignored.</p>
<label for="search-box" class="sr-only">Search documentation</label>
<input id="search-box" type="search" class="form-control" placeholder="For example: CFL_ADAPT, wall function, transition" autofocus>
<p id="search-count" role="status"></p>
<ol id="search-results"></ol>
</div>
<script>
(function () {
var box = document.getElementById("search-box");
var count = document.getElementById("search-count");
var list = document.getElementById("search-results");
var pages = null;
var loadError = false;
var names = { docs_v7: "Docs", tutorials: "Tutorials", vandv: "V&V" };
function escapeHtml(s) {
return s.replace(/[&<>"]/g, function (c) { return { "&": "&", "<": "<", ">": ">", '"': """ }[c]; });
}
function snippet(text, word) {
var i = text.toLowerCase().indexOf(word);
if (i < 0) return escapeHtml(text.slice(0, 160));
var start = Math.max(0, i - 80);
return (start > 0 ? "..." : "") + escapeHtml(text.slice(start, i + 120)) + "...";
}
function run() {
var words = box.value.toLowerCase().split(/\s+/).filter(function (w) { return w.length > 1; });
list.innerHTML = "";
if (!pages) {
count.textContent = loadError ? "Search could not load. Reload this page to try again." : "Loading search...";
return;
}
if (words.length === 0) {
count.textContent = box.value.trim() ? "Enter a search term with at least two characters." : "";
return;
}
var hits = [];
pages.forEach(function (p) {
var title = p.title.toLowerCase(), text = p.text.toLowerCase(), score = 0;
for (var k = 0; k < words.length; k++) {
var inTitle = title.indexOf(words[k]) >= 0, inText = text.indexOf(words[k]) >= 0;
if (!inTitle && !inText) return;
score += (inTitle ? 20 : 0) + Math.min(text.split(words[k]).length - 1, 10);
}
hits.push({ page: p, score: score });
});
hits.sort(function (a, b) { return b.score - a.score; });
count.textContent = hits.length + (hits.length === 1 ? " page found." : " pages found.");
hits.slice(0, 50).forEach(function (h) {
var li = document.createElement("li");
li.innerHTML = '<a href="' + h.page.url + '"><b>' + escapeHtml(h.page.title) + "</b></a> (" +
names[h.page.section] + ")<br><small>" + snippet(h.page.text, words[0]) + "</small>";
list.appendChild(li);
});
}
var q = new URLSearchParams(location.search).get("q");
if (q) box.value = q;
run();
fetch("{{ '/search.json' | relative_url }}").then(function (r) {
if (!r.ok) throw new Error("Search index request failed");
return r.json();
}).then(function (data) {
if (!Array.isArray(data)) throw new Error("Invalid search index");
pages = data;
run();
}).catch(function () {
pages = null;
loadError = true;
run();
});
box.addEventListener("input", run);
})();
</script>