diff options
author | Vika <vika@fireburn.ru> | 2023-08-08 14:56:37 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2023-08-08 14:56:37 +0300 |
commit | fbea42e5494c96174097bf3809a5b53d11d68a48 (patch) | |
tree | da8e5cfde49692105552fcb38382bcf25db92172 | |
parent | 108c462126b5da4a584d432e64371333d090a004 (diff) | |
download | kittybox-fbea42e5494c96174097bf3809a5b53d11d68a48.tar.zst |
templates-neo: fix hacks around the `html` crate
Finally I can properly add child elements without using the string hack.
-rw-r--r-- | Cargo.lock | 8 | ||||
-rw-r--r-- | templates-neo/Cargo.toml | 2 | ||||
-rw-r--r-- | templates-neo/src/mf2.rs | 95 |
3 files changed, 92 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock index 774d5f8..ef2030b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1310,18 +1310,18 @@ dependencies = [ [[package]] name = "html" -version = "0.5.2" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc3a7577238029a2704c53d9571a1e5c67f36ea9f487b03b405df8b64030cacf" +checksum = "e75c7e7321db52934cd91225ae7b6ecfe03920d3521a2357f186bcafa6b2ec14" dependencies = [ "html-sys", ] [[package]] name = "html-sys" -version = "0.3.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05eabf4b1d6f9cf57ee8ddaab1a3065ae8394c5cb08d80f9b49d9ebe9307b56d" +checksum = "b7d441fe4819dca7694335441917634ab425e1c2e4aa085134d8b5b6cbabd200" [[package]] name = "html5ever" diff --git a/templates-neo/Cargo.toml b/templates-neo/Cargo.toml index c0b426c..09abbfb 100644 --- a/templates-neo/Cargo.toml +++ b/templates-neo/Cargo.toml @@ -16,7 +16,7 @@ rand = "^0.8.5" [dependencies] ellipse = "^0.2.0" http = "^0.2.7" -html = "^0.5.2" +html = "^0.6.0" serde_json = "^1.0.64" include_dir = "^0.7.2" axum = "^0.6.18" diff --git a/templates-neo/src/mf2.rs b/templates-neo/src/mf2.rs index 706a301..c4d49a2 100644 --- a/templates-neo/src/mf2.rs +++ b/templates-neo/src/mf2.rs @@ -58,7 +58,76 @@ impl TryFrom<Item> for Card { }) } - todo!() + let mut props = card.properties.take(); + let uid = { + let uids = props.remove("uid").ok_or(Error::NoUid)?; + if let Some(PropertyValue::Url(uid)) = uids.into_iter().take(1).next() { + uid + } else { + return Err(Error::NoUid) + } + }; + + Ok(Self { + uid, + urls: props.remove("url").unwrap_or_default().into_iter() + .filter_map(|v| if let PropertyValue::Url(url) = v { + Some(url) + } else { + None + }).collect(), + name: props.remove("name") + .unwrap_or_default() + .into_iter() + .next() + .ok_or(Error::MissingProperty("name")) + .and_then(|v| match v { + PropertyValue::Plain(plain) => Ok(plain), + other => Err(Error::WrongValueType { + expected: "string", + got: other + }) + })?, + note: props.remove("note") + .unwrap_or_default() + .into_iter() + .next() + .and_then(|v| match v { + PropertyValue::Plain(plain) => Some(Ok(plain)), + other => Some(Err(Error::WrongValueType { + expected: "string", + got: other + })) + }) + .transpose()?, + photo: props.remove("photo") + .unwrap_or_default() + .into_iter() + .next() + .ok_or(Error::MissingProperty("photo")) + .and_then(|v| match v { + PropertyValue::Url(url) => Ok(Image::Plain(url)), + PropertyValue::Image(image) => Ok(Image::Accessible { + src: image.src, + alt: image.alt + }), + other => Err(Error::WrongValueType { + expected: "string", + got: other + }) + })?, + pronouns: props.remove("pronoun") + .unwrap_or_default() + .into_iter() + .map(|v| match v { + PropertyValue::Plain(plain) => Ok(plain), + other => Err(Error::WrongValueType { + expected: "string", + got: other + }) + }) + .collect::<Result<Vec<String>, _>>()? + }) } } @@ -110,8 +179,6 @@ impl Card { .href(url.clone()) .text(url) .build() - // XXX https://github.com/yoshuawuyts/html/issues/51 - .to_string() })); } @@ -274,7 +341,13 @@ impl TryFrom<Item> for Entry { .map(|v| -> Result<chrono::DateTime<chrono::FixedOffset>, Error> { match v { PropertyValue::Temporal(Temporal::Timestamp(dt)) => { - todo!() + // This is incredibly sketchy. + let (date, time, offset) = (dt.date.clone().unwrap().data, dt.as_time().unwrap().data.clone(), dt.as_time().unwrap().offset.unwrap().data); + + date.and_time(time).and_local_timezone(offset).single().ok_or_else(|| Error::WrongValueType { + expected: "datetime with timezone", + got: PropertyValue::Temporal(Temporal::Timestamp(dt)) + }) }, other => Err(Error::WrongValueType { expected: "timestamp", @@ -313,13 +386,11 @@ impl Entry { .class("u-url u-uid") .href(String::from(self.uid)) .push(html::inline_text::Time::builder() - .text(self.published.to_string()) - .text(self.published.to_rfc3339_opts( + .text(self.published.format("%Y-%m-%d %a %H:%M:%S %z").to_string()) + .date_time(self.published.to_rfc3339_opts( chrono::SecondsFormat::Secs, false )) .build() - // XXX https://github.com/yoshuawuyts/html/issues/51 - .to_string() ))) .division(|div| div .text("Tagged") @@ -336,5 +407,13 @@ impl Entry { ) }) ) + .main(|main| { + if let Some(lang) = self.content.0.lang { + main.lang(lang); + } + + main.push(self.content.0.html) + }) + } } |