Skip to content

Commit

Permalink
feat(Markdown encoding): Encode code expressions and chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 31, 2021
1 parent c2c7148 commit da64b64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rust/src/methods/encode/md.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions rust/tests/strategies/mod.rs
Expand Up @@ -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::<String>()
},
programming_language in match freedom {
Freedom::Min => "python",
Freedom::Low => r"[A-Za-z0-9-]+",
_ => any::<String>()
}
) -> 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)(
Expand Down Expand Up @@ -236,6 +258,7 @@ pub fn inline_content(freedom: Freedom) -> impl Strategy<Value = InlineContent>
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(),
Expand Down Expand Up @@ -499,6 +522,7 @@ pub fn thematic_break() -> impl Strategy<Value = BlockContent> {
pub fn block_content(freedom: Freedom) -> impl Strategy<Value = BlockContent> {
Union::new(vec![
code_block(freedom).boxed(),
code_chunk(freedom).boxed(),
heading(freedom).boxed(),
list(freedom).boxed(),
paragraph(freedom).boxed(),
Expand Down

0 comments on commit da64b64

Please sign in to comment.