Skip to content

Commit

Permalink
option to preserve yaml front matter for md_document
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Mar 27, 2014
1 parent 190246f commit 9a5d207
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
22 changes: 21 additions & 1 deletion R/md_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' \href{http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html}{pandoc's
#' markdown} for details.
#'
#' @param preserve_yaml Preserve YAML front matter in final document.
#'
#' @param fig_retina Scaling to perform for retina displays. Defaults to
#' \code{NULL} which performs no scaling. A setting of 2 will work for all
#' widely used retina displays, but will also result in the output of
Expand Down Expand Up @@ -41,6 +43,7 @@
#'
#' @export
md_document <- function(variant = "markdown_strict",
preserve_yaml = FALSE,
toc = FALSE,
toc_depth = 3,
fig_width = 7,
Expand All @@ -66,11 +69,28 @@ md_document <- function(variant = "markdown_strict",
# pandoc args
args <- c(args, pandoc_args)

# add post_processor for yaml preservation
if (preserve_yaml) {
post_processor <- function(input_file, output_file, verbose) {
input_lines <- readLines(input_file, warn = FALSE)
partitioned <- partition_yaml_front_matter(input_lines)
if (!is.null(partitioned$front_matter)) {
output_lines <- c(partitioned$front_matter,
"",
readLines(output_file, warn = FALSE))
writeLines(output_lines, output_file, useBytes = TRUE)
}
}
} else {
post_processor <- NULL
}

# return format
output_format(
knitr = knitr_options_html(fig_width, fig_height, fig_retina),
pandoc = pandoc_options(to = variant,
from = from_rmarkdown(),
args = args)
args = args),
post_processor = post_processor
)
}
10 changes: 7 additions & 3 deletions man/md_document.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
\alias{md_document}
\title{Convert to a markdown document}
\usage{
md_document(variant = "markdown_strict", toc = FALSE, toc_depth = 3,
fig_width = 7, fig_height = 5, fig_retina = NULL, includes = NULL,
data_dir = NULL, pandoc_args = NULL)
md_document(variant = "markdown_strict", preserve_yaml = FALSE,
toc = FALSE, toc_depth = 3, fig_width = 7, fig_height = 5,
fig_retina = NULL, includes = NULL, data_dir = NULL,
pandoc_args = NULL)
}
\arguments{
\item{variant}{Markdown variant to produce (defaults to
Expand All @@ -16,6 +17,9 @@ md_document(variant = "markdown_strict", toc = FALSE, toc_depth = 3,
\href{http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html}{pandoc's
markdown} for details.}
\item{preserve_yaml}{Preserve YAML front matter in final
document.}
\item{fig_retina}{Scaling to perform for retina displays.
Defaults to \code{NULL} which performs no scaling. A
setting of 2 will work for all widely used retina
Expand Down

0 comments on commit 9a5d207

Please sign in to comment.