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, |
| 4 | package = "posterdown") |
| 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 | } |
| 55 | |
| 56 | |
| 57 | # Call rmarkdown::pdf_document and mark the return value as inheriting pdf_document |
| 58 | inherit_pdf_document <- function(...) { |
| 59 | fmt <- rmarkdown::pdf_document(...) |
| 60 | fmt$inherits <- "pdf_document" |
| 61 | fmt |
| 62 | } |
| 63 | |
| 64 | # Helper function to create a custom format derived from pdf_document |
| 65 | # that includes a custom LaTeX template and custom CSL definition |
| 66 | pdf_document_format <- function(..., format, template, csl) { |
| 67 | |
| 68 | # base format |
| 69 | fmt <- inherit_pdf_document(..., template = find_resource(format, template)) |
| 70 | |
| 71 | # add csl to pandoc_args |
| 72 | fmt$pandoc$args <- c(fmt$pandoc$args, |
| 73 | "--csl", |
| 74 | rmarkdown::pandoc_path_arg(find_resource(format, csl))) |
| 75 | |
| 76 | |
| 77 | # return format |
| 78 | fmt |
| 79 | } |