Debian Codegraph @

R/rtf_document.R

raw
1 #' Convert to an RTF document
2 #'
3 #' Format for converting from R Markdown to an RTF document.
4 #'
5 #' See the \href{https://yihui.org/rmarkdown/rich-text-format-document.html}{online
6 #' documentation} for additional details on using the \code{rtf_document} format.
7 #'
8 #' R Markdown documents can have optional metadata that is used to generate a
9 #' document header that includes the title, author, and date. For more details
10 #' see the documentation on R Markdown \link[=rmd_metadata]{metadata}.
11 #'
12 #' R Markdown documents also support citations. You can find more information on
13 #' the markdown syntax for citations in the
14 #' \href{https://pandoc.org/MANUAL.html#citations}{Bibliographies
15 #' and Citations} article in the online documentation.
16 #' @inheritParams pdf_document
17 #' @inheritParams html_document
18 #' @inheritParams word_document
19 #' @return R Markdown output format to pass to \code{\link{render}}
20 #' @examples
21 #' \dontrun{
22 #'
23 #' library(rmarkdown)
24 #'
25 #' # simple invocation
26 #' render("input.Rmd", rtf_document())
27 #'
28 #' # specify table of contents option
29 #' render("input.Rmd", rtf_document(toc = TRUE))
30 #' }
31 #' @export
32 rtf_document <- function(toc = FALSE,
33 toc_depth = 3,
34 number_sections = FALSE,
35 fig_width = 5,
36 fig_height = 4,
37 keep_md = FALSE,
38 md_extensions = NULL,
39 pandoc_args = NULL) {
40
41 # knitr options and hooks
42 knitr <- knitr_options(
43 opts_chunk = list(dev = 'png',
44 dpi = 96,
45 fig.width = fig_width,
46 fig.height = fig_height)
47 )
48
49 # build pandoc args
50 args <- c("--standalone")
51
52 # table of contents
53 args <- c(args, pandoc_toc_args(toc, toc_depth))
54
55 # pandoc args
56 args <- c(args, pandoc_args)
57
58 preserved_chunks <- character()
59
60 pre_processor <- function(metadata, input_file, runtime, knit_meta,
61 files_dir, output_dir) {
62 preserved_chunks <<- extract_preserve_chunks(input_file, knitr::extract_raw_output)
63 NULL
64 }
65
66 post_processor <- function(metadata, input_file, output_file, clean, verbose) {
67 output_str <- read_utf8(output_file)
68 output_res <- knitr::restore_raw_output(output_str, preserved_chunks)
69 if (!identical(output_str, output_res)) write_utf8(output_res, output_file)
70 output_file
71 }
72
73 # return output format
74 output_format(
75 knitr = knitr,
76 pandoc = pandoc_options(
77 to = "rtf",
78 from = from_rmarkdown(extensions = md_extensions),
79 args = args,
80 lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")
81 ),
82 keep_md = keep_md,
83 pre_processor = pre_processor,
84 post_processor = post_processor
85 )
86 }