From ffa5349f2591ff8a83db883a6af644bdea815cf9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 15 Dec 2023 13:14:49 -0500 Subject: [PATCH] Fix get_contents for headlines. --- src/parser/headline.rs | 7 +++++++ src/types/document.rs | 28 ++-------------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/parser/headline.rs b/src/parser/headline.rs index aca0fa3f..a3889b32 100644 --- a/src/parser/headline.rs +++ b/src/parser/headline.rs @@ -66,6 +66,7 @@ fn _heading<'b, 'g, 'r, 's>( let (remaining, pre_headline) = headline(context, input, parent_star_count)?; let section_matcher = bind_context!(section, context); let heading_matcher = bind_context!(heading(pre_headline.star_count), context); + let contents_begin = remaining; let (remaining, maybe_section) = opt(map(section_matcher, DocumentElement::Section))(remaining)?; let (remaining, _ws) = opt(tuple((start_of_line, many0(blank_line))))(remaining)?; @@ -91,6 +92,7 @@ fn _heading<'b, 'g, 'r, 's>( }; let is_archived = pre_headline.tags.contains(&"ARCHIVE"); + let contents = get_consumed(contents_begin, remaining); let source = get_consumed(input, remaining); Ok(( remaining, @@ -112,6 +114,11 @@ fn _heading<'b, 'g, 'r, 's>( scheduled, deadline, closed, + contents: if contents.len() > 0 { + Some(Into::<&str>::into(contents)) + } else { + None + }, }, )) } diff --git a/src/types/document.rs b/src/types/document.rs index 157e929e..f0bfa740 100644 --- a/src/types/document.rs +++ b/src/types/document.rs @@ -35,6 +35,7 @@ pub struct Heading<'s> { pub scheduled: Option>, pub deadline: Option>, pub closed: Option>, + pub contents: Option<&'s str>, } #[derive(Debug)] @@ -119,32 +120,7 @@ impl<'s> StandardProperties<'s> for Heading<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - let first_child = self.children.first(); - let last_child = self.children.last(); - match (first_child, last_child) { - (None, None) => None, - (None, Some(_)) | (Some(_), None) => unreachable!(), - (Some(first_child), Some(last_child)) => { - let first_child_offset = first_child.get_source().as_ptr() as usize; - let last_child_offset = { - let last_child_source = last_child.get_source(); - last_child_source.as_ptr() as usize + last_child_source.len() - }; - let source_offset = self.source.as_ptr() as usize; - debug_assert!(super::lesser_element::is_slice_of( - self.source, - first_child.get_source() - )); - debug_assert!(super::lesser_element::is_slice_of( - self.source, - last_child.get_source() - )); - Some( - &self.source[(first_child_offset - source_offset) - ..(last_child_offset - first_child_offset)], - ) - } - } + self.contents } fn get_post_blank(&self) -> PostBlank {