diff --git a/Cargo.lock b/Cargo.lock index ecf074f..e87f534 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,7 +309,10 @@ dependencies = [ "elefren", "env_logger", "html2md", + "html5ever 0.25.1", "log", + "markup5ever 0.10.1", + "markup5ever_rcdom", "reqwest", "serde", "serde_derive", @@ -536,7 +539,7 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3f40515ebe18a0e23fa35cfce0694e681474a29c6cf0ebe510c05ad6cf2233c" dependencies = [ - "html5ever", + "html5ever 0.22.5", "jni", "lazy_static", "percent-encoding 1.0.1", @@ -551,12 +554,26 @@ checksum = "c213fa6a618dc1da552f54f85cba74b05d8e883c92ec4e89067736938084c26e" dependencies = [ "log", "mac", - "markup5ever", + "markup5ever 0.7.5", "proc-macro2 0.4.30", "quote 0.6.13", "syn 0.15.44", ] +[[package]] +name = "html5ever" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aafcf38a1a36118242d29b92e1b08ef84e67e4a5ed06e0a80be20e6a32bfed6b" +dependencies = [ + "log", + "mac", + "markup5ever 0.10.1", + "proc-macro2 1.0.4", + "quote 1.0.2", + "syn 1.0.5", +] + [[package]] name = "http" version = "0.1.18" @@ -695,8 +712,8 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "265ef164908329e47e753c769b14cbb27434abf0c41984dca201484022f09ce5" dependencies = [ - "phf", - "phf_codegen", + "phf 0.7.24", + "phf_codegen 0.7.24", "serde", ] @@ -785,14 +802,40 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897636f9850c3eef4905a5540683ed53dc9393860f0846cab2c2ddf9939862ff" dependencies = [ - "phf", - "phf_codegen", + "phf 0.7.24", + "phf_codegen 0.7.24", "serde", "serde_derive", "serde_json", - "string_cache", - "string_cache_codegen", + "string_cache 0.7.3", + "string_cache_codegen 0.4.4", + "tendril", +] + +[[package]] +name = "markup5ever" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a24f40fb03852d1cdd84330cddcaf98e9ec08a7b7768e952fad3b4cf048ec8fd" +dependencies = [ + "log", + "phf 0.8.0", + "phf_codegen 0.8.0", + "string_cache 0.8.1", + "string_cache_codegen 0.5.1", + "tendril", +] + +[[package]] +name = "markup5ever_rcdom" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f015da43bcd8d4f144559a3423f4591d69b8ce0652c905374da7205df336ae2b" +dependencies = [ + "html5ever 0.25.1", + "markup5ever 0.10.1", "tendril", + "xml5ever", ] [[package]] @@ -1024,7 +1067,16 @@ version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" dependencies = [ - "phf_shared", + "phf_shared 0.7.24", +] + +[[package]] +name = "phf" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" +dependencies = [ + "phf_shared 0.8.0", ] [[package]] @@ -1033,8 +1085,18 @@ version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" dependencies = [ - "phf_generator", - "phf_shared", + "phf_generator 0.7.24", + "phf_shared 0.7.24", +] + +[[package]] +name = "phf_codegen" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" +dependencies = [ + "phf_generator 0.8.0", + "phf_shared 0.8.0", ] [[package]] @@ -1043,17 +1105,36 @@ version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" dependencies = [ - "phf_shared", + "phf_shared 0.7.24", "rand 0.6.5", ] +[[package]] +name = "phf_generator" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" +dependencies = [ + "phf_shared 0.8.0", + "rand 0.7.2", +] + [[package]] name = "phf_shared" version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" dependencies = [ - "siphasher", + "siphasher 0.2.3", +] + +[[package]] +name = "phf_shared" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" +dependencies = [ + "siphasher 0.3.5", ] [[package]] @@ -1159,7 +1240,7 @@ dependencies = [ "rand_isaac", "rand_jitter", "rand_os", - "rand_pcg", + "rand_pcg 0.1.2", "rand_xorshift", "winapi 0.3.8", ] @@ -1175,6 +1256,7 @@ dependencies = [ "rand_chacha 0.2.1", "rand_core 0.5.1", "rand_hc 0.2.0", + "rand_pcg 0.2.1", ] [[package]] @@ -1283,6 +1365,15 @@ dependencies = [ "rand_core 0.4.2", ] +[[package]] +name = "rand_pcg" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "rand_xorshift" version = "0.1.1" @@ -1524,6 +1615,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" +[[package]] +name = "siphasher" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbce6d4507c7e4a3962091436e56e95290cb71fa302d0d270e32130b75fbff27" + [[package]] name = "skeptic" version = "0.13.4" @@ -1575,26 +1672,51 @@ checksum = "25d70109977172b127fe834e5449e5ab1740b9ba49fa18a2020f509174f25423" dependencies = [ "lazy_static", "new_debug_unreachable", - "phf_shared", + "phf_shared 0.7.24", "precomputed-hash", "serde", - "string_cache_codegen", + "string_cache_codegen 0.4.4", "string_cache_shared", ] +[[package]] +name = "string_cache" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ddb1139b5353f96e429e1a5e19fbaf663bddedaa06d1dbd49f82e352601209a" +dependencies = [ + "lazy_static", + "new_debug_unreachable", + "phf_shared 0.8.0", + "precomputed-hash", + "serde", +] + [[package]] name = "string_cache_codegen" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f45ed1b65bf9a4bf2f7b7dc59212d1926e9eaf00fa998988e420fd124467c6" dependencies = [ - "phf_generator", - "phf_shared", + "phf_generator 0.7.24", + "phf_shared 0.7.24", "proc-macro2 1.0.4", "quote 1.0.2", "string_cache_shared", ] +[[package]] +name = "string_cache_codegen" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f24c8e5e19d22a726626f1a5e16fe15b132dcf21d10177fa5a45ce7962996b97" +dependencies = [ + "phf_generator 0.8.0", + "phf_shared 0.8.0", + "proc-macro2 1.0.4", + "quote 1.0.2", +] + [[package]] name = "string_cache_shared" version = "0.3.0" @@ -2054,3 +2176,15 @@ dependencies = [ "winapi 0.2.8", "winapi-build", ] + +[[package]] +name = "xml5ever" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b1b52e6e8614d4a58b8e70cf51ec0cc21b256ad8206708bcff8139b5bbd6a59" +dependencies = [ + "log", + "mac", + "markup5ever 0.10.1", + "time", +] diff --git a/Cargo.toml b/Cargo.toml index f1fc4c6..3faf911 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,7 @@ serde_derive = "*" toml = "0.5" log = "0.4" env_logger = "0.8" -chrono = "0.4" \ No newline at end of file +chrono = "0.4" +html5ever = "0.25" +markup5ever_rcdom = "0.1" +markup5ever = "0.10" diff --git a/src/storage/org.rs b/src/storage/org.rs index 5655267..b2c7dd2 100644 --- a/src/storage/org.rs +++ b/src/storage/org.rs @@ -18,6 +18,7 @@ use std::borrow::Borrow; use std::default::Default; +use std::fs::File; use std::fs::OpenOptions; use std::io::prelude::*; use std::path::Path; @@ -37,7 +38,9 @@ use crate::storage::storage::Storage; /// Definition for the Org storage pub struct Org { /// The path where the file will be stored - file: PathBuf, + path: PathBuf, + /// The filename for the org file + filename: String, /// The date being processed, needed for the header if it is a new file date: String, } @@ -130,7 +133,8 @@ impl Org { log::debug!("Org file: {}", full_path.to_string_lossy()); Org { - file: full_path, + path: Path::new(&config.location).to_path_buf(), + filename: filename, date, } } @@ -149,26 +153,27 @@ impl Org { let mut result = String::new(); result.push_str(" "); // initial identantion walk(&dom.document, &mut result); - result + result.trim().into() } } impl Storage for Org { fn save(&self, record: &Data) { + let org_file = self.path.join(&self.filename); let mut fp = OpenOptions::new() .write(true) .append(true) - .open(&self.file) + .open(&org_file) .unwrap_or_else(|_| { // Let's assume here that the problem is that the file doesn't exist. log::debug!( "Creating {filename}", - filename = &self.file.to_string_lossy() + filename = &org_file.to_string_lossy() ); OpenOptions::new() .write(true) .create(true) - .open(&self.file) + .open(&org_file) .map(|mut fp| { let text = format!("#+title: Favourites from {date}\n\n", date = &self.date); @@ -180,6 +185,26 @@ impl Storage for Org { fp.write_all(Org::title(record).as_bytes()).unwrap(); fp.write_all("\n".as_bytes()).unwrap(); fp.write_all(Org::body(record).as_bytes()).unwrap(); - fp.write_all("\n".as_bytes()).unwrap(); + fp.write_all("\n\n".as_bytes()).unwrap(); + + if !record.attachments.is_empty() { + fp.write_all(" Attachments:\n".as_bytes()).unwrap(); + for attachment in record.attachments.iter() { + let filename = attachment.filename().to_string(); + let in_storage = self.path.join(&record.id).join(&filename); + let mut target = File::create(&in_storage).unwrap(); + log::debug!( + "Downloading attachment {} as {}", + filename, + in_storage.to_string_lossy() + ); + attachment.download().copy_to(&mut target).unwrap(); + + let attachment_info = + format!(" - [[{}][{}]\n", in_storage.to_string_lossy(), filename); + fp.write_all(attachment_info.as_bytes()).unwrap(); + } + fp.write_all("\n\n".as_bytes()).unwrap() + } } }