diff --git a/src/parser/document.rs b/src/parser/document.rs index 32c408f..7127f3c 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -3,8 +3,11 @@ use nom::bytes::complete::tag; use nom::character::complete::line_ending; use nom::character::complete::space1; use nom::combinator::eof; +use nom::combinator::map; use nom::combinator::not; use nom::combinator::recognize; +use nom::combinator::verify; +use nom::multi::many0; use nom::multi::many1; use nom::multi::many1_count; use nom::sequence::tuple; @@ -35,6 +38,7 @@ pub struct Document<'s> { #[derive(Debug)] pub struct Heading<'s> { pub source: &'s str, + pub stars: usize, pub children: Vec>, } @@ -96,7 +100,25 @@ fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> { not(|i| context.check_exit_matcher(i))(input)?; let (remaining, (star_count, _ws, title, _ws2)) = headline(context, input)?; - todo!() + let section_matcher = parser_with_context!(section)(context); + // TODO: This needs to only match headings below the current level + let heading_matcher = parser_with_context!(heading)(context); + let (remaining, children) = many0(alt(( + map( + verify(heading_matcher, |h| h.stars > star_count), + DocumentElement::Heading, + ), + map(section_matcher, DocumentElement::Section), + )))(remaining)?; + let source = get_consumed(input, remaining); + Ok(( + remaining, + Heading { + source: source, + stars: star_count, + children, + }, + )) } fn headline<'r, 's>(