Make sure text markup doesn't have interior spaces.

This commit is contained in:
Tom Alexander 2023-04-22 22:34:37 -04:00
parent da76d3714c
commit 9968aedd74
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 28 additions and 0 deletions

View File

@ -2,9 +2,11 @@ use nom::branch::alt;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::character::complete::anychar; use nom::character::complete::anychar;
use nom::character::complete::line_ending; use nom::character::complete::line_ending;
use nom::character::complete::multispace1;
use nom::character::complete::one_of; use nom::character::complete::one_of;
use nom::character::complete::space0; use nom::character::complete::space0;
use nom::combinator::map; use nom::combinator::map;
use nom::combinator::not;
use nom::combinator::peek; use nom::combinator::peek;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
@ -30,6 +32,7 @@ use crate::parser::Object;
use crate::parser::StrikeThrough; use crate::parser::StrikeThrough;
use crate::parser::Underline; use crate::parser::Underline;
use crate::parser::Verbatim; use crate::parser::Verbatim;
use crate::parser::util::preceded_by_whitespace;
#[tracing::instrument(ret, level = "debug")] #[tracing::instrument(ret, level = "debug")]
pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Object<'s>> { pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Object<'s>> {
@ -112,6 +115,7 @@ fn _text_markup_object<'r, 's, 'x>(
) -> Res<&'s str, Vec<Object<'s>>> { ) -> Res<&'s str, Vec<Object<'s>>> {
let (remaining, _) = pre(context, input)?; let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?;
let text_markup_end_specialized = text_markup_end(open); let text_markup_end_specialized = text_markup_end(open);
let parser_context = let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
@ -148,6 +152,7 @@ fn _text_markup_string<'r, 's, 'x>(
) -> Res<&'s str, &'s str> { ) -> Res<&'s str, &'s str> {
let (remaining, _) = pre(context, input)?; let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?;
let text_markup_end_specialized = text_markup_end(open); let text_markup_end_specialized = text_markup_end(open);
let parser_context = let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
@ -208,6 +213,7 @@ fn _text_markup_end<'r, 's, 'x>(
input: &'s str, input: &'s str,
marker_symbol: &'x str, marker_symbol: &'x str,
) -> Res<&'s str, &'s str> { ) -> Res<&'s str, &'s str> {
not(parser_with_context!(preceded_by_whitespace)(context))(input)?;
let (remaining, _marker) = terminated( let (remaining, _marker) = terminated(
tag(marker_symbol), tag(marker_symbol),
peek(parser_with_context!(post)(context)), peek(parser_with_context!(post)(context)),

View File

@ -151,6 +151,28 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
Ok((input, ())) Ok((input, ()))
} }
/// Check that we are at the start of a line
#[tracing::instrument(ret, level = "debug")]
pub fn preceded_by_whitespace<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, ()> {
let document_root = context.get_document_root().unwrap();
let preceding_character = get_one_before(document_root, input)
.map(|slice| slice.chars().next())
.flatten();
match preceding_character {
Some('\n') | Some('\r') | Some(' ') | Some('\t') => {}
// If None, we are at the start of the file which is not allowed
None | Some(_) => {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Not preceded by whitespace.",
))));
}
};
Ok((input, ()))
}
/// Pull one non-whitespace character. /// Pull one non-whitespace character.
/// ///
/// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace. /// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace.