Move the text element parser into the text module.

This commit is contained in:
Tom Alexander 2022-12-18 03:11:56 -05:00
parent 46672b40b2
commit 4ac3a47deb
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 34 additions and 31 deletions

View File

@ -1,6 +1,7 @@
use crate::parser::parser_with_context::parser_with_context;
use super::combinator::context_many_till;
use super::document::in_section;
use super::error::CustomError;
use super::error::MyError;
use super::error::Res;
@ -8,10 +9,9 @@ use super::parser_context::ChainBehavior;
use super::parser_context::ContextElement;
use super::parser_context::ExitMatcherNode;
use super::text::symbol;
use super::text::text_element;
use super::text::Bold;
use super::text::TextElement;
use super::text_element_parser::flat_text_element;
use super::text_element_parser::in_section;
use super::token::Token;
use super::Context;
use nom::branch::alt;
@ -29,7 +29,7 @@ pub fn bold<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Bold<'
}))
.with_additional_node(ContextElement::Context("bold"));
let (remaining, captured) = recognize(tuple((bold_start, |i| {
context_many_till(&parser_context, flat_text_element, context_bold_end)(i)
context_many_till(&parser_context, text_element, context_bold_end)(i)
})))(i)?;
let ret = Bold { contents: captured };
Ok((remaining, ret))

View File

@ -47,24 +47,3 @@ pub fn in_section<'s, 'r, 'x>(context: Context<'r, 's>, section_name: &'x str) -
}
false
}
pub fn flat_text_element<'s, 'r>(
context: Context<'r, 's>,
i: &'s str,
) -> Res<&'s str, TextElement<'s>> {
not(|i| context.check_exit_matcher(i))(i)?;
let bold_matcher = parser_with_context!(bold)(&context);
let link_matcher = parser_with_context!(link)(&context);
alt((
map(bold_matcher, TextElement::Bold),
map(link_matcher, TextElement::Link),
map(span, TextElement::Span),
map(symbol("*"), TextElement::Symbol),
map(symbol("["), TextElement::Symbol),
map(symbol("]"), TextElement::Symbol),
map(space, TextElement::Space),
map(line_break, TextElement::LineBreak),
))(i)
}

View File

@ -1,6 +1,7 @@
use crate::parser::parser_with_context::parser_with_context;
use super::combinator::context_many_till;
use super::document::in_section;
use super::error::CustomError;
use super::error::MyError;
use super::error::Res;
@ -8,10 +9,9 @@ use super::parser_context::ChainBehavior;
use super::parser_context::ContextElement;
use super::parser_context::ExitMatcherNode;
use super::text::symbol;
use super::text::text_element;
use super::text::Link;
use super::text::TextElement;
use super::text_element_parser::flat_text_element;
use super::text_element_parser::in_section;
use super::Context;
use nom::combinator::map;
use nom::combinator::recognize;
@ -25,7 +25,7 @@ pub fn link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<'
}))
.with_additional_node(ContextElement::Context("link"));
let (remaining, captured) = recognize(tuple((link_start, |i| {
context_many_till(&parser_context, flat_text_element, context_link_end)(i)
context_many_till(&parser_context, text_element, context_link_end)(i)
})))(i)?;
let ret = Link { contents: captured };
Ok((remaining, ret))

View File

@ -7,7 +7,7 @@ mod paragraph;
mod parser_context;
mod parser_with_context;
mod text;
mod text_element_parser;
mod document;
mod token;
pub use text_element_parser::document;
pub use document::document;
type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>;

View File

@ -5,9 +5,9 @@ use super::parser_context::ContextElement;
use super::parser_context::ExitMatcherNode;
use super::text::blank_line;
use super::text::line_break;
use super::text::text_element;
use super::text::Paragraph;
use super::text::TextElement;
use super::text_element_parser::flat_text_element;
use super::token::Token;
use super::Context;
use nom::branch::alt;
@ -27,7 +27,7 @@ pub fn paragraph<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, P
}))
.with_additional_node(ContextElement::StartOfParagraph);
let (remaining, (many, till)) =
context_many_till(&paragraph_context, flat_text_element, context_paragraph_end)(i)?;
context_many_till(&paragraph_context, text_element, context_paragraph_end)(i)?;
let many = many
.into_iter()
.filter_map(|token| match token {

View File

@ -1,12 +1,18 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::alphanumeric1;
use nom::character::complete::line_ending;
use nom::character::complete::space1;
use nom::combinator::map;
use nom::combinator::not;
use nom::combinator::recognize;
use nom::multi::many_till;
use super::bold::bold;
use super::error::Res;
use super::link::link;
use super::parser_with_context::parser_with_context;
use super::Context;
#[derive(Debug)]
pub enum TextElement<'a> {
@ -92,3 +98,21 @@ pub fn blank_line(input: &str) -> Res<&str, BlankLine> {
|contents| BlankLine { contents },
)(input)
}
pub fn text_element<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, TextElement<'s>> {
not(|i| context.check_exit_matcher(i))(i)?;
let bold_matcher = parser_with_context!(bold)(&context);
let link_matcher = parser_with_context!(link)(&context);
alt((
map(bold_matcher, TextElement::Bold),
map(link_matcher, TextElement::Link),
map(span, TextElement::Span),
map(symbol("*"), TextElement::Symbol),
map(symbol("["), TextElement::Symbol),
map(symbol("]"), TextElement::Symbol),
map(space, TextElement::Space),
map(line_break, TextElement::LineBreak),
))(i)
}