blob: dba1921810e8dc156e35029dde440b7a9d1d4901 [file] [log] [blame]
brentthorne275dbbe2019-02-05 13:31:30 -05001# Most here is from thesisdown for the posterdown_pdf option
Brent Thorne06ae3ac2018-11-30 15:16:47 -05002find_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
12find_resource <- function(template, file) {
13 find_file(template, file.path("resources", file))
14}
15
16knitr_fun <- function(name) utils::getFromNamespace(name, 'knitr')
17
18output_asis <- knitr_fun('output_asis')
19
20merge_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
41template_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}