brentthorne | 275dbbe | 2019-02-05 13:31:30 -0500 | [diff] [blame] | 1 | # Most here is from thesisdown for the posterdown_pdf option |
Brent Thorne | 06ae3ac | 2018-11-30 15:16:47 -0500 | [diff] [blame] | 2 | find_file <- function(template, file) { |
| 3 | template <- system.file("rmarkdown", "templates", template, file, |
Marc Kupietz | e7b1945 | 2024-05-31 14:43:10 +0200 | [diff] [blame^] | 4 | package = "posterdown.ids") |
Brent Thorne | 06ae3ac | 2018-11-30 15:16:47 -0500 | [diff] [blame] | 5 | if (template == "") { |
| 6 | stop("Couldn't find template file ", template, "/", file, call. = FALSE) |
| 7 | } |
| 8 | |
| 9 | template |
| 10 | } |
| 11 | |
| 12 | find_resource <- function(template, file) { |
| 13 | find_file(template, file.path("resources", file)) |
| 14 | } |
| 15 | |
| 16 | knitr_fun <- function(name) utils::getFromNamespace(name, 'knitr') |
| 17 | |
| 18 | output_asis <- knitr_fun('output_asis') |
| 19 | |
| 20 | merge_list <- function(x, y) { |
| 21 | fun <- knitr_fun('merge_list') |
| 22 | fun(as.list(x), y) |
| 23 | } |
| 24 | |
| 25 | #' Render a pandoc template. |
| 26 | #' |
| 27 | #' This is a hacky way to access the pandoc templating engine. |
| 28 | #' |
| 29 | #' @param metadata A named list containing metadata to pass to template. |
| 30 | #' @param template Path to a pandoc template. |
| 31 | #' @param output Path to save output. |
| 32 | #' @return (Invisibly) The path of the generate file. |
| 33 | #' @examples |
| 34 | #' x <- posterdown:::template_pandoc( |
| 35 | #' list(preamble = "%abc", filename = "wickham"), |
| 36 | #' posterdown:::find_resource("posterdown_generic", "template.tex"), |
| 37 | #' tempfile() |
| 38 | #' ) |
| 39 | #' if (interactive()) file.show(x) |
| 40 | #' @noRd |
| 41 | template_pandoc <- function(metadata, template, output, verbose = FALSE) { |
| 42 | tmp <- tempfile(fileext = ".md") |
| 43 | on.exit(unlink(tmp)) |
| 44 | |
| 45 | cat("---\n", file = tmp) |
| 46 | cat(yaml::as.yaml(metadata), file = tmp, append = TRUE) |
| 47 | cat("---\n", file = tmp, append = TRUE) |
| 48 | cat("\n", file = tmp, append = TRUE) |
| 49 | |
| 50 | rmarkdown::pandoc_convert(tmp, "markdown", output = output, |
| 51 | options = paste0("--template=", template), verbose = verbose) |
| 52 | |
| 53 | invisible(output) |
| 54 | } |