Debian Codegraph @

R/odt_document.R

raw
1 #' Convert to an OpenDocument Text (ODT) document
2 #'
3 #' Format for converting from R Markdown to an ODT document.
4 #'
5 #' See the \href{https://yihui.org/rmarkdown/opendocument-text-document.html}{online
6 #' documentation} for additional details on using the \code{odt_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 #' @param reference_odt Use the specified file as a style reference in
19 #' producing an odt file. For best results, the reference odt should be a
20 #' modified version of an odt file produced using pandoc. Pass "default"
21 #' to use the rmarkdown default styles.
22 #' @return R Markdown output format to pass to \code{\link{render}}
23 #' @examples
24 #' \dontrun{
25 #' library(rmarkdown)
26 #'
27 #' # simple invocation
28 #' render("input.Rmd", odt_document())
29 #'
30 #' # specify an option for syntax highlighting
31 #' render("input.Rmd", odt_document(highlight = "zenburn"))
32 #' }
33 #' @export
34 odt_document <- function(number_sections = FALSE,
35 fig_width = 5,
36 fig_height = 4,
37 fig_caption = TRUE,
38 template = "default",
39 reference_odt = "default",
40 includes = NULL,
41 keep_md = FALSE,
42 md_extensions = NULL,
43 pandoc_args = NULL) {
44
45 # knitr options and hooks
46 knitr <- knitr_options(
47 opts_chunk = list(dev = 'png',
48 dpi = 96,
49 fig.width = fig_width,
50 fig.height = fig_height)
51 )
52
53 # base pandoc options for all odt output
54 args <- c()
55
56 # template
57 if (!is.null(template) && !identical(template, "default"))
58 args <- c(args, "--template", pandoc_path_arg(template))
59
60 # content includes
61 args <- c(args, includes_to_pandoc_args(includes))
62
63 # reference odt
64 args <- c(args, reference_doc_args("odt", reference_odt))
65
66 # pandoc args
67 args <- c(args, pandoc_args)
68
69 saved_files_dir <- NULL
70 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
71 saved_files_dir <<- files_dir
72 NULL
73 }
74
75 intermediates_generator <- function(...) {
76 reference_intermediates_generator(saved_files_dir, ..., reference_odt)
77 }
78
79 # return output format
80 output_format(
81 knitr = knitr,
82 pandoc = pandoc_options(
83 to = "odt",
84 from = from_rmarkdown(fig_caption, md_extensions),
85 args = args,
86 lua_filters = pkg_file_lua(
87 c("pagebreak.lua", if (number_sections) "number-sections.lua"))
88 ),
89 keep_md = keep_md,
90 pre_processor = pre_processor,
91 intermediates_generator = intermediates_generator
92 )
93 }