From 417320b27c5633e3c52b9a9f1f24113a47783de8 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Tue, 13 Oct 2020 18:01:20 -0700 Subject: [PATCH] FEAT Parsing the content for references This features all of the reference types that I commonly use, including the ORG mode [[Title]], #CamelCase, #lisp-case, and #colon:case. There are still edge cases around capitalization and the mixing of symbols and numbers, and I'll have to hack on those until I'm satisfied. --- server/nm-store/src/reference_parser.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/nm-store/src/reference_parser.rs b/server/nm-store/src/reference_parser.rs index 0dbd19b..98c39f2 100644 --- a/server/nm-store/src/reference_parser.rs +++ b/server/nm-store/src/reference_parser.rs @@ -33,7 +33,7 @@ pub(crate) fn find_links(document: &str) -> Vec { finder.iter_nodes(root, &|node| { lazy_static! { - static ref RE_REFERENCES: BytesRegex = BytesRegex::new(r"(\[\[([^\]]+)\]\]|(\#[\w\-]+))").unwrap(); + static ref RE_REFERENCES: BytesRegex = BytesRegex::new(r"(\[\[([^\]]+)\]\]|(\#[:\w\-]+))").unwrap(); } match &node.data.borrow().value { @@ -55,7 +55,7 @@ fn recase(title: &str) -> String { static ref RE_PASS1: Regex = Regex::new(r"(?P.)(?P[A-Z][a-z]+)").unwrap(); static ref RE_PASS2: Regex = Regex::new(r"(?P[[:lower:]]|\d)(?P[[:upper:]])").unwrap(); static ref RE_PASS4: Regex = Regex::new(r"(?P[a-z])(?P\d)").unwrap(); - static ref RE_PASS3: Regex = Regex::new(r"(_|-| )+").unwrap(); + static ref RE_PASS3: Regex = Regex::new(r"(:|_|-| )+").unwrap(); } // This should panic if misused, so... :-) @@ -65,7 +65,7 @@ fn recase(title: &str) -> String { let pass = RE_PASS1.replace_all(&pass, "$s $n"); let pass = RE_PASS4.replace_all(&pass, "$s $n"); let pass = RE_PASS2.replace_all(&pass, "$s $n"); - RE_PASS3.replace_all(&pass, " ").to_string() + RE_PASS3.replace_all(&pass, " ").trim().to_string() } fn build_page_titles(references: &Vec) -> Vec { @@ -80,6 +80,7 @@ fn build_page_titles(references: &Vec) -> Vec { _ => "".to_string(), } }) + .filter(|s| s.len() > 0) .collect() } @@ -96,7 +97,10 @@ mod tests { - #Test3 - #TestFourAndSo - #Test-six-is-six +- #recipe:entree - # +- #-_- +- #--Prefixed - [[]] But *[[Test Seven]]* isn't. And *#Test_Eight____is_Messed-up* @@ -110,10 +114,12 @@ Right? [[ "Test 3", "Test Four And So", "Test six is six", + "recipe entree", + "Prefixed", "Test Seven", "Test Eight is Messed up", "Test Bite Me", ]; - assert!(res.iter().eq(expected.iter())); + assert!(res.iter().eq(expected.iter()), "{:?}", res); } }