From 7c1cecb2112e087e2ffb45937bea29ebf81df01e Mon Sep 17 00:00:00 2001 From: Reilly Tucker Siemens Date: Sun, 26 Mar 2017 02:52:06 -0700 Subject: [PATCH 1/2] Use lazy front-matter regex Using a greedy regex could lead to unintended consequences, like the inability to syntax highlight a unified diff. This resolves issues like that by using .*? instead of .* so that the closing +++ is matched as soon as possible. --- src/front_matter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/front_matter.rs b/src/front_matter.rs index c9d7732..84b6bef 100644 --- a/src/front_matter.rs +++ b/src/front_matter.rs @@ -11,7 +11,7 @@ use errors::{Result, ResultExt}; lazy_static! { - static ref PAGE_RE: Regex = Regex::new(r"^\n?\+\+\+\n((?s).*(?-s))\+\+\+\n?((?s).*(?-s))$").unwrap(); + static ref PAGE_RE: Regex = Regex::new(r"^\n?\+\+\+\n((?s).*?(?-s))\+\+\+\n?((?s).*(?-s))$").unwrap(); } From f0dab659cc2a6e6eb1704a1890c06b90a3b4b5ae Mon Sep 17 00:00:00 2001 From: Reilly Tucker Siemens Date: Sun, 26 Mar 2017 20:01:32 -0700 Subject: [PATCH 2/2] Add regression test for lazy front-matter matching --- tests/front_matter.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/front_matter.rs b/tests/front_matter.rs index 14291c6..bea21cf 100644 --- a/tests/front_matter.rs +++ b/tests/front_matter.rs @@ -184,6 +184,20 @@ date = "2002/10/12" assert_eq!(front_matter.title, "Title"); } +#[test] +fn test_can_split_content_lazily() { + let content = r#" ++++ +title = "Title" +description = "hey there" +date = "2002-10-02T15:00:00Z" ++++ ++++"#; + let (front_matter, content) = split_content(Path::new(""), content).unwrap(); + assert_eq!(content, "+++"); + assert_eq!(front_matter.title, "Title"); +} + #[test] fn test_error_if_cannot_locate_frontmatter() { let content = r#"