From 6d19eeb0f4dd3e50d380972b5a373de9b9a4940d Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 18 Mar 2023 17:32:07 -0400 Subject: [PATCH] Add parsers for lines that are indented less than or equal to the current line item's indent. --- src/parser/plain_list.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 5175322..07fd7d1 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -10,11 +10,14 @@ use nom::combinator::not; use nom::combinator::opt; use nom::combinator::peek; use nom::combinator::recognize; +use nom::combinator::verify; use nom::multi::many0_count; use nom::multi::many1; use nom::sequence::tuple; use super::combinator::context_many_till; +use super::error::CustomError; +use super::error::MyError; use super::error::Res; use super::paragraph::paragraph_end; use super::parser_context::ContextElement; @@ -115,3 +118,26 @@ pub fn item_end<'r, 's>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, &' recognize(tuple((line_ending, peek(item_matcher)))), ))(i) } + +fn line_indented_lte<'r, 's>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, &'s str> { + let current_item_indent_level: &usize = get_context_item_indent(context).ok_or( + nom::Err::Error(CustomError::MyError(MyError("NotInPlainListItem"))), + )?; + + let matched = recognize(verify( + tuple((line_ending::<&str, _>, space0, anychar)), + |(_newline, _space0, _anychar)| _space0.len() <= *current_item_indent_level, + ))(i)?; + + Ok(matched) +} + +fn get_context_item_indent<'r, 's>(context: Context<'r, 's>) -> Option<&'r usize> { + for thing in context.iter() { + match thing.get_data() { + ContextElement::ListItem(depth) => return Some(depth), + _ => {} + }; + } + None +}