Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,67 @@ | ||
use super::md; | ||
use eyre::Result; | ||
use stencila_schema::Node; | ||
use stencila_schema::{ | ||
BlockContent, CodeBlock, CodeChunk, CodeExpression, CodeFragment, Delete, Emphasis, | ||
InlineContent, Node, NontextualAnnotation, Paragraph, Strong, Subscript, Superscript, | ||
}; | ||
|
||
/// Encode a `Node` to R Markdown | ||
pub fn encode(node: &Node) -> Result<String> { | ||
// TODO: Any necessary translations of Markdown to RMarkdown | ||
md::encode(node) | ||
let mut node = node.clone(); | ||
if let Node::Article(article) = &mut node { | ||
if let Some(content) = &mut article.content { | ||
transform_blocks(content) | ||
} | ||
} | ||
md::encode(&node) | ||
} | ||
|
||
fn transform_blocks(blocks: &mut Vec<BlockContent>) { | ||
for block in blocks { | ||
match block { | ||
// Code chunks are transformed to code blocks with curly braced language | ||
BlockContent::CodeChunk(CodeChunk { | ||
programming_language, | ||
text, | ||
.. | ||
}) => { | ||
*block = BlockContent::CodeBlock(CodeBlock { | ||
programming_language: Some(Box::new(["{", programming_language, "}"].concat())), | ||
text: text.to_string(), | ||
..Default::default() | ||
}) | ||
} | ||
// Transform the inline content of other block types | ||
BlockContent::Paragraph(Paragraph { content, .. }) => transform_inlines(content), | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
fn transform_inlines(inlines: &mut Vec<InlineContent>) { | ||
for inline in inlines { | ||
match inline { | ||
// Code expressions are transformed to code fragments prefixed with the language | ||
InlineContent::CodeExpression(CodeExpression { | ||
programming_language, | ||
text, | ||
.. | ||
}) => { | ||
*inline = InlineContent::CodeFragment(CodeFragment { | ||
text: [programming_language, " ", text].concat(), | ||
..Default::default() | ||
}) | ||
} | ||
// Recursively transform other inlines | ||
InlineContent::Delete(Delete { content, .. }) | ||
| InlineContent::Emphasis(Emphasis { content, .. }) | ||
| InlineContent::Subscript(Subscript { content, .. }) | ||
| InlineContent::Superscript(Superscript { content, .. }) | ||
| InlineContent::Strong(Strong { content, .. }) | ||
| InlineContent::NontextualAnnotation(NontextualAnnotation { content, .. }) => { | ||
transform_inlines(content) | ||
} | ||
_ => (), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters