From 944c35f12c04f79ab9bca803c1f54b0c735f3aa9 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 21 Dec 2023 03:00:18 -0600 Subject: [PATCH] clippy and fixes --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/engines/answer/calc.rs | 13 ++++++++++--- src/engines/answer/useragent.rs | 2 +- src/engines/answer/wikipedia.rs | 4 ++-- src/engines/mod.rs | 6 +++--- src/engines/search/bing.rs | 30 +++++++++++++++++++++++++++++- src/normalize.rs | 13 +++++++++++-- src/parse.rs | 4 +++- src/web/assets/script.js | 12 ++++++++++++ src/web/assets/style.css | 9 +++++++-- 11 files changed, 78 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e75cd04..cd4ec9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,12 +52,6 @@ dependencies = [ "url", ] -[[package]] -name = "anyhow" -version = "1.0.75" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" - [[package]] name = "async-stream" version = "0.3.5" @@ -812,7 +806,6 @@ name = "metasearch2" version = "0.1.0" dependencies = [ "ammonia", - "anyhow", "async-stream", "axum", "base64", diff --git a/Cargo.toml b/Cargo.toml index cb98f83..eefb136 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] ammonia = "3.3.0" -anyhow = "1.0.75" async-stream = "0.3.5" axum = { version = "0.7.2", features = ["http2"] } base64 = "0.21.5" diff --git a/src/engines/answer/calc.rs b/src/engines/answer/calc.rs index c128faa..387e56a 100644 --- a/src/engines/answer/calc.rs +++ b/src/engines/answer/calc.rs @@ -1,5 +1,7 @@ use crate::engines::EngineResponse; +use super::regex; + pub fn request(query: &str) -> EngineResponse { let Some(result_html) = evaluate(query, true) else { return EngineResponse::new(); @@ -19,7 +21,7 @@ pub fn request_autocomplete(query: &str) -> Vec { results.push(format!("{query}={result}")); } - return results; + results } fn evaluate(query: &str, html: bool) -> Option { @@ -28,13 +30,18 @@ fn evaluate(query: &str, html: bool) -> Option { return None; } + // probably a query operator thing or a url, fend evaluates these but it shouldn't + if regex!("^[a-z]{2,}:").is_match(query) { + return None; + } + let mut context = fend_core::Context::new(); // make lowercase f and c work context.define_custom_unit_v1("f", "f", "°F", &fend_core::CustomUnitAttribute::Alias); context.define_custom_unit_v1("c", "c", "°C", &fend_core::CustomUnitAttribute::Alias); // make random work - context.set_random_u32_fn(|| rand::random::()); + context.set_random_u32_fn(rand::random::); if html { // this makes it generate slightly nicer outputs for some queries like 2d6 context.set_output_mode_terminal(); @@ -71,5 +78,5 @@ fn evaluate(query: &str, html: bool) -> Option { } } - return Some(result_html); + Some(result_html) } diff --git a/src/engines/answer/useragent.rs b/src/engines/answer/useragent.rs index 43a10e7..7225439 100644 --- a/src/engines/answer/useragent.rs +++ b/src/engines/answer/useragent.rs @@ -14,6 +14,6 @@ pub fn request(query: &SearchQuery) -> EngineResponse { EngineResponse::answer_html(if let Some(user_agent) = user_agent { format!("

{user_agent}

