From 9c68514dfbccf4dd9ea6c590d5109e8c6a3bdacc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 18:21:44 +0200 Subject: [PATCH 1/9] Add processor for collecting links from content and putting them into the header via the functionality offered by `libimagentrylink`. --- lib/entry/libimagentrymarkdown/Cargo.toml | 6 +- lib/entry/libimagentrymarkdown/src/error.rs | 28 +++ lib/entry/libimagentrymarkdown/src/lib.rs | 10 + lib/entry/libimagentrymarkdown/src/link.rs | 6 + .../libimagentrymarkdown/src/processor.rs | 211 ++++++++++++++++++ 5 files changed, 259 insertions(+), 2 deletions(-) create mode 100644 lib/entry/libimagentrymarkdown/src/processor.rs diff --git a/lib/entry/libimagentrymarkdown/Cargo.toml b/lib/entry/libimagentrymarkdown/Cargo.toml index 3b962eac..ad27d0d2 100644 --- a/lib/entry/libimagentrymarkdown/Cargo.toml +++ b/lib/entry/libimagentrymarkdown/Cargo.toml @@ -19,6 +19,8 @@ hoedown = "5.0.0" url = "1.2" error-chain = "0.10" -libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" } -libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } +libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" } +libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } +libimagentrylink = { version = "0.4.0", path = "../../../lib/entry/libimagentrylink/" } +libimagutil = { version = "0.4.0", path = "../../../lib/etc/libimagutil/" } diff --git a/lib/entry/libimagentrymarkdown/src/error.rs b/lib/entry/libimagentrymarkdown/src/error.rs index 4df464ec..d3efc6ec 100644 --- a/lib/entry/libimagentrymarkdown/src/error.rs +++ b/lib/entry/libimagentrymarkdown/src/error.rs @@ -17,11 +17,25 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use url::Url; + +use libimagstore::storeid::StoreId; + error_chain! { types { MarkdownError, MarkdownErrorKind, ResultExt, Result; } + links { + StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); + LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind); + RefError(::libimagentryref::error::RefError, ::libimagentryref::error::RefErrorKind); + } + + foreign_links { + UrlParserError(::url::ParseError); + } + errors { MarkdownRenderError { description("Markdown render error") @@ -33,6 +47,20 @@ error_chain! { display("Link parsing error") } + StoreGetError(id: StoreId) { + description("Failed to get entry from store") + display("Failed to get entry '{}' from store", id) + } + + UndecidableLinkType(s: String) { + description("Failed to qualify link type") + display("The Type of the link '{}' cannot be recognized", s) + } + + UrlProcessingError(u: Url) { + description("Failed to properly processing URL") + display("The URL '{:?}' could not be processed properly", u) + } } } diff --git a/lib/entry/libimagentrymarkdown/src/lib.rs b/lib/entry/libimagentrymarkdown/src/lib.rs index e17751dc..5626154d 100644 --- a/lib/entry/libimagentrymarkdown/src/lib.rs +++ b/lib/entry/libimagentrymarkdown/src/lib.rs @@ -39,9 +39,19 @@ extern crate hoedown; extern crate url; extern crate libimagstore; extern crate libimagerror; +extern crate libimagentrylink; +extern crate libimagutil; #[macro_use] extern crate error_chain; +#[cfg(test)] +extern crate env_logger; + +#[allow(unused)] +#[macro_use] +extern crate log; + pub mod error; pub mod html; pub mod link; +pub mod processor; diff --git a/lib/entry/libimagentrymarkdown/src/link.rs b/lib/entry/libimagentrymarkdown/src/link.rs index 7de3edc7..bd26bea1 100644 --- a/lib/entry/libimagentrymarkdown/src/link.rs +++ b/lib/entry/libimagentrymarkdown/src/link.rs @@ -48,6 +48,7 @@ pub struct UrlLink { pub link: Url, } +#[derive(Debug)] struct LinkExtractor { links: Vec, } @@ -76,6 +77,10 @@ impl Render for LinkExtractor { let link = link.and_then(|l| l.to_str().ok()).map(String::from); let content = content.and_then(|l| l.to_str().ok()).map(String::from); + trace!("Processing..."); + trace!("link = {:?}", link); + trace!("content = {:?}", content); + match (link, content) { (Some(link), Some(content)) => { self.links.push(Link { link: link, title: content }); @@ -94,6 +99,7 @@ impl Render for LinkExtractor { pub fn extract_links(buf: &str) -> Vec { let mut le = LinkExtractor::new(); le.render(&Markdown::new(buf)); + trace!("Extracted: {:?}", le); le.links() } diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs new file mode 100644 index 00000000..4fbf3633 --- /dev/null +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -0,0 +1,211 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +use error::MarkdownError as ME; +use error::MarkdownErrorKind as MEK; +use error::*; +use link::extract_links; + +use libimagentrylink::external::ExternalLinker; +use libimagentrylink::internal::InternalLinker; +use libimagentryref::refstore::RefStore; +use libimagentryref::flags::RefFlags; +use libimagstore::store::Entry; +use libimagstore::store::Store; +use libimagstore::storeid::StoreId; +use libimagutil::iter::FoldResult; + +use std::path::PathBuf; + +use url::Url; + +/// A link Processor which collects the links from a Markdown and passes them on to +/// `libimagentrylink` functionality +/// +/// The processor can be configured to +/// +/// * Process internal links (from store entry to store entry) +/// * Process internal links with automatically creating targets +/// If an internal link is encountered, the corrosponding target must be present in the store. +/// If it is not, it will either be created or the processing fails +/// * Process external links (from store entry to URL) +/// * Process refs (from store entry to files on the filesystem and outside of the store) +/// (default: false) +/// +/// # Note +/// +/// There's no LinkProcessor::new() function, please use `LinkProcessor::default()`. +/// +pub struct LinkProcessor { + process_internal_links: bool, + create_internal_targets: bool, + process_external_links: bool, + process_refs: bool +} + +impl LinkProcessor { + + /// Switch internal link processing on/off + /// + /// Internal links are links which are simply `dirctory/file`, but not `/directory/file`, as + /// beginning an id with `/` is discouraged in imag. + pub fn process_internal_links(mut self, b: bool) -> Self { + self.process_internal_links = b; + self + } + + /// Switch internal link target creation on/off + /// + /// If a link points to a non-existing imag entry, a `false` here will cause the processor to + /// return an error from `process()`. A `true` setting will create the entry and then fetch it + /// to link it to the processed entry. + pub fn create_internal_targets(mut self, b: bool) -> Self { + self.create_internal_targets = b; + self + } + + /// Switch external link processing on/off + /// + /// An external link must start with `https://` or `http://`. + pub fn process_external_links(mut self, b: bool) -> Self { + self.process_external_links = b; + self + } + + /// Switch ref processing on/off + /// + /// A Ref is to be expected beeing a link with `file::///` at the beginning. + pub fn process_refs(mut self, b: bool) -> Self { + self.process_refs = b; + self + } + + /// Process an Entry for its links + /// + /// # Warning + /// + /// When `LinkProcessor::create_internal_targets()` was called to set the setting to true, this + /// function returns all errors returned by the Store. + /// + pub fn process<'a>(&self, entry: &mut Entry, store: &'a Store) -> Result<()> { + let text = entry.to_str(); + trace!("Processing: {:?}", entry.get_location()); + extract_links(&text) + .into_iter() + .fold_result::<_, MarkdownError, _>(|link| { + trace!("Processing {:?}", link); + match LinkQualification::qualify(&link.link) { + LinkQualification::InternalLink => { + if !self.process_internal_links { + return Ok(()); + } + + let spath = Some(store.path().clone()); + let id = StoreId::new(spath, PathBuf::from(&link.link))?; + let mut target = if self.create_internal_targets { + try!(store.retrieve(id)) + } else { + store.get(id.clone())? + .ok_or(ME::from_kind(MEK::StoreGetError(id)))? + }; + + entry.add_internal_link(&mut target).map_err(From::from) + }, + LinkQualification::ExternalLink(url) => { + if !self.process_external_links { + return Ok(()); + } + + entry.add_external_link(store, url).map_err(From::from) + }, + LinkQualification::RefLink(url) => { + if !self.process_refs { + return Ok(()); + } + + let flags = RefFlags::default() + .with_content_hashing(false) + .with_permission_tracking(false); + trace!("URL = {:?}", url); + trace!("URL.path() = {:?}", url.path()); + trace!("URL.host_str() = {:?}", url.host_str()); + let path = url.host_str().unwrap_or_else(|| url.path()); + let path = PathBuf::from(path); + let mut target = try!(RefStore::create(store, path, flags)); + + entry.add_internal_link(&mut target).map_err(From::from) + }, + LinkQualification::Undecidable(e) => { + // error + Err(e).chain_err(|| MEK::UndecidableLinkType(link.link.clone())) + }, + } + }) + } + +} + +/// Enum to tell what kind of link a string of text is +enum LinkQualification { + InternalLink, + ExternalLink(Url), + RefLink(Url), + Undecidable(ME), +} + +impl LinkQualification { + fn qualify(text: &str) -> LinkQualification { + match Url::parse(text) { + Ok(url) => { + if url.scheme() == "file" { + return LinkQualification::RefLink(url) + } + + // else we assume the following, as other stuff gets thrown out by + // url::Url::parse() as Err(_) + // + // if url.scheme() == "https" || url.scheme() == "http" { + return LinkQualification::ExternalLink(url); + // } + }, + + Err(e) => { + match e { + ::url::ParseError::RelativeUrlWithoutBase => { + LinkQualification::InternalLink + }, + + _ => LinkQualification::Undecidable(ME::from(e)), + } + } + } + } +} + +impl Default for LinkProcessor { + fn default() -> Self { + LinkProcessor { + process_internal_links: true, + create_internal_targets: false, + process_external_links: true, + process_refs: false + } + } +} + From 0a60f5ca552a71bc9ff7e8f57d425844e467a03d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 21:51:54 +0200 Subject: [PATCH 2/9] Add test for processing entry without links --- lib/entry/libimagentrymarkdown/Cargo.toml | 2 + lib/entry/libimagentrymarkdown/src/lib.rs | 2 +- .../libimagentrymarkdown/src/processor.rs | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/entry/libimagentrymarkdown/Cargo.toml b/lib/entry/libimagentrymarkdown/Cargo.toml index ad27d0d2..92d923d8 100644 --- a/lib/entry/libimagentrymarkdown/Cargo.toml +++ b/lib/entry/libimagentrymarkdown/Cargo.toml @@ -18,9 +18,11 @@ log = "0.3" hoedown = "5.0.0" url = "1.2" error-chain = "0.10" +env_logger = "0.3" libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } libimagentrylink = { version = "0.4.0", path = "../../../lib/entry/libimagentrylink/" } +libimagentryref = { version = "0.4.0", path = "../../../lib/entry/libimagentryref/" } libimagutil = { version = "0.4.0", path = "../../../lib/etc/libimagutil/" } diff --git a/lib/entry/libimagentrymarkdown/src/lib.rs b/lib/entry/libimagentrymarkdown/src/lib.rs index 5626154d..154868eb 100644 --- a/lib/entry/libimagentrymarkdown/src/lib.rs +++ b/lib/entry/libimagentrymarkdown/src/lib.rs @@ -40,13 +40,13 @@ extern crate url; extern crate libimagstore; extern crate libimagerror; extern crate libimagentrylink; +extern crate libimagentryref; extern crate libimagutil; #[macro_use] extern crate error_chain; #[cfg(test)] extern crate env_logger; -#[allow(unused)] #[macro_use] extern crate log; diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 4fbf3633..e5a36ab7 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -209,3 +209,40 @@ impl Default for LinkProcessor { } } +#[cfg(test)] +mod tests { + use super::*; + + use std::path::PathBuf; + + use libimagstore::store::Store; + + fn setup_logging() { + use env_logger; + let _ = env_logger::init().unwrap_or(()); + } + + pub fn get_store() -> Store { + use libimagstore::file_abstraction::InMemoryFileAbstraction; + let fs = InMemoryFileAbstraction::new(); + Store::new_with_backend(PathBuf::from("/"), None, Box::new(fs)).unwrap() + } + + #[test] + fn test_process_no_links() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-1")).unwrap(); + *base.get_content_mut() = format!("This is an example entry with no links"); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default(); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok()); + } +} + From 0b640e18befc513163585e409ea241b4ff0fc3d3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 22:14:49 +0200 Subject: [PATCH 3/9] Add test to check whether internal links are parsed and set correctly --- lib/entry/libimagentrymarkdown/src/lib.rs | 3 +- .../libimagentrymarkdown/src/processor.rs | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/entry/libimagentrymarkdown/src/lib.rs b/lib/entry/libimagentrymarkdown/src/lib.rs index 154868eb..9e4df569 100644 --- a/lib/entry/libimagentrymarkdown/src/lib.rs +++ b/lib/entry/libimagentrymarkdown/src/lib.rs @@ -43,12 +43,11 @@ extern crate libimagentrylink; extern crate libimagentryref; extern crate libimagutil; #[macro_use] extern crate error_chain; +#[macro_use] extern crate log; #[cfg(test)] extern crate env_logger; -#[macro_use] -extern crate log; pub mod error; pub mod html; diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index e5a36ab7..ea85d7b9 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -216,6 +216,7 @@ mod tests { use std::path::PathBuf; use libimagstore::store::Store; + use libimagentrylink::internal::InternalLinker; fn setup_logging() { use env_logger; @@ -244,5 +245,49 @@ mod tests { let result = processor.process(&mut base, &store); assert!(result.is_ok()); } + + #[test] + fn test_process_one_existing_file_linked() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-2.1")).unwrap(); + *base.get_content_mut() = format!("This is an example entry with one [link](test-2.2)"); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + // immediately drop as we don't need this entry right now + let _ = store.create(PathBuf::from("test-2.2")).unwrap(); + + let processor = LinkProcessor::default() + .process_internal_links(true) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + { + let base_links = base.get_internal_links(); + assert!(base_links.is_ok()); + let base_links : Vec<_> = base_links.unwrap().collect(); + + assert_eq!(1, base_links.len()); + assert_eq!("test-2.2", base_links[0].to_str().unwrap()); + } + + { + let link = store.get(PathBuf::from("test-2.2")).unwrap().unwrap(); + let link_links = link.get_internal_links(); + assert!(link_links.is_ok()); + let link_links : Vec<_> = link_links.unwrap().collect(); + + assert_eq!(1, link_links.len()); + assert_eq!("test-2.1", link_links[0].to_str().unwrap()); + } + } + } From 0aea9151dd39d83c4067c1a4d9a3183babba54aa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 22:19:42 +0200 Subject: [PATCH 4/9] Add test for faulty link --- .../libimagentrymarkdown/src/processor.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index ea85d7b9..2664a0f1 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -289,5 +289,25 @@ mod tests { } } + #[test] + fn test_process_one_existing_file_linked_faulty() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-2.1")).unwrap(); + *base.get_content_mut() = format!("This is an example entry with one [link](/test-2.2)"); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(true) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_err(), "Should be Err(_), but is Ok(())"); + } } From 3cb2393c08162dcad3fa4c369711a0d3a478626b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 22:21:00 +0200 Subject: [PATCH 5/9] Add test for automatically creating linked entries --- .../libimagentrymarkdown/src/processor.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 2664a0f1..39bea4a4 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -309,5 +309,45 @@ mod tests { let result = processor.process(&mut base, &store); assert!(result.is_err(), "Should be Err(_), but is Ok(())"); } + + #[test] + fn test_process_one_nonexisting_file_linked() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-2.1")).unwrap(); + *base.get_content_mut() = format!("This is an example entry with one [link](test-2.2)"); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(true) + .create_internal_targets(true) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + { + let base_links = base.get_internal_links(); + assert!(base_links.is_ok()); + let base_links : Vec<_> = base_links.unwrap().collect(); + + assert_eq!(1, base_links.len()); + assert_eq!("test-2.2", base_links[0].to_str().unwrap()); + } + + { + let link = store.get(PathBuf::from("test-2.2")).unwrap().unwrap(); + let link_links = link.get_internal_links(); + assert!(link_links.is_ok()); + let link_links : Vec<_> = link_links.unwrap().collect(); + + assert_eq!(1, link_links.len()); + assert_eq!("test-2.1", link_links[0].to_str().unwrap()); + } + } } From 24985a23841cbc3bd785f158dca0e30374943331 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Sep 2017 22:34:41 +0200 Subject: [PATCH 6/9] Add test for processing external link --- .../libimagentrymarkdown/src/processor.rs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 39bea4a4..6f130ebe 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -349,5 +349,53 @@ mod tests { assert_eq!("test-2.1", link_links[0].to_str().unwrap()); } } + + #[test] + fn test_process_one_external_link() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-5.1")).unwrap(); + *base.get_content_mut() = format!("An [example](http://example.com) is here."); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(true) + .create_internal_targets(true) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + // The hash of "http://example.com" processed in the `libimagentrylink` way. + let expected_link = "links/external/9c17e047f58f9220a7008d4f18152fee4d111d14"; + { + let base_links = base.get_internal_links(); + assert!(base_links.is_ok()); + let base_links : Vec<_> = base_links.unwrap().collect(); + + assert_eq!(1, base_links.len()); + assert_eq!(expected_link, base_links[0].to_str().unwrap()); + } + + let entries = store.entries(); + assert!(entries.is_ok()); + let entries : Vec<_> = entries.unwrap().collect(); + + assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries); + + { + let link = store.get(PathBuf::from(expected_link)).unwrap().unwrap(); + let link_links = link.get_internal_links(); + assert!(link_links.is_ok()); + let link_links : Vec<_> = link_links.unwrap().collect(); + + assert_eq!(1, link_links.len()); + assert_eq!("test-5.1", link_links[0].to_str().unwrap()); + } + } } From 98aa7bf71631cd8cf71d5902263cc19e19ed39ae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Sep 2017 16:27:59 +0200 Subject: [PATCH 7/9] Add test to test ref creation --- .../libimagentrymarkdown/src/processor.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 6f130ebe..c656b42d 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -397,5 +397,35 @@ mod tests { assert_eq!("test-5.1", link_links[0].to_str().unwrap()); } } + + #[test] + fn test_process_one_ref() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-5.1")).unwrap(); + + // As the ref target must exist, we're using /etc/hosts here + *base.get_content_mut() = format!("An [example ref](file:///etc/hosts) is here."); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(false) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(true); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + let entries = store.entries(); + assert!(entries.is_ok()); + let entries : Vec<_> = entries.unwrap().collect(); + + assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries); + println!("{:?}", entries); + } } From 18ebd568dc371843cf229e0c94b319ab07a6658c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 7 Sep 2017 20:47:41 +0200 Subject: [PATCH 8/9] Add test for processing two refs --- .../libimagentrymarkdown/src/processor.rs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index c656b42d..6a6f7376 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -427,5 +427,38 @@ mod tests { assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries); println!("{:?}", entries); } + + #[test] + fn test_process_two_refs() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-5.1")).unwrap(); + + // As the ref target must exist, we're using /etc/hosts here + *base.get_content_mut() = format!( + r#"An [example ref](file:///etc/hosts) + is [here](file:///etc/group)."# + ); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(false) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(true); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + let entries = store.entries(); + assert!(entries.is_ok()); + let entries : Vec<_> = entries.unwrap().collect(); + + assert_eq!(3, entries.len(), "Expected 3 links, got: {:?}", entries); + println!("{:?}", entries); + } } From 250bc3d8a7d83a3109af47c76932eb9ceb4d1e66 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 7 Sep 2017 20:48:56 +0200 Subject: [PATCH 9/9] Add tests for processing with appropriate setting switched off --- .../libimagentrymarkdown/src/processor.rs | 89 ++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 6a6f7376..4ac570c8 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -364,7 +364,7 @@ mod tests { let processor = LinkProcessor::default() .process_internal_links(true) .create_internal_targets(true) - .process_external_links(false) + .process_external_links(true) .process_refs(false); let result = processor.process(&mut base, &store); @@ -460,5 +460,92 @@ mod tests { assert_eq!(3, entries.len(), "Expected 3 links, got: {:?}", entries); println!("{:?}", entries); } + + #[test] + fn test_process_refs_with_ref_processing_switched_off() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-5.1")).unwrap(); + + // As the ref target must exist, we're using /etc/hosts here + *base.get_content_mut() = format!( + r#"An [example ref](file:///etc/hosts) + is [here](file:///etc/group)."# + ); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(false) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + let entries = store.entries(); + assert!(entries.is_ok()); + let entries : Vec<_> = entries.unwrap().collect(); + + assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries); + println!("{:?}", entries); + } + + #[test] + fn test_process_external_link_with_external_link_processing_switched_off() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-5.1")).unwrap(); + *base.get_content_mut() = format!("An [example](http://example.com) is here."); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + let processor = LinkProcessor::default() + .process_internal_links(true) + .create_internal_targets(true) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + let entries = store.entries(); + assert!(entries.is_ok()); + let entries : Vec<_> = entries.unwrap().collect(); + + assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries); + } + + #[test] + fn test_process_one_existing_file_linked_with_internal_processing_switched_off() { + setup_logging(); + let store = get_store(); + + let mut base = store.create(PathBuf::from("test-2.1")).unwrap(); + *base.get_content_mut() = format!("This is an example entry with one [link](test-2.2)"); + + let update = store.update(&mut base); + assert!(update.is_ok()); + + // immediately drop as we don't need this entry right now + let _ = store.create(PathBuf::from("test-2.2")).unwrap(); + + let processor = LinkProcessor::default() + .process_internal_links(false) + .create_internal_targets(false) + .process_external_links(false) + .process_refs(false); + + let result = processor.process(&mut base, &store); + assert!(result.is_ok(), "Should be Ok(()): {:?}", result); + + assert_eq!(2, store.entries().unwrap().collect::>().len()); + } + }