Damien Elmes 2020-08-12 19:28:26 +10:00
parent 4086042970
commit 85e2db8ab0

View file

@ -107,13 +107,31 @@ fn classify_handle(s: &str) -> Token {
static ALT_HANDLEBAR_DIRECTIVE: &str = "{{=<% %>=}}"; static ALT_HANDLEBAR_DIRECTIVE: &str = "{{=<% %>=}}";
fn legacy_text_token(s: &str) -> nom::IResult<&str, Token> { fn legacy_text_token(s: &str) -> nom::IResult<&str, Token> {
map( if s.is_empty() {
verify( return Err(nom::Err::Error(nom::error::make_error(
alt((take_until("{{"), take_until("<%"), rest)), s,
|out: &str| !out.is_empty(), nom::error::ErrorKind::TakeUntil,
), )));
Token::Text, }
)(s) // if we locate a starting normal or alternate handlebar, use
// whichever one we found first
let normal_result: nom::IResult<&str, &str> = take_until("{{")(s);
let (normal_remaining, normal_span) = normal_result.unwrap_or_else(|_e| ("", s));
let alt_result: nom::IResult<&str, &str> = take_until("<%")(s);
let (alt_remaining, alt_span) = alt_result.unwrap_or_else(|_e| ("", s));
match (normal_span.len(), alt_span.len()) {
(0, 0) => {
// neither handlebar kind found
map(rest, Token::Text)(s)
}
(n, a) => {
if n < a {
Ok((normal_remaining, Token::Text(normal_span)))
} else {
Ok((alt_remaining, Token::Text(alt_span)))
}
}
}
} }
fn legacy_next_token(input: &str) -> nom::IResult<&str, Token> { fn legacy_next_token(input: &str) -> nom::IResult<&str, Token> {
@ -966,6 +984,30 @@ mod test {
} }
] ]
); );
let input = "
{{=<% %>=}}
{{#foo}}
<%Front%>
{{/foo}}
";
assert_eq!(
PT::from_text(input).unwrap().0,
vec![
Text("\n".into()),
Conditional {
key: "foo".into(),
children: vec![
Text("\n".into()),
Replacement {
key: "Front".into(),
filters: vec![]
},
Text("\n".into())
]
},
Text("\n".into())
]
);
} }
#[test] #[test]