Skip to content

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWise committed Feb 2, 2022
1 parent d855a6d commit 633fafd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
29 changes: 23 additions & 6 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,32 @@ fn overview(path: WikiPagePath, w: Wiki) -> Result<response::content::Html<Strin
overview_inner(path, w)
}

fn search_inner(q: &str, w: Wiki) -> Result<response::content::Html<String>, MyError> {
let results = w.search(q)?;
let html = render_search_results(q, results)?;
fn search_inner(
q: &str,
offset: Option<usize>,
w: Wiki,
) -> Result<response::content::Html<String>, MyError> {
const RESULTS_PER_PAGE: usize = 10;
let results = w.search(q, RESULTS_PER_PAGE, offset)?;
let prev_url = offset.and_then(|v| {
if v >= RESULTS_PER_PAGE {
Some(uri!(search(q, Some(v - RESULTS_PER_PAGE))).to_string())
} else {
None
}
});
let next_url = Some(uri!(search(q, Some(offset.unwrap_or(0) + RESULTS_PER_PAGE))).to_string());
let html = render_search_results(q, results, prev_url, next_url)?;
Ok(response::content::Html(html))
}

#[get("/search?<q>")]
fn search(q: &str, w: Wiki) -> Result<response::content::Html<String>, MyError> {
search_inner(q, w)
#[get("/search?<q>&<offset>")]
fn search(
q: &str,
offset: Option<usize>,
w: Wiki,
) -> Result<response::content::Html<String>, MyError> {
search_inner(q, offset, w)
}

#[get("/")]
Expand Down
11 changes: 10 additions & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,16 @@ struct SearchResultsTemplate<'a> {
query: &'a str,
documents: Vec<SearchResult>,
breadcrumbs: Vec<Breadcrumb<'a>>,
prev_url: Option<String>,
next_url: Option<String>,
}

pub fn render_search_results(query: &str, documents: Vec<SearchResult>) -> askama::Result<String> {
pub fn render_search_results(
query: &str,
documents: Vec<SearchResult>,
prev_url: Option<String>,
next_url: Option<String>,
) -> askama::Result<String> {
let primer_css_uri = &primer_css_uri();
let favicon_png_uri = &favicon_png_uri();
let breadcrumbs = vec![];
Expand All @@ -179,6 +186,8 @@ pub fn render_search_results(query: &str, documents: Vec<SearchResult>) -> askam
query,
breadcrumbs,
documents,
prev_url,
next_url,
};
template.render()
}
19 changes: 15 additions & 4 deletions src/wiki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ fn create_index(settings: &Settings, repository: &RepoBox) -> Result<Index, MyEr
Ok(index)
}

// TODO: this does not belong at all in the Wiki, it belongs more in request handeling
// TODO: this does not belong at all in the Wiki, it belongs more in request handling
fn highlight(snippet: Snippet) -> String {
let mut result = String::new();
let mut start_from = 0;

for fragment_range in snippet.highlighted() {
result.push_str(&snippet.fragments()[start_from..fragment_range.start]);
result.push_str("<span class=\"color-bg-accent-emphasis color-fg-on-emphasis p-1 rounded mb-4\">");
result.push_str(
"<span class=\"color-bg-accent-emphasis color-fg-on-emphasis p-1 rounded mb-4\">",
);
result.push_str(&snippet.fragments()[fragment_range.clone()]);
result.push_str("</span>");
start_from = fragment_range.end;
Expand Down Expand Up @@ -177,7 +179,12 @@ impl Wiki {
self.0.repository.enumerate_files(directory)
}

pub fn search(&self, query: &str) -> Result<Vec<SearchResult>, MyError> {
pub fn search(
&self,
query: &str,
num_results: usize,
offset: Option<usize>,
) -> Result<Vec<SearchResult>, MyError> {
let reader = self
.0
.index
Expand All @@ -192,7 +199,11 @@ impl Wiki {

let query = query_parser.parse_query(query)?;

let top_docs = searcher.search(&query, &TopDocs::with_limit(10))?;
let mut top_docs = TopDocs::with_limit(num_results);
if let Some(offset) = offset {
top_docs = top_docs.and_offset(offset);
}
let top_docs = searcher.search(&query, &top_docs)?;

let snippet_generator = SnippetGenerator::create(&searcher, &*query, fields.body)?;

Expand Down
34 changes: 26 additions & 8 deletions templates/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

{% block extra_scripts %}
<script>
window.addEventListener('DOMContentLoaded', (event) => {
// Place the current search query back into the search box
let params = (new URL(document.location)).searchParams
let input = document.getElementById('search-query');
input.value = params.get('q');
});
window.addEventListener('DOMContentLoaded', (event) => {
// Place the current search query back into the search box
let params = (new URL(document.location)).searchParams
let input = document.getElementById('search-query');
input.value = params.get('q');
});
</script>
{% endblock %}

Expand All @@ -25,8 +25,8 @@ <h1 class="pt-4">
<li class="Box-row">
<span class="Counter Counter--gray tooltipped tooltipped-w"
aria-label="score: {{doc.score}}">{{doc.score}}</span>
<span class="text-bold"><a href="/page{{doc.path}}">{{doc.title}}</a></span> <span
class="text-gray-light">- {{doc.path}}</span>
<span class="text-bold"><a href="/page{{doc.path}}">{{doc.title}}</a></span> <span class="text-gray-light">-
{{doc.path}}</span>
</li>
<div class="Box-row">
{{doc.snippet_html}}
Expand All @@ -35,4 +35,22 @@ <h1 class="pt-4">
</ul>
</div>

<nav class="paginate-container" aria-label="Pagination">
<div class="pagination">
{% match prev_url %}
{% when Some with (url) %}
<a class="previous_page" rel="prev" href="{{url}}" aria-label="Previous Page">Previous</a>
{% when None %}
<span class="previous_page" aria-disabled="true">Previous</span>
{% endmatch %}

{% match next_url %}
{% when Some with (url) %}
<a class="next_page" rel="next" href="{{url}}" aria-label="Next Page">Next</a>
{% when None %}
<span class="next_page" aria-disabled="true">Next</span>
{% endmatch %}
</div>
</nav>

{% endblock %}

0 comments on commit 633fafd

Please sign in to comment.