From 565978225a57d0dcd11aaafc3d74abf403a1bffa Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 15 Dec 2023 17:11:22 -0500 Subject: [PATCH] Implement the new fields for table. --- src/parser/table.rs | 24 +++++++++++++++++------- src/types/greater_element.rs | 15 +++++++++++---- src/types/lesser_element.rs | 5 +++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/parser/table.rs b/src/parser/table.rs index 775b4c62..0d294e79 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -3,6 +3,7 @@ use nom::bytes::complete::is_not; use nom::bytes::complete::tag; use nom::character::complete::line_ending; use nom::character::complete::space0; +use nom::combinator::consumed; use nom::combinator::not; use nom::combinator::opt; use nom::combinator::peek; @@ -67,13 +68,13 @@ where let org_mode_table_row_matcher = parser_with_context!(org_mode_table_row)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); - let (remaining, (children, _exit_contents)) = - many_till(org_mode_table_row_matcher, exit_matcher)(remaining)?; + let (remaining, (contents, (children, _exit_contents))) = + consumed(many_till(org_mode_table_row_matcher, exit_matcher))(remaining)?; let (remaining, formulas) = many0(parser_with_context!(table_formula_keyword)(context))(remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); @@ -87,6 +88,8 @@ where ), formulas, children, + contents: Into::<&str>::into(contents), + post_blank: post_blank.map(Into::<&str>::into), }, )) } @@ -150,6 +153,7 @@ fn org_mode_table_row_rule<'b, 'g, 'r, 's>( TableRow { source: source.into(), children: Vec::new(), + contents: None, }, )) } @@ -164,8 +168,8 @@ fn org_mode_table_row_regular<'b, 'g, 'r, 's>( ) -> Res, TableRow<'s>> { start_of_line(input)?; let (remaining, _) = tuple((space0, tag("|")))(input)?; - let (remaining, children) = - many1(parser_with_context!(org_mode_table_cell)(context))(remaining)?; + let (remaining, (contents, children)) = + consumed(many1(parser_with_context!(org_mode_table_cell)(context)))(remaining)?; let (remaining, _tail) = recognize(tuple((space0, org_line_ending)))(remaining)?; let source = get_consumed(input, remaining); Ok(( @@ -173,6 +177,11 @@ fn org_mode_table_row_regular<'b, 'g, 'r, 's>( TableRow { source: source.into(), children, + contents: if contents.len() > 0 { + Some(Into::<&str>::into(contents)) + } else { + None + }, }, )) } @@ -194,12 +203,12 @@ fn org_mode_table_cell<'b, 'g, 'r, 's>( parser_with_context!(table_cell_set_object)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, _) = space0(input)?; - let (remaining, (children, _exit_contents)) = verify( + let (remaining, (contents, (children, _exit_contents))) = consumed(verify( many_till(table_cell_set_object_matcher, exit_matcher), |(children, exit_contents)| { !children.is_empty() || Into::<&str>::into(exit_contents).ends_with('|') }, - )(remaining)?; + ))(remaining)?; let (remaining, _tail) = org_mode_table_cell_end(&parser_context, remaining)?; @@ -210,6 +219,7 @@ fn org_mode_table_cell<'b, 'g, 'r, 's>( TableCell { source: source.into(), children, + contents: Into::<&str>::into(contents), }, )) } diff --git a/src/types/greater_element.rs b/src/types/greater_element.rs index bb848d97..bbb7ccdc 100644 --- a/src/types/greater_element.rs +++ b/src/types/greater_element.rs @@ -130,12 +130,15 @@ pub struct Table<'s> { pub affiliated_keywords: AffiliatedKeywords<'s>, pub formulas: Vec>, pub children: Vec>, + pub contents: &'s str, + pub post_blank: Option<&'s str>, } #[derive(Debug)] pub struct TableRow<'s> { pub source: &'s str, pub children: Vec>, + pub contents: Option<&'s str>, } #[derive(Debug)] @@ -322,11 +325,15 @@ impl<'s> StandardProperties<'s> for Table<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + Some(self.contents) } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } } @@ -336,11 +343,11 @@ impl<'s> StandardProperties<'s> for TableRow<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + self.contents } fn get_post_blank(&self) -> PostBlank { - todo!() + 0 } } diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index ab696b4d..32b0c81e 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -42,6 +42,7 @@ pub struct Comment<'s> { pub struct TableCell<'s> { pub source: &'s str, pub children: Vec>, + pub contents: &'s str, } #[derive(Debug)] @@ -257,11 +258,11 @@ impl<'s> StandardProperties<'s> for TableCell<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + Some(self.contents) } fn get_post_blank(&self) -> PostBlank { - todo!() + 0 } }