block: specify lang in code block event

instead of using span
This commit is contained in:
Noah Hellman 2023-04-29 18:51:47 +02:00
parent e90594f2b7
commit dbedeeb5ee
2 changed files with 19 additions and 17 deletions

View file

@ -76,7 +76,7 @@ pub enum Leaf<'s> {
/// Span is language specifier. /// Span is language specifier.
/// Each inline is a line. /// Each inline is a line.
CodeBlock, CodeBlock { language: &'s str },
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -256,7 +256,9 @@ impl<'s> TreeParser<'s> {
Kind::Fenced { Kind::Fenced {
kind: FenceKind::CodeBlock(..), kind: FenceKind::CodeBlock(..),
.. ..
} => Block::Leaf(CodeBlock), } => Block::Leaf(CodeBlock {
language: span.of(self.src),
}),
Kind::Fenced { Kind::Fenced {
kind: FenceKind::Div, kind: FenceKind::Div,
.. ..
@ -1433,9 +1435,9 @@ mod test {
fn parse_code_block() { fn parse_code_block() {
test_parse!( test_parse!(
concat!("```\n", "l0\n"), concat!("```\n", "l0\n"),
(Enter(Leaf(CodeBlock)), "",), (Enter(Leaf(CodeBlock { language: "" })), "",),
(Inline, "l0\n"), (Inline, "l0\n"),
(Exit(Leaf(CodeBlock)), "",), (Exit(Leaf(CodeBlock { language: "" })), "",),
); );
test_parse!( test_parse!(
concat!( concat!(
@ -1445,9 +1447,9 @@ mod test {
"\n", "\n",
"para\n", // "para\n", //
), ),
(Enter(Leaf(CodeBlock)), ""), (Enter(Leaf(CodeBlock { language: "" })), ""),
(Inline, "l0\n"), (Inline, "l0\n"),
(Exit(Leaf(CodeBlock)), ""), (Exit(Leaf(CodeBlock { language: "" })), ""),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Leaf(Paragraph)), ""), (Enter(Leaf(Paragraph)), ""),
(Inline, "para"), (Inline, "para"),
@ -1461,11 +1463,11 @@ mod test {
" l1\n", " l1\n",
"````", // "````", //
), ),
(Enter(Leaf(CodeBlock)), "lang"), (Enter(Leaf(CodeBlock { language: "lang" })), "lang"),
(Inline, "l0\n"), (Inline, "l0\n"),
(Inline, "```\n"), (Inline, "```\n"),
(Inline, " l1\n"), (Inline, " l1\n"),
(Exit(Leaf(CodeBlock)), "lang"), (Exit(Leaf(CodeBlock { language: "lang" })), "lang"),
); );
test_parse!( test_parse!(
concat!( concat!(
@ -1476,12 +1478,12 @@ mod test {
"bbb\n", // "bbb\n", //
"```\n", // "```\n", //
), ),
(Enter(Leaf(CodeBlock)), ""), (Enter(Leaf(CodeBlock { language: "" })), ""),
(Inline, "a\n"), (Inline, "a\n"),
(Exit(Leaf(CodeBlock)), ""), (Exit(Leaf(CodeBlock { language: "" })), ""),
(Enter(Leaf(CodeBlock)), ""), (Enter(Leaf(CodeBlock { language: "" })), ""),
(Inline, "bbb\n"), (Inline, "bbb\n"),
(Exit(Leaf(CodeBlock)), ""), (Exit(Leaf(CodeBlock { language: "" })), ""),
); );
test_parse!( test_parse!(
concat!( concat!(
@ -1490,10 +1492,10 @@ mod test {
" block\n", " block\n",
"~~~\n", // "~~~\n", //
), ),
(Enter(Leaf(CodeBlock)), ""), (Enter(Leaf(CodeBlock { language: "" })), ""),
(Inline, "code\n"), (Inline, "code\n"),
(Inline, " block\n"), (Inline, " block\n"),
(Exit(Leaf(CodeBlock)), ""), (Exit(Leaf(CodeBlock { language: "" })), ""),
); );
} }

View file

@ -897,12 +897,12 @@ impl<'s> Parser<'s> {
.into(), .into(),
}, },
block::Leaf::DescriptionTerm => Container::DescriptionTerm, block::Leaf::DescriptionTerm => Container::DescriptionTerm,
block::Leaf::CodeBlock => { block::Leaf::CodeBlock { language } => {
self.verbatim = enter; self.verbatim = enter;
if let Some(format) = content.strip_prefix('=') { if let Some(format) = language.strip_prefix('=') {
Container::RawBlock { format } Container::RawBlock { format }
} else { } else {
Container::CodeBlock { language: content } Container::CodeBlock { language }
} }
} }
block::Leaf::TableCell(alignment) => Container::TableCell { block::Leaf::TableCell(alignment) => Container::TableCell {