Debian Codegraph @

R/format.R

raw
1 is_rmd_preview = function() Sys.getenv('RMARKDOWN_PREVIEW_DIR') != ''
2
3 output_format = function(to, options, meta, ...) {
4 if (is_rmd_preview()) stop(
5 "It appears that you clicked the 'Knit' button in RStudio to render the document. ",
6 "You are recommended to use litedown::roam() to preview or render documents instead. ",
7 "Alternatively, you can add a top-level field 'knit: litedown:::knit' to the YAML metadata, ",
8 "so the document can be rendered by litedown::fuse() instead of rmarkdown::render().",
9 call. = FALSE
10 )
11 msg = 'Please render the document via litedown::fuse() instead of rmarkdown::render().'
12 if ('pkgdown' %in% loadedNamespaces()) {
13 warning(
14 msg, '\n\nIf you intend to build a package website, you can also use litedown:',
15 ' https://yihui.org/litedown/#sec:pkg-site', call. = FALSE
16 )
17 ns = asNamespace('rmarkdown')
18 ag = merge_list(list(to = to), list(...))
19 ag = ag[intersect(names(formals(ns$pandoc_options)), names(ag))]
20 opts = do.call(ns$pandoc_options, ag)
21 return(ns$output_format(NULL, opts))
22 }
23 stop(msg, call. = FALSE)
24 }
25
26 #' Output formats in YAML metadata
27 #'
28 #' These functions exist only for historical reasons, and should never be called
29 #' directly. They can be used to configure output formats in YAML, but you are
30 #' recommended to use the file format names instead of these function names.
31 #'
32 #' To configure output formats in the YAML metadata of the Markdown document,
33 #' simply use the output format names such as `html` or `latex` in the `output`
34 #' field in YAML, e.g.,
35 #'
36 #' ```yaml
37 #' ---
38 #' output:
39 #' html:
40 #' options:
41 #' toc: true
42 #' keep_md: true
43 #' latex:
44 #' latex_engine: pdflatex
45 #' ---
46 #' ```
47 #'
48 #' You can also use `litedown::html_format` instead of `html` (or
49 #' `litedown::latex_format` instead of `latex`) if you like.
50 #' @param meta,options Arguments to be passed to [mark()].
51 #' @param template A template file path.
52 #' @param keep_md,keep_tex Whether to keep the intermediate \file{.md} and
53 #' \file{.tex} files generated from \file{.Rmd}.
54 #' @param latex_engine The LaTeX engine to compile \file{.tex} to \file{.pdf}.
55 #' @param citation_package The LaTeX package for processing citations. Possible
56 #' values are `none`, `natbib`, and `biblatex`.
57 #' @note If you want to use the `Knit` button in RStudio, you must add a
58 #' top-level field `knit: litedown:::knit` to the YAML metadata. See
59 #' \url{https://yihui.org/litedown/#sec:knit-button} for more information.
60 #' @export
61 html_format = function(options = NULL, meta = NULL, template = NULL, keep_md = FALSE) {
62 output_format('html', options, meta, template, keep_md)
63 }
64
65 #' @rdname html_format
66 #' @export
67 latex_format = function(
68 options = NULL, meta = NULL, template = NULL, keep_md = FALSE,
69 keep_tex = FALSE, latex_engine = 'xelatex', citation_package = 'natbib'
70 ) {
71 output_format()
72 }
73
74 # map rmarkdown arguments to markdown
75 map_args = function(
76 toc = FALSE, toc_depth = 3, number_sections = FALSE, anchor_sections = FALSE,
77 code_folding = 'none', self_contained = TRUE, math_method = 'default',
78 css = NULL, includes = NULL, ...
79 ) {
80 opts = list(
81 toc = toc, number_sections = number_sections, embed_resources = self_contained
82 )
83 meta = list(css = c('default', css))
84 if (toc) opts$toc = list(depth = toc_depth)
85 if (identical(
86 if (is.list(math_method)) math_method$engine else math_method, 'mathjax'
87 )) opts$js_math = 'mathjax'
88 if (!isFALSE(anchor_sections)) {
89 meta$js = c(meta$js, '@heading-anchor')
90 meta$css = c(meta$css, '@heading-anchor')
91 }
92 # 'hide' is not supported here; if it is desired, use <script data-open=false>
93 if (code_folding != 'none') meta$js = c(
94 meta$js, '@fold-details'
95 )
96 if (is.list(includes)) meta[
97 c('header_includes', 'include_before', 'include_after')
98 ] = includes[c('in_header', 'before_body', 'after_body')]
99 list(meta = meta, options = opts, ...)
100 }
101
102 # split YAML and body from text input
103 yaml_body = function(text, ...) {
104 res = xfun::yaml_body(text, use_yaml = FALSE, ...)
105 if (!is.null(yaml <- normalize_yaml(res$yaml))) res$yaml = yaml
106 res
107 }
108
109 # normalize (rmarkdown) output formats in YAML to litedown's formats
110 normalize_yaml = function(x) {
111 if (!length(out <- x[['output']])) {
112 # if the key 'format' is provided, normalize it to 'output'
113 if (length(out <- x[['format']])) x$format = NULL else return()
114 }
115 if (is.character(out)) out = set_names(vector('list', length(out)), out)
116 if (!is.list(out))
117 stop('The output format field in YAML must be either list or character')
118 fmt = c(html_document = 'html', html_vignette = 'html', pdf_document = 'latex')
119 for (i in intersect(names(fmt), names(out))) {
120 out[[i]] = if (is.list(out[[i]])) do.call(map_args, out[[i]]) else list()
121 names(out)[names(out) == i] = fmt[i]
122 }
123 # normalize format names `(lite|mark)down::*_format` to `*`
124 names(out) = gsub('^(lite|mark)down::+([^_]+)_.*', '\\2', names(out))
125 x$output = out
126 x
127 }
128
129 # get metadata from a certain field under an output format
130 yaml_field = function(yaml, format, name = 'meta') {
131 if (is.list(out <- yaml[['output']]) && is.list(out <- out[[format]])) {
132 if (length(name) == 1) out[[name]] else out[name]
133 }
134 }
135
136 # get output format from YAML's `output` field
137 yaml_format = function(yaml) {
138 if (is.list(out <- yaml[['output']])) out = names(out)
139 c(out, 'html')[1]
140 }
141
142 # determine output format based on output file name and input's YAML
143 detect_format = function(output, yaml) {
144 res = if (is.character(output)) {
145 # check if output is a known format, e.g., output = 'html'
146 if (output %in% names(md_formats)) output else {
147 # check file extension, e.g., output = '.pdf'
148 ext = file_ext(output)
149 if (ext == 'pdf') 'latex' else {
150 names(which(md_formats == paste0('.', ext))) %|%
151 # output = 'markdown:format', e.g., markdown:latex means the final
152 # format is latex but the intermediate output should be markdown
153 if (startsWith(output, 'markdown:')) intersect(
154 sub('^markdown:', '', output), names(md_formats)
155 )
156 }
157 }
158 }
159 # if unable to detect format from `output`, try YAML
160 if (length(res) == 1) res else yaml_format(yaml)
161 }
162
163 md_formats = c(
164 html = '.html', xml = '.xml', man = '.man', commonmark = '.markdown',
165 markdown = '.md', text = '.txt', latex = '.tex'
166 )