") } else { - format!("You don't have a user agent") + "You don't have a user agent".to_string() }) } diff --git a/src/engines/answer/wikipedia.rs b/src/engines/answer/wikipedia.rs index a8a67d6..c36742c 100644 --- a/src/engines/answer/wikipedia.rs +++ b/src/engines/answer/wikipedia.rs @@ -76,14 +76,14 @@ pub fn parse_response(body: &str) -> eyre::Result { extract, thumbnail: _, } = page; - if extract.ends_with(":") { + if extract.ends_with(':') { return Ok(EngineResponse::new()); } // this is present on the wikipedia article for google let extract = extract.replace("( )", ""); - let page_title = title.replace(" ", "_"); + let page_title = title.replace(' ', "_"); let page_url = format!("https://en.wikipedia.org/wiki/{page_title}"); Ok(EngineResponse::infobox_html(format!( diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 6b42353..2f5ad27 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -262,7 +262,7 @@ pub async fn search_with_engines( requests.push(async { let engine = *engine; - let request_response = engine.request(query).into(); + let request_response = engine.request(query); let response = match request_response { RequestResponse::Http(request) => { @@ -424,12 +424,12 @@ pub async fn search( progress_tx: mpsc::UnboundedSender, ) -> eyre::Result<()> { let engines = Engine::all(); - search_with_engines(&engines, &query, progress_tx).await + search_with_engines(engines, &query, progress_tx).await } pub async fn autocomplete(query: &str) -> eyre::Result> { let engines = Engine::all(); - autocomplete_with_engines(&engines, query).await + autocomplete_with_engines(engines, query).await } #[derive(Debug, Clone)] diff --git a/src/engines/search/bing.rs b/src/engines/search/bing.rs index fda80e7..e90966f 100644 --- a/src/engines/search/bing.rs +++ b/src/engines/search/bing.rs @@ -38,7 +38,35 @@ pub fn parse_response(body: &str) -> eyre::Result { .unwrap_or_default(); clean_url(url) }))) - .description(".b_caption > p, p.b_algoSlug, .b_caption .ipText"), + .description(QueryMethod::Manual(Box::new(|el: &ElementRef| { + let mut description = String::new(); + for inner_node in el + .select( + &Selector::parse(".b_caption > p, p.b_algoSlug, .b_caption .ipText") + .unwrap(), + ) + .next() + .map(|n| n.children().collect::>()) + .unwrap_or_default() + { + match inner_node.value() { + scraper::Node::Text(t) => { + description.push_str(&t.text); + } + scraper::Node::Element(inner_el) => { + if !inner_el + .has_class("algoSlug_icon", scraper::CaseSensitivity::CaseSensitive) + { + let element_ref = ElementRef::wrap(inner_node).unwrap(); + description.push_str(&element_ref.text().collect::()); + } + } + _ => {} + } + } + + Ok(description) + }))), ) } diff --git a/src/normalize.rs b/src/normalize.rs index 75cf4bb..1a6c1de 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -44,10 +44,19 @@ pub fn normalize_url(url: &str) -> eyre::Result { )); } + // convert minecraft.fandom.com/wiki/ to minecraft.wiki/w/ + if url.host_str() == Some("minecraft.fandom.com") { + let path = url.path().to_string(); + if let Some(path) = path.strip_prefix("/wiki/") { + url.set_host(Some("minecraft.wiki")).unwrap(); + url.set_path(&format!("/w/{path}")); + } + } + // url decode and encode path let path = url.path().to_string(); let path = urlencoding::decode(&path)?; - url.set_path(&path.to_string()); + url.set_path(path.as_ref()); let url = url.to_string(); // remove trailing slash @@ -57,5 +66,5 @@ pub fn normalize_url(url: &str) -> eyre::Result { url }; - return Ok(url); + Ok(url) } diff --git a/src/parse.rs b/src/parse.rs index 6760d9b..bf120dc 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -72,12 +72,14 @@ impl ParseOpts { } } +type ManualQueryMethod = Box eyre::Result>; + #[derive(Default)] pub enum QueryMethod { #[default] None, CssSelector(&'static str), - Manual(Box eyre::Result>), + Manual(ManualQueryMethod), } impl From<&'static str> for QueryMethod { diff --git a/src/web/assets/script.js b/src/web/assets/script.js index 587c81c..35b613e 100644 --- a/src/web/assets/script.js +++ b/src/web/assets/script.js @@ -118,6 +118,14 @@ document.addEventListener("keydown", (e) => { searchInputEl.focus(); searchInputEl.setSelectionRange(0, 0); } + // backspace key focuses it at the end + else if (e.key === "Backspace") { + searchInputEl.focus(); + searchInputEl.setSelectionRange( + searchInputEl.value.length, + searchInputEl.value.length + ); + } }); // update the input suggestions on input @@ -127,3 +135,7 @@ searchInputEl.addEventListener("input", () => { }); // and on focus searchInputEl.addEventListener("focus", updateSuggestions); +// on unfocus hide the suggestions +searchInputEl.addEventListener("blur", () => { + suggestionsEl.style.visibility = "hidden"; +}); diff --git a/src/web/assets/style.css b/src/web/assets/style.css index bc5f754..c71e36e 100644 --- a/src/web/assets/style.css +++ b/src/web/assets/style.css @@ -53,6 +53,9 @@ a { a:visited { color: #92e; } +pre { + white-space: pre-wrap; +} /* index page */ .main-container { @@ -219,13 +222,15 @@ h1 { .infobox p { margin: 0; } -.infobox-stackoverflow-answer pre > code { +.infobox-stackoverflow-answer pre > code, +.infobox-github-readme pre { border: 1px solid #234; padding: 0.5rem; display: block; font-weight: normal; } -.infobox-stackoverflow-answer code { +.infobox-stackoverflow-answer code, +.infobox-github-readme code { font-weight: bold; } .postsearch-infobox p {