use serde_json::{Map, Value};
use crate::grammar::{self, Meta, ParseError, RangeError};
use crate::markdown_it::js;
use crate::markdown_it::{PseudoShape, PseudoToken, Token};
pub const OPEN: &[u16] = &[b'{' as u16, b'%' as u16];
pub const CLOSE: &[u16] = &[b'%' as u16, b'}' as u16];
const NEWLINE: u16 = b'\n' as u16;
const QUOTE: u16 = b'"' as u16;
const BACKSLASH: u16 = b'\\' as u16;
pub fn is_identifier(s: &str) -> bool {
!s.is_empty()
&& s.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum States {
Normal,
String,
Escape,
}
pub fn find_tag_end(content: &[u16], start: usize) -> Option<usize> {
let mut state = States::Normal;
let mut pos = start;
while pos < content.len() {
let char = content[pos];
match state {
States::String => match char {
QUOTE => state = States::Normal,
BACKSLASH => state = States::Escape,
_ => {}
},
States::Escape => state = States::String,
States::Normal => {
if char == QUOTE {
state = States::String;
} else if js::starts_with(content, CLOSE, pos) {
return Some(pos);
}
}
}
pos += 1;
}
None
}
pub fn contains_markdoc_tag_in_url(content: &[u16], allowed_protocols: &[String]) -> bool {
let Some(before) = text_before_first_closed_tag(content) else {
return false;
};
allowed_protocols.iter().any(|protocol| {
let needle = js::to_utf16(&format!("{protocol}://"));
js::includes(before, &needle, 0)
})
}
pub fn text_before_first_closed_tag(content: &[u16]) -> Option<&[u16]> {
let mut pos = 0;
while pos < content.len() {
if !js::starts_with(content, OPEN, pos) {
pos += 1;
continue;
}
let end = find_tag_end(content, pos);
if end.is_none() {
pos += OPEN.len() - 1;
pos += 1;
continue;
}
let mut start = pos;
while start > 0 && !js::is_js_space(u32::from(content[start - 1])) {
start -= 1;
}
return Some(js::slice_to(content, start, pos));
}
None
}
struct TagResult {
kind: String,
nesting: Option<i8>,
meta: Value,
grammar_meta: Option<Meta>,
}
fn parse_tag(content: &[u16], line: usize, content_start: i64) -> Result<TagResult, RangeError> {
match grammar::parse_utf16(content) {
Ok(parsed) => Ok(TagResult {
kind: parsed.kind.as_str().to_owned(),
nesting: parsed.nesting,
meta: parsed.meta.to_json(),
grammar_meta: Some(parsed.meta),
}),
Err(ParseError::Range(error)) => Err(error),
Err(ParseError::Syntax(error)) => {
let point = |line: usize, character: i64| {
let mut o = Map::new();
o.insert("line".into(), line.into());
o.insert("character".into(), character.into());
Value::Object(o)
};
let mut location = Map::new();
location.insert(
"start".into(),
point(line, error.location.start.offset as i64 + content_start),
);
location.insert(
"end".into(),
point(
line.saturating_add(1),
error.location.end.offset as i64 + content_start,
),
);
Ok(TagResult {
kind: "error".to_owned(),
nesting: None,
meta: error_meta(&error, Value::Object(location)),
grammar_meta: None,
})
}
}
}
pub(crate) fn error_meta(error: &grammar::SyntaxError, location: Value) -> Value {
let mut inner = Map::new();
inner.insert(
"message".into(),
Value::String(error.message.to_string_lossy()),
);
inner.insert("location".into(), location);
let mut meta = Map::new();
meta.insert("error".into(), Value::Object(inner));
Value::Object(meta)
}
#[derive(Clone, Debug, PartialEq)]
pub struct SpannedToken {
pub token: Token,
pub start: usize,
pub end: i64,
}
struct NewlineCursor<'a> {
content: &'a [u16],
scanned: usize,
last: Option<usize>,
next: Option<(usize, Option<usize>)>,
}
impl<'a> NewlineCursor<'a> {
fn new(content: &'a [u16]) -> Self {
NewlineCursor {
content,
scanned: 0,
last: None,
next: None,
}
}
fn last_at_or_before(&mut self, pos: usize) -> Option<usize> {
let upto = pos.saturating_add(1).min(self.content.len());
if upto > self.scanned {
if let Some(i) = self.content[self.scanned..upto]
.iter()
.rposition(|&c| c == NEWLINE)
{
self.last = Some(self.scanned + i);
}
self.scanned = upto;
}
self.last
}
fn first_at_or_after(&mut self, from: usize) -> Option<usize> {
if let Some((asked, found)) = self.next {
if asked <= from && found.is_none_or(|f| f >= from) {
return found;
}
}
let start = from.min(self.content.len());
let found = self.content[start..]
.iter()
.position(|&c| c == NEWLINE)
.map(|i| start + i);
self.next = Some((from, found));
found
}
}
pub fn parse_tags(content: &[u16], first_line: usize) -> Result<Vec<SpannedToken>, RangeError> {
let mut line = first_line.saturating_add(1);
let mut output = Vec::new();
let mut start: usize = 0;
let mut newlines = NewlineCursor::new(content);
let mut pos: usize = 0;
while pos < content.len() {
if content[pos] == NEWLINE {
line = line.saturating_add(1);
pos += 1;
continue;
}
if !js::starts_with(content, OPEN, pos) {
pos += 1;
continue;
}
let end = find_tag_end(content, pos);
let Some(end) = end else {
pos += OPEN.len();
pos += 1;
continue;
};
let text = js::slice_to(content, pos, end + CLOSE.len());
let inner = js::slice_to(content, pos + OPEN.len(), end);
let line_start: i64 = newlines.last_at_or_before(pos).map_or(-1, |i| i as i64);
let line_end: i64 = newlines.first_at_or_after(end).map_or(-1, |i| i as i64);
let line_content = js::slice(content, line_start, Some(line_end));
let tag = parse_tag(js::trim(inner), line, pos as i64 - line_start)?;
let preceding_text_end: i64 = if js::trim(line_content) == text {
line_start
} else {
pos as i64
};
let preceding_text = js::slice(content, start as i64, Some(preceding_text_end));
let mut text_token = Token::new("text", "", 0);
text_token.content = js::to_string(preceding_text);
text_token.pseudo = Some(Box::new(PseudoToken {
start: start as i64,
end: pos as i64 - 1,
shape: PseudoShape::Text,
}));
output.push(SpannedToken {
token: text_token,
start,
end: pos as i64 - 1,
});
let mut tag_token = Token::new(&tag.kind, "", tag.nesting.unwrap_or(0));
tag_token.map = Some([line, line.saturating_add(1)]);
let mut position = Map::new();
position.insert("start".into(), (pos as i64 - line_start).into());
position.insert(
"end".into(),
(pos as i64 - line_start + text.len() as i64).into(),
);
tag_token.position = Some(Box::new(Value::Object(position)));
tag_token.info = js::to_string(text);
tag_token.meta = Some(tag.meta);
tag_token.grammar_meta = tag.grammar_meta.map(Box::new);
tag_token.pseudo = Some(Box::new(PseudoToken {
start: pos as i64,
end: (pos + text.len()) as i64 - 1,
shape: PseudoShape::Tag {
nesting: tag.nesting.is_some(),
},
}));
output.push(SpannedToken {
token: tag_token,
start: pos,
end: (pos + text.len()) as i64 - 1,
});
start = end + CLOSE.len();
pos = start; }
let mut text_token = Token::new("text", "", 0);
text_token.content = js::to_string(js::slice_to(content, start, content.len()));
text_token.pseudo = Some(Box::new(PseudoToken {
start: start as i64,
end: content.len() as i64 - 1,
shape: PseudoShape::Text,
}));
output.push(SpannedToken {
token: text_token,
start,
end: content.len() as i64 - 1,
});
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;
fn u(s: &str) -> Vec<u16> {
js::to_utf16(s)
}
#[test]
fn parse_tags_simple_example() {
let example = u("this is a {% foo blah=\"asdf\" %}test{% /foo %} of template parsing");
let output = parse_tags(&example, 0).unwrap();
let summary: Vec<(String, String, String, usize, i64)> = output
.iter()
.map(|t| {
(
t.token.kind.clone(),
t.token.content.clone(),
t.token.info.clone(),
t.start,
t.end,
)
})
.collect();
let s = |kind: &str, content: &str, info: &str, start: usize, end: i64| {
(kind.into(), content.into(), info.into(), start, end)
};
assert_eq!(
summary,
vec![
s("text", "this is a ", "", 0, 9),
s("tag_open", "", "{% foo blah=\"asdf\" %}", 10, 30),
s("text", "test", "", 31, 34),
s("tag_close", "", "{% /foo %}", 35, 44),
s("text", " of template parsing", "", 45, 64),
]
);
assert_eq!(output[1].token.nesting, 1);
assert_eq!(
output[1].token.meta,
Some(serde_json::json!({
"tag": "foo",
"attributes": [{"type": "attribute", "name": "blah", "value": "asdf"}]
}))
);
assert_eq!(output[3].token.nesting, -1);
assert_eq!(
output[3].token.meta,
Some(serde_json::json!({"tag": "foo"}))
);
}
#[test]
fn find_tag_end_inline_tags() {
let at = |s: &str, start: usize| find_tag_end(&u(s), start);
assert_eq!(at("# Testing {% #foo.bar baz=1 %}", 0), Some(28));
assert_eq!(
at("# Testing {% #foo.bar baz=\"example\" test=true %}", 0),
Some(46)
);
assert_eq!(
at(
"# Testing {% #foo.bar baz={test: 1, foo: {test: \"asdf{\"}} %}",
0
),
Some(58)
);
assert_eq!(at("{% foo %}", 0), Some(7));
assert_eq!(at("{% foo .bar.baz#test %}", 0), Some(21));
assert_eq!(at("{% foo test=\"this is a test\" %}", 0), Some(29));
assert_eq!(at("{% foo .bar#baz", 0), None);
assert_eq!(
at(
"{% #foo .bar .baz test=\"this} is \\\"{test}\\\" a test\" %} this is a test",
0
),
Some(52)
);
}
#[test]
fn find_tag_end_multiline_tags() {
let at = |s: &str| find_tag_end(&u(s), 0);
let simple = "\n {% test #foo.bar\n baz=1 %}\n ";
assert_eq!(at(simple), Some(46));
let with_string = "\n {% test #foo.bar\n baz=\"this is a test\"\n example=1 %}\n ";
assert_eq!(at(with_string), Some(85));
let escaped = "\n {% test #foo.bar\n baz=\"this \\\"is a test\"\n example=1 %}\n ";
assert_eq!(at(escaped), Some(87));
let brace = "\n {% test #foo.bar\n baz=\"this {is a test\"\n example=1 %}\n ";
assert_eq!(at(brace), Some(86));
let escapes = "\n {% test #foo.bar\n baz=\"th\\\"is {is a \\\\te\\\"st\"\n example=1 %}\n ";
assert_eq!(at(escapes), Some(92));
let object = "\n {% test #foo.bar\n foo={testing: \"this } is a test\", bar: {baz: 1}}\n example=1 another=\"test}\" %}\n ";
assert_eq!(at(object), Some(129));
let invalid = "\n {test #foo.bar\n foo={testing: \"this } is a test\", bar: {baz: 1}\n example=1 another=\"test}\"}\n ";
assert_eq!(at(invalid), None);
}
#[test]
fn parse_tags_does_not_hang_on_an_open_brace_in_a_code_block() {
let example = u("```\n{%a %b %c}\n```");
let output = parse_tags(&example, 0).unwrap();
assert_eq!(output.len(), 1);
assert_eq!(output[0].token.kind, "text");
assert_eq!(output[0].token.content, "```\n{%a %b %c}\n```");
assert_eq!((output[0].start, output[0].end), (0, 17));
}
#[test]
fn the_newline_cursor_answers_as_last_index_of_and_index_of() {
for len in 0..8usize {
for bits in 0..(1u32 << len) {
let content: Vec<u16> = (0..len)
.map(|i| if bits >> i & 1 == 1 { NEWLINE } else { 0x61 })
.collect();
for step in 1..4 {
let mut cursor = NewlineCursor::new(&content);
let mut pos = 0;
while pos < len + 2 {
let end = pos + step - 1;
assert_eq!(
cursor.last_at_or_before(pos),
js::last_index_of(&content, &[NEWLINE], Some(pos)),
"{content:?} lastIndexOf at {pos}"
);
assert_eq!(
cursor.first_at_or_after(end),
js::index_of(&content, &[NEWLINE], end),
"{content:?} indexOf from {end}"
);
pos += step;
}
}
}
}
}
#[test]
fn many_tags_on_one_line_are_linear() {
let tags = 40_000;
let content = u(&format!("a\n{}\nb", "{% x %}".repeat(tags)));
let output = parse_tags(&content, 3).unwrap();
assert_eq!(output.len(), 2 * tags + 1);
assert_eq!(output[0].token.content, "a\n");
assert_eq!(output[1].token.info, "{% x %}");
assert_eq!(
output[1].token.position.as_deref(),
Some(&serde_json::json!({"start": 1, "end": 8}))
);
assert_eq!(
output[2 * tags - 1].token.position.as_deref(),
Some(&serde_json::json!({"start": 1 + 7 * (tags - 1), "end": 1 + 7 * tags}))
);
assert_eq!(output[2 * tags].token.content, "\nb");
assert!(
output[1..2 * tags]
.iter()
.step_by(2)
.all(|t| t.token.map == Some([5, 6]))
);
}
#[test]
fn is_identifier_is_the_regex() {
for (s, ok) in [
("a", true),
("a-b_c9", true),
("", false),
("a b", false),
("a\n", false),
("é", false),
("a.b", false),
] {
assert_eq!(is_identifier(s), ok, "{s:?}");
}
}
#[test]
fn contains_markdoc_tag_in_url_looks_at_the_first_closed_tag_only() {
let http = ["http".to_owned(), "https".to_owned()];
let check = |s: &str| contains_markdoc_tag_in_url(&u(s), &http);
assert!(check("https://example.com/{% tag %}"));
assert!(check("[Link](https://{% $variable.custom_value %})"));
assert!(!check("The link is https://example.com. {% tag /%})"));
assert!(!check("[Link]({% tag %})"));
assert!(check("{% x \"https://a{% y %}"));
assert!(!check("{% x https://a{% y %}"));
assert!(!check("{% a %} https://x{% b %}"));
assert!(!check("https://a\u{a0}{% b %}"));
}
}