From 5d0d47df9dc976c9d79673ccba6cd8382f5568d3 Mon Sep 17 00:00:00 2001 From: Shrecknt <58538423+Shrecknt@users.noreply.github.com> Date: Tue, 16 Apr 2024 23:35:24 -0700 Subject: [PATCH] tweaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit i am going insane 🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈 --- Cargo.toml | 1 + default-config.toml | 1 + src/build.rs | 19 +++++++++++ src/config.rs | 7 +++- src/engines/postsearch/minecraft_wiki.rs | 43 +++--------------------- src/web/assets/index.html | 1 + src/web/assets/style.css | 5 +++ src/web/mod.rs | 21 ++++++++++-- 8 files changed, 56 insertions(+), 42 deletions(-) create mode 100644 src/build.rs diff --git a/Cargo.toml b/Cargo.toml index 93c6812..b63c34e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "metasearch2" version = "0.1.0" edition = "2021" +build = "src/build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/default-config.toml b/default-config.toml index a926a75..631b3fb 100644 --- a/default-config.toml +++ b/default-config.toml @@ -1,6 +1,7 @@ bind = "0.0.0.0:28019" engine_list_separator = false +version_info = false [engines] google = { weight = 1.05 } diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..1545216 --- /dev/null +++ b/src/build.rs @@ -0,0 +1,19 @@ +use std::process::Command; + +fn main() { + println!("cargo:rustc-rerun-if-changed=.git/HEAD"); + let output = Command::new("git").args(["rev-parse", "HEAD"]).output(); + let git_hash = match output { + Ok(output) => String::from_utf8(output.stdout).unwrap_or("unknown".into()), + Err(_) => "unknown".into(), + }; + let output = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output(); + let git_hash_short = match output { + Ok(output) => String::from_utf8(output.stdout).unwrap_or("unknown".into()), + Err(_) => "unknown".into(), + }; + println!("cargo:rustc-env=GIT_HASH={}", git_hash); + println!("cargo:rustc-env=GIT_HASH_SHORT={}", git_hash_short); +} diff --git a/src/config.rs b/src/config.rs index f7e69c1..145baa2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,6 +11,8 @@ pub struct Config { pub bind: SocketAddr, #[serde(default)] pub engine_list_separator: Option, + #[serde(default)] + pub version_info: Option, pub engines: EnginesConfig, } @@ -19,7 +21,8 @@ impl Config { let default_config_str = include_str!("../default-config.toml"); let mut config: Config = toml::from_str(default_config_str)?; - let config_path = Path::new("config.toml"); + let config_path = std::env::args().nth(1).unwrap_or("config.toml".into()); + let config_path = Path::new(&config_path); if config_path.exists() { let given_config = toml::from_str::(&fs::read_to_string(config_path)?)?; config.update(given_config); @@ -38,6 +41,8 @@ impl Config { self.bind = new.bind; self.engine_list_separator = new.engine_list_separator.or(self.engine_list_separator); assert_ne!(self.engine_list_separator, None); + self.version_info = new.version_info.or(self.version_info); + assert_ne!(self.version_info, None); for (key, new) in new.engines.map { if let Some(existing) = self.engines.map.get_mut(&key) { existing.update(new); diff --git a/src/engines/postsearch/minecraft_wiki.rs b/src/engines/postsearch/minecraft_wiki.rs index 8b565be..854857f 100644 --- a/src/engines/postsearch/minecraft_wiki.rs +++ b/src/engines/postsearch/minecraft_wiki.rs @@ -1,4 +1,4 @@ -use scraper::{ElementRef, Html, Selector}; +use scraper::{Html, Selector}; use crate::engines::{HttpResponse, Response, CLIENT}; @@ -25,14 +25,13 @@ pub fn parse_response(HttpResponse { res, body, .. }: &HttpResponse) -> Option p").unwrap(); let doc_html = dom .select(&doc_query) .next() - .map(|doc| strip_gallery(doc)) - .unwrap_or_default() - .join(""); + .map(|doc| doc.html()) + .unwrap_or_default(); let doc_html = ammonia::Builder::default() .link_rel(None) @@ -52,37 +51,3 @@ pub fn parse_response(HttpResponse { res, body, .. }: &HttpResponse) -> Option{doc_html}"# )) } - -fn strip_gallery(doc: ElementRef) -> Vec { - let mut gallery = false; - doc.children() - .filter(|elem| { - let value = elem.value(); - if gallery { - return false; - } - match value { - scraper::Node::Element(_) => { - let elem = ElementRef::wrap(*elem).unwrap(); - let is_gallery_title = elem.first_child().map_or(false, |elem| { - elem.value().as_element().map_or(false, |_| { - let elem = ElementRef::wrap(elem).unwrap(); - elem.text().collect::() == "Gallery" - }) - }); - if is_gallery_title { - gallery = true; - return false; - } - true - } - _ => true, - } - }) - .map(|elem| { - ElementRef::wrap(elem) - .map(|elem| elem.html()) - .unwrap_or_default() - }) - .collect() -} diff --git a/src/web/assets/index.html b/src/web/assets/index.html index dd5a625..99864bd 100644 --- a/src/web/assets/index.html +++ b/src/web/assets/index.html @@ -30,5 +30,6 @@ + %version-info% diff --git a/src/web/assets/style.css b/src/web/assets/style.css index 3952799..2295bc2 100644 --- a/src/web/assets/style.css +++ b/src/web/assets/style.css @@ -40,6 +40,11 @@ body { line-height: 1.2; height: 100%; } +.version-info { + position: absolute; + bottom: 16px; + right: 16px; +} .results-container { /* enough space for the infobox */ max-width: 73.5rem; diff --git a/src/web/mod.rs b/src/web/mod.rs index 7ecdfb0..6d3d832 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -9,16 +9,33 @@ use tracing::info; use crate::config::Config; +const BASE_COMMIT_URL: &str = "https://github.com/mat-1/metasearch2/commit/"; +const VERSION: &str = std::env!("CARGO_PKG_VERSION"); +const COMMIT_HASH: &str = std::env!("GIT_HASH"); +const COMMIT_HASH_SHORT: &str = std::env!("GIT_HASH_SHORT"); + pub async fn run(config: Config) { let bind_addr = config.bind; + let version_info = if config.version_info.unwrap() { + if COMMIT_HASH == "unknown" || COMMIT_HASH_SHORT == "unknown" { + format!(r#"Version {VERSION} (unknown commit)"#) + } else { + format!( + r#"Version {VERSION} ({COMMIT_HASH_SHORT})"# + ) + } + } else { + String::new() + }; + let app = Router::new() .route( "/", - get(|| async { + get(|| async move { ( [(header::CONTENT_TYPE, "text/html; charset=utf-8")], - include_str!("assets/index.html"), + include_str!("assets/index.html").replace("%version-info%", &version_info), ) }), )