forked from mitchelloharawild/vitae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_document.R
66 lines (60 loc) · 2.59 KB
/
cv_document.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#' Output format for vitae
#'
#' This output format provides support for including LaTeX dependencies and
#' bibliography entries in extension of the `rmarkdown::pdf_document()` format.
#'
#' @inheritParams rmarkdown::pdf_document
#' @inheritParams bookdown::pdf_document2
#' @param ... Arguments passed to rmarkdown::pdf_document().
#' @param pandoc_vars Pandoc variables to be passed to the template.
#'
#' @keywords internal
#' @export
cv_document <- function(..., pandoc_args = NULL, pandoc_vars = NULL,
base_format = rmarkdown::pdf_document) {
for (i in seq_along(pandoc_vars)){
pandoc_args <- c(pandoc_args, rmarkdown::pandoc_variable_arg(names(pandoc_vars)[[i]], pandoc_vars[[i]]))
}
# Inject multiple-bibliographies lua filter
mult_bib <- file.path(tempdir(), "multiple-bibliographies.lua")
if(rmarkdown::pandoc_version() <= package_version("2.7.3")) {
warn(sprintf("Detected pandoc version %s, which may cause issues with bibliography_entries().
Please update pandoc if you have any issues knitting bibliographies (this can be done by updating RStudio).", rmarkdown::pandoc_version()))
}
cat(
gsub("<<PANDOC_PATH>>", rmarkdown::find_pandoc()$dir, fixed = TRUE,
readLines(system.file("multiple-bibliographies.lua", package = "vitae", mustWork = TRUE), encoding = "UTF-8")),
file = mult_bib, sep = "\n"
)
pandoc_args <- c(
c(rbind("--lua-filter", mult_bib)),
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.
meta_nocite <- vapply(knit_meta, inherits, logical(1L), "vitae_nocite")
metadata$nocite <- c(metadata$nocite, paste0("@", do.call(c, knit_meta[meta_nocite]), 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
}
copy_supporting_files <- function(template) {
path <- system.file("rmarkdown", "templates", template, "skeleton", package = "vitae")
files <- list.files(path)
# Copy class and style files
for (f in files[files != "skeleton.Rmd"]) {
if (!file.exists(f)) {
file.copy(file.path(path, f), ".", recursive = TRUE)
}
}
}