Debian Codegraph @

R/md_document.R

raw
1 #' Convert to a markdown document
2 #'
3 #' Format for converting from R Markdown to another variant of markdown (e.g.
4 #' strict markdown or github flavored markdown)
5 #'
6 #' See the [online
7 #' documentation](https://yihui.org/rmarkdown/markdown-document.html)
8 #' for additional details on using the `md_document()` format.
9 #'
10 #' R Markdown documents can have optional metadata that is used to generate a
11 #' document header that includes the title, author, and date. For more details
12 #' see the documentation on R Markdown [metadata][rmd_metadata].
13 #' @inheritParams html_document
14 #' @param variant Markdown variant to produce (defaults to "markdown_strict").
15 #' Other valid values are "commonmark", "gfm", "commonmark_x", "markdown_mmd",
16 #' markdown_phpextra", "markdown_github", or even "markdown" (which produces
17 #' pandoc markdown). You can also compose custom markdown variants, see the
18 #' \href{https://pandoc.org/MANUAL.html}{pandoc online documentation} for
19 #' details.
20 #' @param preserve_yaml Preserve YAML front matter in final document.
21 #' @param standalone Set to `TRUE` to include title, date and other metadata
22 #' field in addition to Rmd content as a body.
23 #' @param fig_retina Scaling to perform for retina displays. Defaults to
24 #' `NULL` which performs no scaling. A setting of 2 will work for all
25 #' widely used retina displays, but will also result in the output of
26 #' `<img>` tags rather than markdown images due to the need to set the
27 #' width of the image explicitly.
28 #' @param ext Extension of the output file (defaults to ".md").
29 #' @return R Markdown output format to pass to [render()]
30 #' @examples
31 #' \dontrun{
32 #' library(rmarkdown)
33 #'
34 #' render("input.Rmd", md_document())
35 #'
36 #' render("input.Rmd", md_document(variant = "markdown_github"))
37 #' }
38 #' @export
39 #' @md
40 md_document <- function(variant = "markdown_strict",
41 preserve_yaml = FALSE,
42 toc = FALSE,
43 toc_depth = 3,
44 number_sections = FALSE,
45 standalone = FALSE,
46 fig_width = 7,
47 fig_height = 5,
48 fig_retina = NULL,
49 dev = 'png',
50 df_print = "default",
51 includes = NULL,
52 md_extensions = NULL,
53 pandoc_args = NULL,
54 ext = ".md") {
55
56
57 # base pandoc options for all markdown output
58
59 if (toc) standalone <- TRUE
60
61 args <- c(if (standalone) "--standalone")
62
63 # table of contents
64 args <- c(args, pandoc_toc_args(toc, toc_depth))
65
66 # content includes
67 args <- c(args, includes_to_pandoc_args(includes))
68
69 # pandoc args
70 args <- c(args, pandoc_args)
71
72 # Preprocess number_sections if variant is a markdown flavor +gfm_auto_identifiers
73 if (number_sections && !pandoc_available("2.1")) {
74 warning("`number_sections = TRUE` requires at least Pandoc 2.1. The feature will be deactivated",
75 call. = FALSE)
76 number_sections <- FALSE
77 }
78 pre_processor <- if (
79 number_sections
80 && grepl("^(commonmark|gfm|markdown)", variant)
81 && any(grepl("+gfm_auto_identifiers", md_extensions, fixed = TRUE))
82 ) {
83 function(metadata, input_file, ...) {
84 input_lines <- read_utf8(input_file)
85 pandoc_convert(
86 input_file, to = "markdown", output = input_file,
87 options = c(
88 "--lua-filter", pkg_file_lua("number-sections.lua"),
89 "--metadata", "preprocess_number_sections=true"
90 )
91 )
92 input_lines2 <- read_utf8(input_file)
93 write_utf8(.preserve_yaml(input_lines, input_lines2), input_file)
94 return(character(0L))
95 }
96 }
97
98 # variants
99 variant <- adapt_md_variant(variant)
100
101 # add post_processor for yaml preservation as Pandoc +yaml_metadata_block has
102 # undesired sorting (https://github.com/rstudio/rmarkdown/pull/2190/files)
103 post_processor <- if (preserve_yaml) {
104 function(metadata, input_file, output_file, clean, verbose) {
105 input_lines <- read_utf8(input_file)
106 output_lines <- read_utf8(output_file)
107 write_utf8(
108 .preserve_yaml(input_lines, output_lines),
109 output_file
110 )
111 output_file
112 }
113 }
114
115 # return format
116 output_format(
117 knitr = knitr_options_html(fig_width, fig_height, fig_retina, FALSE, dev),
118 pandoc = pandoc_options(
119 to = variant,
120 from = from_rmarkdown(extensions = md_extensions),
121 args = args,
122 lua_filters = if (number_sections) pkg_file_lua("number-sections.lua"),
123 ext = ext
124 ),
125 clean_supporting = FALSE,
126 df_print = df_print,
127 pre_processor = pre_processor,
128 post_processor = post_processor
129 )
130 }
131
132 .preserve_yaml <- function(input_lines, output_lines) {
133 partitioned <- partition_yaml_front_matter(input_lines)
134 if (!is.null(partitioned$front_matter)) {
135 output_lines <- c(partitioned$front_matter, "", output_lines)
136 }
137 output_lines
138 }
139
140 adapt_md_variant <- function(variant) {
141 variant_base <- gsub("^([^+-]*).*", "\\1", variant)
142 variant_extensions <- gsub(sprintf("^%s", variant_base), "", variant)
143
144 set_extension <- function(format, ext, add = TRUE) {
145 ext <- paste0(ifelse(add, "+", "-"), ext)
146 for (e in ext) {
147 if (grepl(e, format, fixed = TRUE)) next
148 format <- paste0(format, e, collapse = "")
149 }
150 format
151 }
152
153 # Remove yaml_metadata_block extension unless user asked otherwise in variant
154 if (!grepl("yaml_metadata_block", variant_extensions, fixed = TRUE)) {
155 variant_extensions <- switch(
156 variant_base,
157 gfm = ,
158 commonmark = ,
159 commonmark_x = {
160 if (pandoc_available("2.13")) {
161 set_extension(variant_extensions, "yaml_metadata_block", FALSE)
162 } else {
163 # Unsupported extension before YAML 2.13
164 variant_extensions
165 }
166 },
167 markdown = set_extension(variant_extensions, c("yaml_metadata_block", "pandoc_title_block"), FALSE),
168 markdown_mmd = set_extension(variant_extensions, c("yaml_metadata_block", "mmd_title_block"), FALSE),
169 markdown_github = ,
170 markdown_phpextra = ,
171 markdown_strict = set_extension(variant_extensions, "yaml_metadata_block", FALSE),
172 # do not modified for unknown (yet) md variant
173 variant_extensions
174 )
175 }
176
177 paste0(variant_base, variant_extensions, collapse = "")
178 }