Skip to content

Commit

Permalink
new search box
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jun 29, 2024
1 parent 0de8b85 commit d6236ae
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
__site/
node_modules/
Manifest.toml
.vscode/
26 changes: 26 additions & 0 deletions Learning/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Learning Materials

This page contains a series of materials for learning quantum information, many-body physics and scientific computing. These materials target students at the Hong Kong University of Science and Technology (Guangzhou) and are freely available to the public.

## Videos

~~~
<iframe width="560" height="315" src="https://www.youtube.com/embed/eGpDw8tVG3c?si=xOBU69a4aZ66fnd8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
~~~

## Books
- Quantum computation and quantum information
- Quantum computation and quantum information. Nielsen M A, Chuang I L. Cambridge university press, 2010.

- Computational complexity
- The nature of computation. Moore C, Mertens S. OUP Oxford, 2011.

- Scientific computing
- Matrix computations. Golub G H, Van Loan C F. JHU press, 2013.
- Exact exponential algorithms. Fedor V. Fomin , Dieter Kratsch. Springer, 2010.

## Learn Programming
The following resources are used for Julia programming language training:
- [Modern Julia Workflows](https://modernjuliaworkflows.github.io/)
- [Think Julia: How to Think Like a Computer Scientist](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html)
- [GitHub: JuliaComputing/Training](https://github.com/JuliaComputing/Training)
1 change: 1 addition & 0 deletions ReadingList/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Reading List
2 changes: 1 addition & 1 deletion TrainingCamps/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
title = "Past Events"
title = "Past Events"
+++

This page contains a series of materials for training students in quantum information, many-body physics and scientific computing. These materials target students at the Hong Kong University of Science and Technology (Guangzhou) and are freely available to the public.
Expand Down
1 change: 1 addition & 0 deletions _layout/foot.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ga('send', 'pageview');
</script>
<script async src=https://www.google-analytics.com/analytics.js></script>
<script src="/libs/lunr/lunr.min.js"></script>

</body>
</html>
11 changes: 11 additions & 0 deletions _layout/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ <h1>{{title}}</h1>
<br><br>
{{end}}{{end}}
<!-- Content appended here -->
<!doctype html>
<!-- first few lines ... -->
<script src="/libs/lunr/lunr.min.js"></script>
<script src="/libs/lunr/lunr_index.js"></script>
<script src="/libs/lunr/lunrclient.min.js"></script>
</head>
<!-- ... -->
<form id="lunrSearchForm" name="lunrSearchForm">
<input class="search-input" name="q" placeholder="Enter search term" type="text">
<input type="submit" value="Search" formaction="/search/index.html">
</form>
6 changes: 4 additions & 2 deletions _layout/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<nav class="navbar navbar-expand-lg navbar-light justify-content-center">
<ul class="navbar-nav">
<li class="nav-item {{ispage index}}active{{end}}"><a class="nav-link" href="/"><i data-feather="home"></i>Home</a></li>
<li class="nav-item {{ispage post/*}}active{{end}}"><a class="nav-link" href="/TrainingCamps/"><i data-feather="file-text"></i>Training Camps</a></li>
<li class="nav-item {{ispage post/*}}active{{end}}"><a class="nav-link" href="/NextEvent/"><i data-feather="book-open"></i>Next Events</a></li>
<li class="nav-item {{ispage ReadingList/*}}active{{end}}"><a class="nav-link" href="/ReadingList/"><i data-feather="file-text"></i>Reading</a></li>
<li class="nav-item {{ispage Learning/*}}active{{end}}"><a class="nav-link" href="/Learning/"><i data-feather="book-open"></i>Learning</a></li>
<li class="nav-item {{ispage TrainingCamps/*}}active{{end}}"><a class="nav-link" href="/TrainingCamps/"><i data-feather="file-text"></i>Training Camps</a></li>
<li class="nav-item {{ispage NextEvent/*}}active{{end}}"><a class="nav-link" href="/NextEvent/"><i data-feather="bell"></i>Next Events</a></li>
</ul>
</nav>
</div>
7 changes: 7 additions & 0 deletions _layout/style_adjustments.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@
margin: 0
}

.result-title a { text-decoration: none; }
.result-title a:hover { text-decoration: underline; }
.result-preview { color: #808080; }
.resultCount { color: #808080; }
.result-query { font-weight: bold; }
#lunrSearchForm { margin-top: 1em; }

</style>
103 changes: 103 additions & 0 deletions _libs/lunr/build_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
var path = require("path");
var fs = require("fs");
var lunr = require("lunr");
var cheerio = require("cheerio");

// don't modify this, it'll be modified on the fly by lunr() in Franklin
const PATH_PREPEND = "..";

const HTML_FOLDER = "../../__site";
const OUTPUT_INDEX = "lunr_index.js";

function isHtml(filename) {
lower = filename.toLowerCase();
return (lower.endsWith(".htm") || lower.endsWith(".html"));
}

function findHtml(folder) {
if (!fs.existsSync(folder)) {
console.log("Could not find folder: ", folder);
return;
}
var files = fs.readdirSync(folder);
var htmls = [];
for (var i = 0; i < files.length; i++) {
var filename = path.join(folder, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
if (stat == "assets" || stat == "css" || stat == "libs" ) {
continue
}
var recursed = findHtml(filename);
for (var j = 0; j < recursed.length; j++) {
recursed[j] = path.join(files[i], recursed[j]).replace(/\\/g, "/");
}
htmls.push.apply(htmls, recursed);
}
else if (isHtml(filename)){
htmls.push(files[i]);
};
};
return htmls;
};

function readHtml(root, file, fileId) {
var filename = path.join(root, file);
var txt = fs.readFileSync(filename).toString();
var $ = cheerio.load(txt);
var title = $("title").text();
if (typeof title == 'undefined') title = file;
var body = $("body").text()
if (typeof body == 'undefined') body = "";

var data = {
"id": fileId,
"l": filename,
"t": title,
"b": body
}
return data;
}

function buildIndex(docs) {
var idx = lunr(function () {
this.ref('id');
this.field('t'); // title
this.field('b'); // body
docs.forEach(function (doc) {
this.add(doc);
}, this);
});
return idx;
}

function buildPreviews(docs) {
var result = {};
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
result[doc["id"]] = {
"t": doc["t"],
"l": doc["l"].replace(/^\.\.\/\.\.\/__site/gi, '/' + PATH_PREPEND)
}
}
return result;
}

function main() {
files = findHtml(HTML_FOLDER);
var docs = [];
for (var i = 0; i < files.length; i++) {
docs.push(readHtml(HTML_FOLDER, files[i], i));
}
var idx = buildIndex(docs);
var prev = buildPreviews(docs);
var js = "const LUNR_DATA = " + JSON.stringify(idx) + ";\n" +
"const PREVIEW_LOOKUP = " + JSON.stringify(prev) + ";";
fs.writeFile(OUTPUT_INDEX, js, function(err) {
if(err) {
return console.log(err);
}
});
}

main();
1 change: 1 addition & 0 deletions _libs/lunr/lunr.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions _libs/lunr/lunr_index.js

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions _libs/lunr/lunrclient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// This file and its minified version is adapted from https://github.com/BLE-LTER/Lunr-Index-and-Search-for-Static-Sites which is unlicensed.
//

"use strict";

var LUNR_CONFIG = {
"resultsElementId": "searchResults", // Element to contain results
"countElementId": "resultCount" // Element showing number of results
};


// Get URL arguments
function getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}


// Parse search results into HTML
function parseLunrResults(results) {
var html = [];
for (var i = 0; i < results.length; i++) {
var id = results[i]["ref"];
var item = PREVIEW_LOOKUP[id]
var title = item["t"];
var preview = item["p"];
var link = item["l"].replace("__site/", "");
var result = ('<li><span class="result-title"><a href="' + link + '">'
+ title + '</a></span>');
html.push(result);
}
if (html.length) {
html.join("");
return '<ul>'+html+'</ul>'
}
else {
return "";
}
}


function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}


function showResultCount(total) {
var element = document.getElementById(LUNR_CONFIG["countElementId"])
if (element !== null) {
element.innerHTML = total + ".";
}
}


function searchLunr(query) {
var idx = lunr.Index.load(LUNR_DATA);
// Write results to page
var results = idx.search(query);
var resultHtml = parseLunrResults(results);
var elementId = LUNR_CONFIG["resultsElementId"];
document.getElementById(elementId).innerHTML = resultHtml;
// Write the number of results
showResultCount(results.length);
}


// When the window loads, read query parameters and perform search
window.onload = function() {
var query = getParameterByName("q");
if (query != "" && query != null) {
document.forms.lunrSearchForm.q.value = query;
searchLunr(query);
} else {
// empty query: show 0 results (no query)
showResultCount("0 (empty query)");
}
document.getElementById("focus").focus();
};
1 change: 1 addition & 0 deletions _libs/lunr/lunrclient.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@def title = "Search ⋅ Quantum Bay"

## Search

Number of results found: ~~~<span id="resultCount"></span>~~~

~~~
<div id="searchResults"></div>
~~~

0 comments on commit d6236ae

Please sign in to comment.