diff --git a/Cargo.lock b/Cargo.lock index dfd7ad9..dcb54f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,6 +427,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -486,6 +495,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -575,6 +594,7 @@ dependencies = [ "serde_json", "serde_with", "serde_yaml", + "url", ] [[package]] @@ -635,6 +655,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pkg-config" version = "0.3.30" @@ -965,18 +991,48 @@ dependencies = [ "time-core", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "typed-arena" version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode_categories" version = "0.1.1" @@ -989,6 +1045,17 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" +[[package]] +name = "url" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 9eb1082..25b65a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" serde_with = { version = "3.7.0", features = [ "macros" ] } serde_yaml = "0.9.34" +url = "2.5.0" diff --git a/src/convert.rs b/src/convert.rs index 6c1649b..b41a6ae 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,11 +1,12 @@ -use comrak::{arena_tree::Node, nodes::Ast, nodes::NodeValue}; use std::{cell::RefCell, fmt::Error}; -use crate::utils; +use comrak::{arena_tree::Node, nodes::Ast, nodes::NodeValue}; mod npf; + +use crate::utils; use npf::content_types::text::{Formatting, FormattingType, Subtypes}; -use npf::{content_types, ContentType, NPF}; +use npf::{content_types, objects::Media, ContentType, NPF}; #[derive(clap::ValueEnum, Clone, Debug)] pub enum Formats { @@ -62,6 +63,41 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell>) -> Result text.push_str(&format!("{} ", &t)) } NodeValue::Text(t) => text.push_str(&format!("{} ", &t)), + NodeValue::Image(i) => { + if let Ok(u) = url::Url::parse(&i.url) { + if [ + Some("www.youtube.com"), + Some("youtube.com"), + Some("youtu.be"), + ] + .contains(&u.host_str()) + { + let mut video = content_types::Video::from(String::from(u)); + video.provider = Some(String::from("youtube")); + video.embed_iframe = Some(npf::objects::IFrame { + url: String::from(&video.url.clone().unwrap()), + width: None, + height: None, + }); + npf.borrow_mut().content.push(ContentType::Video(video)); + } + } else { + let mut image = content_types::Image::from(vec![Media { + r#type: None, + url: String::from(&i.url), + provider: None, + poster: None, + width: None, + height: None, + has_original_dimensions: None, + cropped: None, + original_dimensions_missing: None, + }]); + image.caption = Some(String::from(&i.title)); + image.alt_text = Some(utils::extract_text(node)); + npf.borrow_mut().content.push(ContentType::Image(image)); + } + } _ => (), };