Add search feature using grep.
This commit is contained in:
41
src/main.rs
41
src/main.rs
@@ -1,7 +1,7 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
extern crate historian;
|
||||
use historian::{Historian, Page, Edit, PageRenderer, export_wiki, Linker};
|
||||
use historian::{Historian, Page, Edit, PageRenderer, export_wiki, Linker, Searcher, SearchResult};
|
||||
|
||||
#[macro_use] extern crate rocket;
|
||||
use rocket::serde::json::Json;
|
||||
@@ -71,6 +71,17 @@ async fn page<'r>(
|
||||
.render()))
|
||||
}
|
||||
}
|
||||
|
||||
if key == "q" {
|
||||
return PageResponder::Page(RawHtml(renderer.template("search.html")
|
||||
.with_historian(&historian)
|
||||
.with_page(&page)
|
||||
.insert("results", &Searcher::new(&historian).search(&page, value))
|
||||
.insert("options", &toml! {
|
||||
dynamic = true
|
||||
})
|
||||
.render()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +202,15 @@ struct Args {
|
||||
|
||||
/// Path to templates
|
||||
#[arg(long)]
|
||||
template_path: Option<String>
|
||||
template_path: Option<String>,
|
||||
|
||||
/// Search the wiki
|
||||
#[arg(long)]
|
||||
search: Option<String>,
|
||||
|
||||
/// Search root
|
||||
#[arg(long)]
|
||||
search_root: Option<String>
|
||||
}
|
||||
|
||||
fn print_tree(historian: &Historian, page: &Page, prefix: &str) {
|
||||
@@ -203,6 +222,14 @@ fn print_tree(historian: &Historian, page: &Page, prefix: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn print_result(result: &SearchResult) {
|
||||
println!("{}", result.page.full_name);
|
||||
for line in &result.matches {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("---");
|
||||
}
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
@@ -239,6 +266,16 @@ async fn main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(search) = args.search {
|
||||
let searcher = Searcher::new(&historian);
|
||||
let search_root = args.search_root.as_deref().unwrap_or("");
|
||||
let page = historian.resolve_to_page(&search_root).expect("failed to find page");
|
||||
for result in searcher.search(&page, &search) {
|
||||
print_result(&result);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
rocket::build()
|
||||
.manage(historian)
|
||||
.manage(renderer)
|
||||
|
||||
Reference in New Issue
Block a user