Support string-based text markup (code and verbatim).

This commit is contained in:
Tom Alexander 2023-04-22 20:57:30 -04:00
parent 036f4add4a
commit e6c5670a85
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,5 +1,6 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::anychar;
use nom::character::complete::line_ending;
use nom::character::complete::one_of;
use nom::character::complete::space0;
@ -23,10 +24,12 @@ use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::get_one_before;
use crate::parser::Bold;
use crate::parser::Code;
use crate::parser::Italic;
use crate::parser::Object;
use crate::parser::StrikeThrough;
use crate::parser::Underline;
use crate::parser::Verbatim;
#[tracing::instrument(ret, level = "debug")]
pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Object<'s>> {
@ -38,8 +41,8 @@ pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s
parser_with_context!(strike_through)(context),
Object::StrikeThrough,
),
// map(parser_with_context!(verbatim)(context), Object::Verbatim),
// map(parser_with_context!(code)(context), Object::Code),
map(parser_with_context!(verbatim)(context), Object::Verbatim),
map(parser_with_context!(code)(context), Object::Code),
))(input)
}
@ -78,6 +81,22 @@ pub fn strike_through<'r, 's>(
Ok((remaining, StrikeThrough { source, children }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn verbatim<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Verbatim<'s>> {
let text_markup_string_specialized = text_markup_string("+");
let (remaining, contents) = text_markup_string_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, Verbatim { source, contents }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn code<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Code<'s>> {
let text_markup_string_specialized = text_markup_string("+");
let (remaining, contents) = text_markup_string_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, Code { source, contents }))
}
fn text_markup_object(
marker_symbol: &str,
) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, Vec<Object<'s>>> {
@ -114,6 +133,42 @@ fn _text_markup_object<'r, 's, 'x>(
Ok((remaining, children))
}
fn text_markup_string(
marker_symbol: &str,
) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> {
let marker_symbol = marker_symbol.to_owned();
move |context: Context, input: &str| _text_markup_string(context, input, marker_symbol.as_str())
}
#[tracing::instrument(ret, level = "debug")]
fn _text_markup_string<'r, 's, 'x>(
context: Context<'r, 's>,
input: &'s str,
marker_symbol: &'x str,
) -> Res<&'s str, &'s str> {
let (remaining, _) = pre(context, input)?;
let (remaining, open) = marker(remaining)?;
let text_markup_end_specialized = text_markup_end(open);
let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &text_markup_end_specialized,
}));
let (remaining, contents) = recognize(verify(
many_till(
anychar,
parser_with_context!(exit_matcher_parser)(&parser_context),
),
|(children, _exit_contents)| !children.is_empty(),
))(remaining)?;
// TODO: Sometimes its plain text, not objects
let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
let (remaining, _trailing_whitespace) = space0(remaining)?;
Ok((remaining, contents))
}
#[tracing::instrument(ret, level = "debug")]
pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> {
let document_root = context.get_document_root().unwrap();