Skip to content

web search done right

parse html mime metadata

Mikkel Denker919441850b89
diff --git a/crates/core/src/webpage/html/mod.rs b/crates/core/src/webpage/html/mod.rs
index 926fe5e0..70ff4fc4 100644
--- a/crates/core/src/webpage/html/mod.rs
+++ b/crates/core/src/webpage/html/mod.rs
@@ -18,6 +18,7 @@ use crate::{enum_map::EnumSet, Result};
use chrono::{DateTime, FixedOffset, Utc};
use itertools::Itertools;
use kuchiki::{traits::TendrilSink, NodeRef};
+use mime::Mime;
use regex::Regex;
use url::Url;
use whatlang::Lang;
@@ -91,6 +92,23 @@ impl Html {
Ok(res)
}
+ pub fn content_type(&self) -> Option<String> {
+ self.metadata()
+ .into_iter()
+ .find(|m| matches!(m.get("http-equiv"), Some(t) if t == "Content-Type"))
+ .and_then(|m| m.get("content").cloned())
+ }
+
+ pub fn mime(&self) -> Option<Mime> {
+ self.content_type()
+ .and_then(|content_type| content_type.parse().ok())
+ }
+
+ pub fn charset(&self) -> Option<String> {
+ self.mime()
+ .and_then(|mime| mime.get_param("charset").map(|s| s.to_string()))
+ }
+
pub fn lang(&self) -> Option<&'_ Lang> {
self.lang.as_ref()
}