Skip to content

Commit 074ecd1

Browse files
committed
adding search
1 parent 9a23ce8 commit 074ecd1

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ layout: default
33
title: {{ site.name }}
44
pdf: true
55
permalink: /
6+
excluded_in_search: true
67
---
78

89
<div style="float:right; margin-bottom:50px; color:#666">
@@ -16,7 +17,6 @@ permalink: /
1617
# Singularity Python
1718
Welcome to the Singularity Python documentation!
1819

19-
2020
## What is Singularity Python
2121
Singularity Python is a Python API to work with the <a href="https://singularityware.github.io" target="_blank">Singularity</a> open source software. Are you a scientist or developer? You likely will be most interested in the ability to interact with Singularity containers from your own Python scripts. See <a href="https://singularityhub.github.io/singularity-cli/api/source/spython.main.html" target="_blank">here</a> for the core module docstrings.
2222

docs/_includes/page-header.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<section class="page-header">
2-
<a href="{{ site.baseurl }}/"><h2 class="project-name">{{ site.title }}</h2></a>
2+
<a href="{{ site.baseurl }}/"><span class="project-name">{{ site.title }}</span> <span style="float:right"> {% include search.html %}</span></a>
33
</section>

docs/_includes/search.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<form action="{{ site.baseurl }}/search" method="get">
2+
<input type="search" name="q" id="search-input" placeholder="What would you like to know?" style="margin-top:5px" autofocus>
3+
<i style="color:white; margin-right:8px; margin-left:5px" class="fa fa-search"></i>
4+
<input type="submit" value="Search" style="display: none;">
5+
</form>

docs/js/lunr.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/js/search.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
layout: null
3+
---
4+
(function () {
5+
function getQueryVariable(variable) {
6+
var query = window.location.search.substring(1),
7+
vars = query.split("&");
8+
9+
for (var i = 0; i < vars.length; i++) {
10+
var pair = vars[i].split("=");
11+
12+
if (pair[0] === variable) {
13+
return decodeURIComponent(pair[1].replace(/\+/g, '%20')).trim();
14+
}
15+
}
16+
}
17+
18+
function getPreview(query, content, previewLength) {
19+
previewLength = previewLength || (content.length * 2);
20+
21+
var parts = query.split(" "),
22+
match = content.toLowerCase().indexOf(query.toLowerCase()),
23+
matchLength = query.length,
24+
preview;
25+
26+
// Find a relevant location in content
27+
for (var i = 0; i < parts.length; i++) {
28+
if (match >= 0) {
29+
break;
30+
}
31+
32+
match = content.toLowerCase().indexOf(parts[i].toLowerCase());
33+
matchLength = parts[i].length;
34+
}
35+
36+
// Create preview
37+
if (match >= 0) {
38+
var start = match - (previewLength / 2),
39+
end = start > 0 ? match + matchLength + (previewLength / 2) : previewLength;
40+
41+
preview = content.substring(start, end).trim();
42+
43+
if (start > 0) {
44+
preview = "..." + preview;
45+
}
46+
47+
if (end < content.length) {
48+
preview = preview + "...";
49+
}
50+
51+
// Highlight query parts
52+
preview = preview.replace(new RegExp("(" + parts.join("|") + ")", "gi"), "<strong>$1</strong>");
53+
} else {
54+
// Use start of content if no match found
55+
preview = content.substring(0, previewLength).trim() + (content.length > previewLength ? "..." : "");
56+
}
57+
58+
return preview;
59+
}
60+
61+
function displaySearchResults(results, query) {
62+
var searchResultsEl = document.getElementById("search-results"),
63+
searchProcessEl = document.getElementById("search-process");
64+
65+
if (results.length) {
66+
var resultsHTML = "";
67+
results.forEach(function (result) {
68+
var item = window.data[result.ref],
69+
contentPreview = getPreview(query, item.content, 170),
70+
titlePreview = getPreview(query, item.title);
71+
72+
resultsHTML += "<li><h4><a href='{{ site.baseurl }}" + item.url.trim() + "'>" + titlePreview + "</a></h4><p><small>" + contentPreview + "</small></p></li>";
73+
});
74+
75+
searchResultsEl.innerHTML = resultsHTML;
76+
searchProcessEl.innerText = "Showing";
77+
} else {
78+
searchResultsEl.style.display = "none";
79+
searchProcessEl.innerText = "No";
80+
}
81+
}
82+
83+
window.index = lunr(function () {
84+
this.field("id");
85+
this.field("title", {boost: 10});
86+
this.field("categories");
87+
this.field("url");
88+
this.field("content");
89+
});
90+
91+
var query = decodeURIComponent((getQueryVariable("q") || "").replace(/\+/g, "%20")),
92+
searchQueryContainerEl = document.getElementById("search-query-container"),
93+
searchQueryEl = document.getElementById("search-query");
94+
95+
searchQueryEl.innerText = query;
96+
searchQueryContainerEl.style.display = "inline";
97+
98+
for (var key in window.data) {
99+
window.index.add(window.data[key]);
100+
}
101+
102+
displaySearchResults(window.index.search(query), query); // Hand the results off to be displayed
103+
})();

0 commit comments

Comments
 (0)