2023-04-24 18:02:37 -04:00
|
|
|
use nom::branch::alt;
|
2023-04-24 20:32:59 -04:00
|
|
|
use nom::bytes::complete::tag;
|
2023-04-24 18:02:37 -04:00
|
|
|
use nom::character::complete::anychar;
|
2023-04-24 20:32:59 -04:00
|
|
|
use nom::combinator::map;
|
2023-04-24 18:02:37 -04:00
|
|
|
use nom::combinator::peek;
|
2023-04-22 19:46:27 -04:00
|
|
|
use nom::combinator::recognize;
|
2023-04-24 18:02:37 -04:00
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::many_till;
|
2023-04-22 19:46:27 -04:00
|
|
|
|
2023-03-25 12:53:57 -04:00
|
|
|
use super::object::PlainText;
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-04-24 20:32:59 -04:00
|
|
|
use super::radio_link::RematchObject;
|
2023-03-25 12:53:57 -04:00
|
|
|
use super::Context;
|
2023-04-24 20:32:59 -04:00
|
|
|
use super::Object;
|
2023-04-21 18:36:01 -04:00
|
|
|
use crate::error::Res;
|
2023-04-22 19:46:27 -04:00
|
|
|
use crate::parser::object_parser::any_object_except_plain_text;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-04-24 18:02:37 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-03-25 12:53:57 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
pub fn plain_text<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, PlainText<'s>> {
|
2023-04-24 18:02:37 -04:00
|
|
|
let (remaining, source) = recognize(verify(
|
|
|
|
many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
parser_with_context!(plain_text_end)(context),
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
))(input)?;
|
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
PlainText {
|
|
|
|
source: source.into(),
|
|
|
|
},
|
|
|
|
))
|
2023-03-25 12:53:57 -04:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
fn plain_text_end<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-04-22 19:46:27 -04:00
|
|
|
recognize(parser_with_context!(any_object_except_plain_text)(context))(input)
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:04:01 -04:00
|
|
|
impl<'x> RematchObject<'x> for PlainText<'x> {
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-04-24 20:32:59 -04:00
|
|
|
fn rematch_object<'r, 's>(
|
2023-07-14 18:04:01 -04:00
|
|
|
&'x self,
|
2023-04-24 20:32:59 -04:00
|
|
|
_context: Context<'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
2023-04-24 20:32:59 -04:00
|
|
|
map(tag(self.source), |s| {
|
2023-08-23 00:30:26 -04:00
|
|
|
Object::PlainText(PlainText {
|
|
|
|
source: Into::<&str>::into(s),
|
|
|
|
})
|
2023-04-24 20:32:59 -04:00
|
|
|
})(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 12:53:57 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use nom::combinator::map;
|
|
|
|
|
2023-04-22 19:46:27 -04:00
|
|
|
use super::*;
|
2023-03-25 12:53:57 -04:00
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ContextTree;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use crate::parser::source::Source;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_text_simple() {
|
|
|
|
let input = "foobarbaz";
|
|
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
|
|
let document_context =
|
|
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
|
|
let plain_text_matcher = parser_with_context!(plain_text)(&document_context);
|
|
|
|
let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap();
|
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(result.get_source(), input);
|
|
|
|
}
|
|
|
|
}
|