Add tag search.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-08-31 01:52:25 -05:00
parent 4e868abb01
commit 3ec4c11219
2 changed files with 56 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ use std::ffi::OsStr;
use git2::{Repository, Signature, Commit};
use rocket::form::validate::Contains;
use tera::Tera;
use toml::value::Table;
use serde::Serialize;
@@ -756,6 +757,36 @@ impl<'a> Searcher<'a> {
results
}
pub fn tag_search(&self, root: &Page, tag: &str) -> Vec<SearchResult> {
let mut results: Vec<SearchResult> = vec![];
for child in &root.children {
if let Some(child_page) = self.historian.resolve_to_page(&child.full_name) {
for result in self.tag_search(&child_page, tag) {
results.push(result);
}
let mut content = String::new();
if let Ok(mut file) = fs::File::open(&child_page.path) {
file.read_to_string(&mut content).unwrap();
}
let mut metadata = Table::new();
parse_markdown(&content, &mut metadata);
if metadata.contains_key("tags") {
let tags = metadata.get("tags").unwrap();
if tags.is_array() && tags.as_array().contains(toml::Value::String(tag.to_owned())) {
results.push(SearchResult {
page: child_page,
matches: vec![]
});
}
}
}
}
results
}
fn do_search(&self, root: &Page, results: &mut Vec<SearchResult>, searcher: &mut grep::searcher::Searcher, matcher: &grep::regex::RegexMatcher) {
for child in &root.children {
if let Some(child_page) = self.historian.resolve_to_page(&child.full_name) {

View File

@@ -82,6 +82,17 @@ async fn page<'r>(
})
.render()))
}
if key == "tag" {
return PageResponder::Page(RawHtml(renderer.template("search.html")
.with_historian(&historian)
.with_page(&page)
.insert("results", &Searcher::new(&historian).tag_search(&page, value))
.insert("options", &toml! {
dynamic = true
})
.render()))
}
}
}
@@ -208,6 +219,10 @@ struct Args {
#[arg(long)]
search: Option<String>,
/// Search the wiki by tag
#[arg(long)]
tag: Option<String>,
/// Search root
#[arg(long)]
search_root: Option<String>
@@ -276,6 +291,16 @@ async fn main() {
return;
}
if let Some(tag) = args.tag {
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.tag_search(&page, &tag) {
print_result(&result);
}
return;
}
rocket::build()
.manage(historian)
.manage(renderer)