From 633fafd4f50a7dc773aeaa10806525b68ffebae9 Mon Sep 17 00:00:00 2001 From: Austin Wise Date: Wed, 2 Feb 2022 00:11:10 -0800 Subject: [PATCH] Add pagination --- src/requests.rs | 29 +++++++++++++++++++++++------ src/templates.rs | 11 ++++++++++- src/wiki.rs | 19 +++++++++++++++---- templates/search_results.html | 34 ++++++++++++++++++++++++++-------- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/requests.rs b/src/requests.rs index c36ad24..7713d2d 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -285,15 +285,32 @@ fn overview(path: WikiPagePath, w: Wiki) -> Result Result, MyError> { - let results = w.search(q)?; - let html = render_search_results(q, results)?; +fn search_inner( + q: &str, + offset: Option, + w: Wiki, +) -> Result, 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?")] -fn search(q: &str, w: Wiki) -> Result, MyError> { - search_inner(q, w) +#[get("/search?&")] +fn search( + q: &str, + offset: Option, + w: Wiki, +) -> Result, MyError> { + search_inner(q, offset, w) } #[get("/")] diff --git a/src/templates.rs b/src/templates.rs index d3e9cf2..ab0629b 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -166,9 +166,16 @@ struct SearchResultsTemplate<'a> { query: &'a str, documents: Vec, breadcrumbs: Vec>, + prev_url: Option, + next_url: Option, } -pub fn render_search_results(query: &str, documents: Vec) -> askama::Result { +pub fn render_search_results( + query: &str, + documents: Vec, + prev_url: Option, + next_url: Option, +) -> askama::Result { let primer_css_uri = &primer_css_uri(); let favicon_png_uri = &favicon_png_uri(); let breadcrumbs = vec![]; @@ -179,6 +186,8 @@ pub fn render_search_results(query: &str, documents: Vec) -> askam query, breadcrumbs, documents, + prev_url, + next_url, }; template.render() } diff --git a/src/wiki.rs b/src/wiki.rs index bd0bad0..8561fd4 100644 --- a/src/wiki.rs +++ b/src/wiki.rs @@ -125,14 +125,16 @@ fn create_index(settings: &Settings, repository: &RepoBox) -> Result 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(""); + result.push_str( + "", + ); result.push_str(&snippet.fragments()[fragment_range.clone()]); result.push_str(""); start_from = fragment_range.end; @@ -177,7 +179,12 @@ impl Wiki { self.0.repository.enumerate_files(directory) } - pub fn search(&self, query: &str) -> Result, MyError> { + pub fn search( + &self, + query: &str, + num_results: usize, + offset: Option, + ) -> Result, MyError> { let reader = self .0 .index @@ -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)?; diff --git a/templates/search_results.html b/templates/search_results.html index f2ec6c3..c7ff690 100644 --- a/templates/search_results.html +++ b/templates/search_results.html @@ -2,12 +2,12 @@ {% block extra_scripts %} {% endblock %} @@ -25,8 +25,8 @@

  • {{doc.score}} - {{doc.title}} - {{doc.path}} + {{doc.title}} - + {{doc.path}}
  • {{doc.snippet_html}} @@ -35,4 +35,22 @@

    + + {% endblock %} \ No newline at end of file