This commit is contained in:
Noah Hellman 2022-11-28 20:30:18 +01:00
parent 680e8ef65f
commit 5d601c0fff

View file

@ -85,14 +85,20 @@ impl<'s> Parser<'s> {
})
.count();
let lines = &mut lines[blanklines..];
Block::parse(lines.iter().map(|sp| sp.of(self.src))).map_or(0, |(kind, span, len)| {
Block::parse(lines.iter().map(|sp| sp.of(self.src))).map_or(
blanklines,
|(kind, span, len)| {
let start = lines.get(0).map(|sp| sp.start()).unwrap();
let span = span.translate(start);
match &kind {
Block::Leaf(_) => {
Block::Leaf(l) => {
self.tree.enter(kind, span);
lines[0] = lines[0].with_start(span.end());
for line in lines.iter().take(len) {
let len = match l {
CodeBlock { .. } => len - 2,
_ => len,
};
for line in &lines[0..len] {
self.tree.elem(Atom::Inline, *line);
}
}
@ -130,7 +136,8 @@ impl<'s> Parser<'s> {
}
self.tree.exit();
blanklines + len
})
},
)
}
}
@ -139,12 +146,13 @@ impl Block {
fn parse<'b, I: Iterator<Item = &'b str>>(mut lines: I) -> Option<(Block, Span, usize)> {
lines.next().map(|l| {
let (kind, sp) = Block::start(l);
let line_count = 1
+ lines.take_while(|l| kind.continues(l)).count()
+ usize::from(matches!(
let has_end_delimiter = matches!(
kind,
Self::Leaf(CodeBlock { .. }) | Self::Container(Div { .. })
));
);
let line_count = 1
+ lines.take_while(|l| kind.continues(l)).count()
+ usize::from(has_end_delimiter);
(kind, sp, line_count)
})
}
@ -195,7 +203,7 @@ impl Block {
':' => Self::Container(Div { fence_length }),
_ => unreachable!(),
},
Span::by_len(start, line.len()),
Span::by_len(start, fence_length.into()),
)
})
})
@ -361,23 +369,35 @@ mod test {
);
}
/*
#[test]
fn parse_code_block() {
test_parse!(
concat!(
"```lang\n",
"```\n",
"l0\n",
"l1\n",
"```", //
),
(Event::Enter(Leaf(CodeBlock { fence_length: 3 })), "```lang\n"),
(Event::Element(Inline), "l0\n"),
(Event::Element(Inline), "l1\n"),
(Event::Exit, "```lang\n"),
(Enter(Leaf(CodeBlock { fence_length: 3 })), "```"),
(Element(Inline), "\n"),
(Element(Inline), "l0\n"),
(Exit, "```"),
);
test_parse!(
concat!(
"````lang\n",
"l0\n",
"```\n",
" l1\n",
"````", //
),
(Enter(Leaf(CodeBlock { fence_length: 4 })), "````"),
(Element(Inline), "lang\n"),
(Element(Inline), "l0\n"),
(Element(Inline), "```\n"),
(Element(Inline), " l1\n"),
(Exit, "````"),
);
}
*/
macro_rules! test_block {
($src:expr, $kind:expr, $str:expr, $len:expr $(,)?) => {