105 lines
3.3 KiB
Rust
105 lines
3.3 KiB
Rust
//! A single element of text.
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
use crate::parser::text::paragraph_end;
|
|
|
|
use super::nom_context::OrgModeContext;
|
|
use super::nom_context::OrgModeContextTree;
|
|
use super::text::bold_end;
|
|
use super::text::bold_start;
|
|
use super::text::line_break;
|
|
use super::text::link_end;
|
|
use super::text::link_start;
|
|
use super::text::space;
|
|
use super::text::span;
|
|
use super::text::symbol;
|
|
use super::text::Bold;
|
|
use super::text::Link;
|
|
use super::text::Res;
|
|
use super::text::TextElement;
|
|
use nom::branch::alt;
|
|
use nom::combinator::map;
|
|
use nom::combinator::not;
|
|
use nom::combinator::recognize;
|
|
use nom::error::ErrorKind;
|
|
use nom::error::ParseError;
|
|
use nom::error::VerboseError;
|
|
use nom::multi::many_till;
|
|
use nom::sequence::tuple;
|
|
use nom::IResult;
|
|
use tracing::instrument;
|
|
use tracing::trace;
|
|
|
|
fn flat_text_element<'s, 'r>(
|
|
i: &'s str,
|
|
context: &'r OrgModeContext<'r>,
|
|
) -> Res<&'s str, TextElement<'s>> {
|
|
not(|i| context.match_fail(i))(i)?;
|
|
|
|
let bold_matcher = parser_with_context!(flat_bold)(context);
|
|
let link_matcher = parser_with_context!(flat_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)
|
|
}
|
|
|
|
fn recognize_bold_end(input: &str) -> Res<&str, &str> {
|
|
recognize(bold_end)(input)
|
|
}
|
|
|
|
fn flat_bold<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, Bold<'s>> {
|
|
let new_context = context.with_additional_fail_matcher(&recognize_bold_end);
|
|
let text_element_parser = parser_with_context!(flat_text_element)(&new_context);
|
|
let (remaining, captured) = recognize(tuple((
|
|
bold_start,
|
|
many_till(text_element_parser, bold_end),
|
|
)))(i)?;
|
|
let ret = Bold { contents: captured };
|
|
Ok((remaining, ret))
|
|
}
|
|
|
|
fn recognize_link_end(input: &str) -> Res<&str, &str> {
|
|
recognize(link_end)(input)
|
|
}
|
|
|
|
fn flat_link<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, Link<'s>> {
|
|
let new_context = context.with_additional_fail_matcher(&recognize_link_end);
|
|
let text_element_parser = parser_with_context!(flat_text_element)(&new_context);
|
|
let (remaining, captured) = recognize(tuple((
|
|
link_start,
|
|
many_till(text_element_parser, link_end),
|
|
)))(i)?;
|
|
let ret = Link { contents: captured };
|
|
Ok((remaining, ret))
|
|
}
|
|
|
|
pub fn paragraph<'s, 'r>(
|
|
i: &'s str,
|
|
context: &'r OrgModeContext<'r>,
|
|
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
|
|
// not(eof)(i)?;
|
|
let paragraph_context = context.with_additional_fail_matcher(¶graph_end);
|
|
let text_element_parser = parser_with_context!(flat_text_element)(¶graph_context);
|
|
let ret = many_till(text_element_parser, paragraph_end)(i);
|
|
trace!("Returning from paragraph with {:#?}", ret);
|
|
if let Ok((remaining, (text_elements, end_of_paragraph))) = &ret {
|
|
if text_elements.len() == 0 {
|
|
return Err(nom::Err::Error(nom::error::VerboseError::from_error_kind(
|
|
remaining,
|
|
ErrorKind::ManyTill,
|
|
)));
|
|
}
|
|
}
|
|
ret
|
|
}
|