Debian Codegraph @

R/utils-vignettes.R

raw
1 #' Package vignette engines
2 #'
3 #' Since R 3.0.0, package vignettes can use non-Sweave engines, and \pkg{knitr}
4 #' has provided a few engines to compile vignettes via \code{\link{knit}()} with
5 #' different templates. See \url{https://yihui.org/knitr/demo/vignette/} for
6 #' more information.
7 #' @name vignette_engines
8 #' @note If you use the \code{knitr::rmarkdown} engine, please make sure that
9 #' you put \pkg{rmarkdown} in the \samp{Suggests} field of your
10 #' \file{DESCRIPTION} file. Also make sure \command{pandoc} is available
11 #' during \command{R CMD build}. If you build your package from RStudio, this
12 #' is normally not a problem. If you build the package outside RStudio, run
13 #' \code{rmarkdown::find_pandoc()} in an R session to check if Pandoc can be
14 #' found.
15 #'
16 #' When the \pkg{rmarkdown} package is not installed or not available, or
17 #' \command{pandoc} cannot be found, the \code{knitr::rmarkdown} engine will
18 #' fall back to the \code{knitr::knitr} engine, which uses R Markdown v1 based
19 #' on the \pkg{markdown} package.
20 #' @examples library(knitr)
21 #' vig_list = tools::vignetteEngine(package = 'knitr')
22 #' str(vig_list)
23 #' vig_list[['knitr::knitr']][c('weave', 'tangle')]
24 #' vig_list[['knitr::knitr_notangle']][c('weave', 'tangle')]
25 #' vig_list[['knitr::docco_classic']][c('weave', 'tangle')]
26 NULL
27
28 vweave = function(file, driver, syntax, encoding = 'UTF-8', quiet = FALSE, ...) {
29 {
30 on.exit({opts_chunk$restore(); knit_hooks$restore()}, add = TRUE)
31 oopts = options(knitr.knit2html.force_v1 = TRUE)
32 on.exit(options(oopts), add = TRUE)
33 }
34 opts_chunk$set(error = FALSE) # should not hide errors
35 knit_hooks$set(purl = function(...) {
36 # run some hooks for vignettes
37 hook_purl(...) # write out code while weaving
38 # optimize PNG images if tools exist and hooks not set
39 if (!is_R_CMD_check()) for (i in c('optipng', 'pngquant'))
40 if (!is.function(knit_hooks$get(i)) && Sys.which(i) != '') {
41 switch(i, optipng = hook_optipng(...), pngquant = hook_pngquant(...))
42 }
43 })
44 (if (grepl('\\.[Rr]md$', file)) knit2html else if (grepl('\\.[Rr]rst$', file)) knit2pandoc else knit)(
45 file, encoding = encoding, quiet = quiet, envir = globalenv(), ...
46 )
47 }
48
49 vtangle = function(file, ..., encoding = 'UTF-8', quiet = FALSE) {
50 purl(file, encoding = encoding, quiet = quiet, ...)
51 }
52
53 vweave_docco_linear = vweave
54 body(vweave_docco_linear)[5L] = expression(docco_linear(
55 file, encoding = encoding, quiet = quiet, envir = globalenv(), ...
56 ))
57
58 vweave_docco_classic = vweave
59 body(vweave_docco_classic)[5L] = expression(rocco(
60 file, encoding = encoding, quiet = quiet, envir = globalenv(), ...
61 ))
62
63 vweave_rmarkdown = vweave
64 body(vweave_rmarkdown)[5L] = expression(rmarkdown::render(
65 file, encoding = encoding, quiet = quiet, envir = globalenv(),
66 output_dir = getwd(), ...
67 ))
68
69 # do not tangle R code from vignettes
70 untangle_weave = function(vig_list, eng) {
71 weave = vig_list[[c(eng, 'weave')]]
72 # remove the purl hook from the weave function, but the rmarkdown engine
73 # function is different (not vweave_rmarkdown above, but the function(...)
74 # defined below in vig_engine('rmarkdown'), and it is not straightforward to
75 # remove the purl hook there)
76 if (eng != 'knitr::rmarkdown') body(weave)[4L] = expression({})
77 weave
78 }
79 vtangle_empty = function(file, ...) {
80 unlink(with_ext(file, 'R'))
81 return()
82 }
83
84 # when neither Pandoc nor markdown is available, just silently skip the vignette
85 vweave_empty = function(file, ..., .reason = 'Pandoc') {
86 out = with_ext(file, 'html')
87 writeLines(sprintf('The vignette could not be built because %s is not available.', .reason), out)
88 out
89 }
90
91 register_vignette_engines = function(pkg) {
92 # the default engine
93 vig_engine('knitr', vweave, '[.]([rRsS](nw|tex)|[Rr](md|html|rst))$')
94 vig_engine('docco_linear', vweave_docco_linear, '[.][Rr](md|markdown)$')
95 vig_engine('docco_classic', vweave_docco_classic, '[.][Rr]mk?d$')
96 vig_engine('rmarkdown', function(...) {
97 if (is_cran_check() && !has_package('rmarkdown'))
98 return(vweave_empty(..., .reason = 'rmarkdown'))
99 if (pandoc_available()) {
100 vweave_rmarkdown(...)
101 } else {
102 (if (is_R_CMD_check()) message else stop2)(
103 'Pandoc is required to build R Markdown vignettes but not available. ',
104 'Please make sure it is installed.'
105 )
106 if (has_package('markdown')) vweave(...) else vweave_empty(...)
107 }
108 }, '[.][Rr](md|markdown)$')
109 # vignette engines that disable tangle
110 vig_list = tools::vignetteEngine(package = 'knitr')
111 engines = grep('_notangle$', names(vig_list), value = TRUE, invert = TRUE)
112 for (eng in engines) vig_engine(
113 paste(sub('^knitr::', '', eng), 'notangle', sep = '_'),
114 untangle_weave(vig_list, eng),
115 tangle = vtangle_empty,
116 pattern = vig_list[[c(eng, 'pattern')]]
117 )
118 }
119 # all engines use the same tangle and package arguments, so factor them out
120 vig_engine = function(..., tangle = vtangle) {
121 tools::vignetteEngine(..., tangle = tangle, package = 'knitr', aspell = list(
122 filter = knit_filter
123 ))
124 }
125
126 #' Spell check filter for source documents
127 #'
128 #' When performing spell checking on source documents, we may need to skip R
129 #' code chunks and inline R expressions, because many R functions and symbols
130 #' are likely to be identified as typos. This function is designed for the
131 #' \code{filter} argument of \code{\link{aspell}()} to filter out code chunks
132 #' and inline expressions.
133 #' @param ifile Filename of the source document.
134 #' @param encoding Ignored (the file \code{ifile} must be encoded in UTF-8).
135 #' @return A character vector of the file content, excluding code chunks and
136 #' inline expressions.
137 #' @export
138 #' @examples library(knitr)
139 #' knitr_example = function(...) system.file('examples', ..., package = 'knitr')
140 #' \donttest{
141 #' if (Sys.which('aspell') != '') {
142 #' # -t means the TeX mode
143 #' utils::aspell(knitr_example('knitr-minimal.Rnw'), knit_filter, control = '-t')
144 #'
145 #' # -H is the HTML mode
146 #' utils::aspell(knitr_example('knitr-minimal.Rmd'), knit_filter, control = '-H -t')
147 #' }}
148 knit_filter = function(ifile, encoding = 'UTF-8') {
149 x = read_utf8(ifile)
150 n = length(x); if (n == 0) return(x)
151 p = detect_pattern(x, tolower(file_ext(ifile)))
152 if (is.null(p)) return(x)
153 p = all_patterns[[p]]; p1 = p$chunk.begin; p2 = p$chunk.end
154 m = group_indices(grepl(p1, x), grepl(p2, x))
155 i = m %% 2 == 0
156 x[i] = '' # remove code chunks
157 x[!i] = gsub(p$inline.code, '', x[!i], perl = TRUE) # remove inline code
158 structure(x, control = '-H -t')
159 }
160
161 pandoc_available = function() {
162 rmarkdown::pandoc_available('1.12.3')
163 }
164
165 html_vignette = function(
166 ..., fig_caption = TRUE, theme = NULL, highlight = NULL,
167 includes = list(
168 in_header = system.file('misc', 'vignette.html', package = 'knitr')
169 )
170 ) {
171 rmarkdown::html_document(
172 ..., fig_caption = fig_caption, theme = theme, highlight = highlight,
173 includes = includes
174 )
175 }