organic/src/parser/text_element_parser.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2022-07-16 03:26:49 +00:00
//! A single element of text.
2022-07-17 01:32:23 +00:00
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::text::paragraph_end;
2022-07-16 03:26:49 +00:00
use super::nom_context::NomContext;
use super::text::line_break;
use super::text::space;
use super::text::span;
use super::text::symbol;
use super::text::Res;
2022-07-16 03:26:49 +00:00
use super::text::TextElement;
use nom::branch::alt;
use nom::combinator::map;
use nom::combinator::not;
use nom::error::VerboseError;
2022-07-17 01:32:23 +00:00
use nom::multi::many_till;
2022-07-16 03:26:49 +00:00
use nom::IResult;
pub fn flat_text_element<'a, F>(
i: &'a str,
context: &mut NomContext<F>,
) -> Res<&'a str, TextElement<'a>>
where
F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
{
2022-07-17 01:36:06 +00:00
not(&mut context.fail_matcher)(i)?;
alt((
// map(
// BoldParser::new(slf.context.fail_matcher.clone()),
// TextElement::Bold,
// ),
// map(
// LinkParser::new(slf.context.fail_matcher.clone()),
// 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)
}
2022-07-17 01:32:23 +00:00
pub fn paragraph(input: &str) -> Res<&str, (Vec<TextElement>, &str)> {
let initial_context = NomContext::new(&paragraph_end);
let text_element_parser = parser_with_context!(flat_text_element)(initial_context);
many_till(text_element_parser, paragraph_end)(input)
}