From f32ca0b01d6e3e93e0f21152268dd10a28c153ed Mon Sep 17 00:00:00 2001 From: mitchelloharawild Date: Thu, 13 Jun 2024 03:53:33 +1000 Subject: [PATCH] Fixed the body of the document being duplicated --- NEWS.md | 1 + R/cv_document.R | 60 ++++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/NEWS.md b/NEWS.md index ac3828ec..00e64449 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,7 @@ * Fixed issue with multiple `brief_entries()` producing overlapping text in `awesomecv` (#258). +* Fixed the body of the document being duplicated since knitr v1.46 (#254). # vitae 0.5.4 diff --git a/R/cv_document.R b/R/cv_document.R index 4079b951..0c8faf2a 100644 --- a/R/cv_document.R +++ b/R/cv_document.R @@ -21,32 +21,40 @@ cv_document <- function(..., pandoc_args = NULL, pandoc_vars = NULL, pandoc_args ) - out <- base_format(..., pandoc_args = pandoc_args) - pre <- out$pre_processor - out$pre_processor <- function (metadata, input_file, runtime, knit_meta, - files_dir, output_dir){ - pre(metadata, input_file, runtime, knit_meta, - files_dir, output_dir) - - # Add citations to front matter yaml, there may be a better way to do this. - # For example, @* wildcard. Keeping as is to avoid unintended side effects. - meta_nocite <- vapply(knit_meta, inherits, logical(1L), "vitae_nocite") - - bib_files <- lapply(knit_meta[meta_nocite], function(x) x$file) - names(bib_files) <- vapply(bib_files, rlang::hash_file, character(1L)) - metadata$bibliography <- bib_files - - bib_ids <- unique(unlist(lapply(knit_meta[meta_nocite], function(x) x$id))) - metadata$nocite <- c(metadata$nocite, paste0("@", bib_ids, collapse = ", ")) - if(is.null(metadata$csl)) metadata$csl <- system.file("vitae.csl", package = "vitae", mustWork = TRUE) - - body <- partition_yaml_front_matter(xfun::read_utf8(input_file))$body - xfun::write_utf8( - c("---", yaml::as.yaml(metadata), "---", body), - input_file - ) - } - out + base <- base_format(...) + rmarkdown::output_format( + knitr = rmarkdown::knitr_options(), + pandoc = rmarkdown::pandoc_options( + to = base$pandoc$to, + args = pandoc_args, + latex_engine = base$pandoc$latex_engine + ), + + pre_processor = function (metadata, input_file, runtime, knit_meta, + files_dir, output_dir) { + # Add citations to front matter yaml, there may be a better way to do this. + # For example, @* wildcard. Keeping as is to avoid unintended side effects. + meta_nocite <- vapply(knit_meta, inherits, logical(1L), "vitae_nocite") + + bib_files <- lapply(knit_meta[meta_nocite], function(x) x$file) + names(bib_files) <- vapply(bib_files, rlang::hash_file, character(1L)) + metadata$bibliography <- bib_files + + bib_ids <- unique(unlist(lapply(knit_meta[meta_nocite], function(x) x$id))) + metadata$nocite <- c(metadata$nocite, paste0("@", bib_ids, collapse = ", ")) + if(is.null(metadata$csl)) metadata$csl <- system.file("vitae.csl", package = "vitae", mustWork = TRUE) + + body <- partition_yaml_front_matter(xfun::read_utf8(input_file))$body + xfun::write_utf8( + c("---", yaml::as.yaml(metadata), "---", body), + input_file + ) + + return(NULL) + }, + + base_format = base + ) } copy_supporting_files <- function(template) {