diff --git a/rust/src/methods/encode/md.rs b/rust/src/methods/encode/md.rs index 69f1a827e0..055080a11c 100644 --- a/rust/src/methods/encode/md.rs +++ b/rust/src/methods/encode/md.rs @@ -57,6 +57,12 @@ impl ToMd for Quote { } } +impl ToMd for CodeExpression { + fn to_md(&self) -> String { + ["`", &self.text, "`{", &self.programming_language, " exec}"].concat() + } +} + macro_rules! delimited_inline_text_to_md { ($type:ty, $delimiter:expr) => { impl ToMd for $type { @@ -119,6 +125,19 @@ impl ToMd for CodeBlock { } } +impl ToMd for CodeChunk { + fn to_md(&self) -> String { + [ + "```", + &self.programming_language, + " exec \n", + &self.text, + "\n```\n\n", + ] + .concat() + } +} + impl ToMd for List { fn to_md(&self) -> String { let ordered = matches!(&self.order, Some(ListOrder::Ascending)); @@ -327,6 +346,7 @@ impl ToMd for InlineContent { InlineContent::AudioObject(node) => node.to_md(), InlineContent::Boolean(node) => node.to_string(), //InlineContent::Cite(node) => node.to_md(), + InlineContent::CodeExpression(node) => node.to_md(), InlineContent::CodeFragment(node) => node.to_md(), InlineContent::Delete(node) => node.to_md(), InlineContent::Emphasis(node) => node.to_md(), @@ -354,6 +374,7 @@ impl ToMd for BlockContent { match self { //BlockContent::Claim(node) => node.to_md(), BlockContent::CodeBlock(node) => node.to_md(), + BlockContent::CodeChunk(node) => node.to_md(), BlockContent::Heading(node) => node.to_md(), BlockContent::List(node) => node.to_md(), //BlockContent::MathBlock(node) => node.to_md(), diff --git a/rust/tests/strategies/mod.rs b/rust/tests/strategies/mod.rs index e0d56f551e..df576e2f1f 100644 --- a/rust/tests/strategies/mod.rs +++ b/rust/tests/strategies/mod.rs @@ -100,6 +100,28 @@ prop_compose! { } } +prop_compose! { + /// Generate a code expression node with arbitrary text and programming language + pub fn code_expression(freedom: Freedom)( + text in match freedom { + Freedom::Min => r"text", + Freedom::Low => r"[A-Za-z0-9-_ ]+", + _ => any::() + }, + programming_language in match freedom { + Freedom::Min => "python", + Freedom::Low => r"[A-Za-z0-9-]+", + _ => any::() + } + ) -> InlineContent { + InlineContent::CodeExpression(CodeExpression{ + text, + programming_language, + ..Default::default() + }) + } +} + prop_compose! { /// Generate a code fragment node with arbitrary text and programming language pub fn code_fragment(freedom: Freedom)( @@ -236,6 +258,7 @@ pub fn inline_content(freedom: Freedom) -> impl Strategy audio_object_simple(freedom).boxed(), image_object_simple(freedom).boxed(), video_object_simple(freedom).boxed(), + code_expression(freedom).boxed(), code_fragment(freedom).boxed(), delete(freedom).boxed(), emphasis(freedom).boxed(), @@ -499,6 +522,7 @@ pub fn thematic_break() -> impl Strategy { pub fn block_content(freedom: Freedom) -> impl Strategy { Union::new(vec![ code_block(freedom).boxed(), + code_chunk(freedom).boxed(), heading(freedom).boxed(), list(freedom).boxed(), paragraph(freedom).boxed(),