Is it possible to convert rmarkdown to pandoc markdown while preserving code chunk options as code block attributes?

Viewed 82

Example Problem

Given an input file of the form:

# Testing


Here is the first block:

```{r}
library(data.table)
library(ggplot2)
library(rstanarm) 
```

```{r test}
library(data.table)
```

```{r  eval=FALSE}
library(ggplot2)
library(rstanarm)
library(arm)
library(faraway) 
```

The command:

library(rmarkdown)

# You can apparently create custom markdown variants too
# "markdown" is pandoc markdown according to the documentation
render("test.rmd",
       md_document(variant="markdown",
                   md_extensions = c("+fenced_code_attributes"),
                   preserve_yaml = TRUE))

yields:

# Testing

Here is the first block:

``` {.r}
library(data.table)
library(ggplot2)
library(rstanarm) 
```

``` {.r}
library(data.table)
```

``` {.r}
library(ggplot2)
library(rstanarm)
library(arm)
library(faraway) 
```

I'm wondering if it's possible to preserve the attributes such that you'd get:

# Testing

Here is the first block:

``` {.r}
library(data.table)
library(ggplot2)
library(rstanarm) 
```

``` {.r .test}
library(data.table)
```

``` {.r eval=FALSE}
library(ggplot2)
library(rstanarm)
library(arm)
library(faraway) 
```

which I believe is valid pandoc markdown and might even be created by rmarkdown itself internally somewhere.

These resources on fenced_code_attributes seem useful:

Why?

I'd like to retain these attributes and easily manipulate them myself using pandoc filters, but can't find a way to keep them. It doesn't seem to be explicitly covered in the rmarkdown documentation either: "These options are mostly useful to HTML output. There are cases in which the attributes may be useful to other output formats, but these cases are relatively rare. The attributes need to be supported by either Pandoc (such as the .numberLines attribute, which works for both HTML and LaTeX output), or a third-party package (usually via a Lua filter, as introduced in Section 4.20)."

Why not just use pandoc?

I don't think that rmarkdown is totally compliant with any form of pandoc supported markdown. For example, the block labeled "test" does not appear to comply with any notion of fenced_code_attributes I've seen because the word test is not written as .test.

0 Answers
Related