From 159ce0f6726ef0e3d252c3fc20c435e2bdb4b368 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Tue, 18 Aug 2020 07:14:27 -0400 Subject: [PATCH] Fix toml parse error line number (kind of) (#1138) - Minor change to how frontmatter is detected so that toml issues correct line numbers - split_content now returns (&str, &str) --- components/front_matter/src/lib.rs | 14 +++++++------- components/library/src/content/page.rs | 2 +- components/library/src/content/section.rs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/components/front_matter/src/lib.rs b/components/front_matter/src/lib.rs index 83f24476..4890da34 100644 --- a/components/front_matter/src/lib.rs +++ b/components/front_matter/src/lib.rs @@ -13,7 +13,7 @@ pub use section::SectionFrontMatter; lazy_static! { static ref PAGE_RE: Regex = - Regex::new(r"^[[:space:]]*\+\+\+\r?\n((?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap(); + Regex::new(r"^[[:space:]]*\+\+\+(\r?\n(?s).*?(?-s))\+\+\+\r?\n?((?s).*(?-s))$").unwrap(); } #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] @@ -37,7 +37,7 @@ pub enum InsertAnchor { /// Split a file between the front matter and its content /// Will return an error if the front matter wasn't found -fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> { +fn split_content<'c>(file_path: &Path, content: &'c str) -> Result<(&'c str, &'c str)> { if !PAGE_RE.is_match(content) { bail!( "Couldn't find front matter in `{}`. Did you forget to add `+++`?", @@ -50,15 +50,15 @@ fn split_content(file_path: &Path, content: &str) -> Result<(String, String)> { // caps[0] is the full match // caps[1] => front matter // caps[2] => content - Ok((caps[1].to_string(), caps[2].to_string())) + Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str())) } /// Split a file between the front matter and its content. /// Returns a parsed `SectionFrontMatter` and the rest of the content -pub fn split_section_content( +pub fn split_section_content<'c>( file_path: &Path, - content: &str, -) -> Result<(SectionFrontMatter, String)> { + content: &'c str, +) -> Result<(SectionFrontMatter, &'c str)> { let (front_matter, content) = split_content(file_path, content)?; let meta = SectionFrontMatter::parse(&front_matter).map_err(|e| { Error::chain( @@ -71,7 +71,7 @@ pub fn split_section_content( /// Split a file between the front matter and its content /// Returns a parsed `PageFrontMatter` and the rest of the content -pub fn split_page_content(file_path: &Path, content: &str) -> Result<(PageFrontMatter, String)> { +pub fn split_page_content<'c>(file_path: &Path, content: &'c str) -> Result<(PageFrontMatter, &'c str)> { let (front_matter, content) = split_content(file_path, content)?; let meta = PageFrontMatter::parse(&front_matter).map_err(|e| { Error::chain( diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index 5199c013..a7233643 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -136,7 +136,7 @@ impl Page { page.lang = page.file.find_language(config)?; - page.raw_content = content; + page.raw_content = content.to_string(); let (word_count, reading_time) = get_reading_analytics(&page.raw_content); page.word_count = Some(word_count); page.reading_time = Some(reading_time); diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 0694969c..16f4ee19 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -107,7 +107,7 @@ impl Section { let (meta, content) = split_section_content(file_path, content)?; let mut section = Section::new(file_path, meta, base_path); section.lang = section.file.find_language(config)?; - section.raw_content = content; + section.raw_content = content.to_string(); let (word_count, reading_time) = get_reading_analytics(§ion.raw_content); section.word_count = Some(word_count); section.reading_time = Some(reading_time);