Fix get_contents for headlines.

This commit is contained in:
Tom Alexander 2023-12-15 13:14:49 -05:00
parent bb472b63cc
commit ffa5349f25
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 9 additions and 26 deletions

View File

@ -66,6 +66,7 @@ fn _heading<'b, 'g, 'r, 's>(
let (remaining, pre_headline) = headline(context, input, parent_star_count)?; let (remaining, pre_headline) = headline(context, input, parent_star_count)?;
let section_matcher = bind_context!(section, context); let section_matcher = bind_context!(section, context);
let heading_matcher = bind_context!(heading(pre_headline.star_count), context); let heading_matcher = bind_context!(heading(pre_headline.star_count), context);
let contents_begin = remaining;
let (remaining, maybe_section) = let (remaining, maybe_section) =
opt(map(section_matcher, DocumentElement::Section))(remaining)?; opt(map(section_matcher, DocumentElement::Section))(remaining)?;
let (remaining, _ws) = opt(tuple((start_of_line, many0(blank_line))))(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 is_archived = pre_headline.tags.contains(&"ARCHIVE");
let contents = get_consumed(contents_begin, remaining);
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
@ -112,6 +114,11 @@ fn _heading<'b, 'g, 'r, 's>(
scheduled, scheduled,
deadline, deadline,
closed, closed,
contents: if contents.len() > 0 {
Some(Into::<&str>::into(contents))
} else {
None
},
}, },
)) ))
} }

View File

@ -35,6 +35,7 @@ pub struct Heading<'s> {
pub scheduled: Option<Timestamp<'s>>, pub scheduled: Option<Timestamp<'s>>,
pub deadline: Option<Timestamp<'s>>, pub deadline: Option<Timestamp<'s>>,
pub closed: Option<Timestamp<'s>>, pub closed: Option<Timestamp<'s>>,
pub contents: Option<&'s str>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -119,32 +120,7 @@ impl<'s> StandardProperties<'s> for Heading<'s> {
} }
fn get_contents<'b>(&'b self) -> Option<&'s str> { fn get_contents<'b>(&'b self) -> Option<&'s str> {
let first_child = self.children.first(); self.contents
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)],
)
}
}
} }
fn get_post_blank(&self) -> PostBlank { fn get_post_blank(&self) -> PostBlank {