diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index ced63e8b..ca184aa3 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -1,11 +1,6 @@ use nom::branch::alt; -use nom::combinator::recognize; use nom::error::VerboseError; use nom::IResult; -use std::cell::RefCell; -use std::rc::Rc; - -use super::text::bold_end; #[derive(Clone)] pub struct NomContext @@ -27,7 +22,7 @@ where { pub fn new(fail_matcher: F) -> Self { NomContext { - fail_matcher: fail_matcher, + fail_matcher, can_match_bold: true, can_match_link: true, } diff --git a/src/parser/text.rs b/src/parser/text.rs index 5bbe3a57..31c56cd7 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -24,6 +24,7 @@ use nom::sequence::tuple; use nom::IResult; use super::nom_context::NomContext; +use super::parser_with_context::parser_with_context; use super::text_element_parser::paragraph; pub type Res = IResult>; @@ -139,5 +140,6 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> { } pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { - many1(paragraph)(input) + let initial_context = NomContext::new(¶graph_end); + many1(parser_with_context!(paragraph)(initial_context))(input) } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 59c67f94..abb6eea7 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -74,8 +74,14 @@ where )(i) } -pub fn paragraph(input: &str) -> Res<&str, (Vec, &str)> { - let initial_context = NomContext::new(¶graph_end); - let text_element_parser = parser_with_context!(flat_text_element)(initial_context); - many_till(text_element_parser, paragraph_end)(input) +pub fn paragraph<'a, F>( + i: &'a str, + context: &mut NomContext, +) -> Res<&'a str, (Vec>, &'a str)> +where + F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>, + F: Clone, +{ + let text_element_parser = parser_with_context!(flat_text_element)(context.clone()); + many_till(text_element_parser, paragraph_end)(i) }