Debian Codegraph @

R/html_vignette.R

raw
1 #' Convert to an HTML vignette
2 #'
3 #' A HTML vignette is a lightweight alternative to [html_document()]
4 #' suitable for inclusion in packages to be released to CRAN. It reduces the
5 #' size of a basic vignette from 100k to around 10k.
6 #'
7 #' Compared to [html_document()], it:
8 #'
9 #' * never uses retina figures
10 #' * never uses a theme
11 #' * has a smaller default figure size
12 #' * uses a custom css stylesheet
13 #'
14 #' See the [online
15 #' documentation](https://yihui.org/rmarkdown/r-package-vignette.html)
16 #' for additional details on using the `html_vignette()` format.
17 #' @inheritParams html_document
18 #' @param highlight,... Additional arguments passed to
19 #' [html_document()]. Please note that `theme` and
20 #' `fig_retina` are hard-coded. Setting any of those will yield an error.
21 #' @param css One or more css files to include.
22 #' @param readme Use this vignette as the package README.md file (i.e. render
23 #' it as README.md to the package root). Note that if there are image files
24 #' within your vignette you should be sure to add \file{README_files} to \file{.Rbuildignore}.
25 #' @param tabset Opt-in tabbed-sections feature inspired by [html_document()].
26 #' See section "Tabbed Sections" for the detail.
27 #' This feature also allows navigation to the tab from table of contents and URL.
28 #' @inheritSection html_document Tabbed Sections
29 #' @inheritSection html_document Code folding
30 #' @return R Markdown output format to pass to [render()]
31 #' @md
32 #' @export
33 html_vignette <- function(fig_width = 3,
34 fig_height = 3,
35 dev = 'png',
36 df_print = "default",
37 css = NULL,
38 highlight = "pygments",
39 keep_md = FALSE,
40 readme = FALSE,
41 self_contained = TRUE,
42 tabset = FALSE,
43 code_folding = c("none", "show", "hide"),
44 extra_dependencies = NULL,
45 pandoc_args = NULL,
46 ...) {
47
48 lua_filters <- c()
49
50 if (is.null(css)) {
51 css <- system.file("rmarkdown", "templates", "html_vignette" ,"resources",
52 "vignette.css", package = "rmarkdown")
53 }
54
55 if (tabset) {
56 extra_dependencies <- append(extra_dependencies, list(html_dependency_tabset()))
57 }
58
59 code_folding <- match.arg(code_folding)
60 if (code_folding != "none") {
61 extra_dependencies <- append(extra_dependencies,
62 list(html_dependency_codefolding_lua()))
63 pandoc_args <- c(pandoc_args,
64 pandoc_metadata_arg("rmd_codefolding_lua", code_folding))
65 lua_filters <- c(lua_filters, pkg_file_lua("codefolding.lua"))
66 }
67
68 pre_knit <- function(input, ...) {
69 if (readme) {
70 rmarkdown::render(input,
71 output_format = "github_document",
72 output_options = list(html_preview = FALSE),
73 output_file = "README.md",
74 output_dir = dirname(dirname(input)),
75 quiet = TRUE)
76 }
77 }
78
79 pre_processor <- function(metadata, input_file, runtime, knit_meta,
80 files_dir, output_dir) {
81 vignette_pre_processor(input_file, metadata)
82 }
83
84 base_format <- html_document(fig_width = fig_width,
85 fig_height = fig_height,
86 dev = dev,
87 fig_retina = NULL,
88 css = css,
89 theme = NULL,
90 highlight = highlight,
91 self_contained = self_contained,
92 extra_dependencies = extra_dependencies,
93 pandoc_args = pandoc_args,
94 ...)
95 base_format$pandoc$lua_filters <- append(base_format$pandoc$lua_filters,
96 lua_filters)
97 output_format(
98 knitr = NULL,
99 pandoc = NULL,
100 df_print = df_print,
101 pre_knit = pre_knit,
102 keep_md = keep_md,
103 clean_supporting = self_contained,
104 pre_processor = pre_processor,
105 base_format = base_format
106 )
107 }
108
109 vignette_pre_processor <- function(input_file, metadata = yaml_front_matter(input_file)) {
110 if (getRversion() < '3.6')
111 return()
112 if (!getOption(o <- 'rmarkdown.html_vignette.check_title', !xfun::is_R_CMD_check()))
113 return()
114 title1 <- metadata[['title']]
115 title2 <- tools::vignetteInfo(input_file)[['title']]
116 # rmarkdown assumes UTF-8 only - tools::vignetteInfo uses readLines with
117 # default OS encoding so issue on Windows for example
118 # https://github.com/rstudio/rmarkdown/issues/1978
119 Encoding(title2) <- "UTF-8"
120 if (!identical(title1, title2)) warning(
121 'The vignette title specified in \\VignetteIndexEntry{} is different from ',
122 'the title in the YAML metadata. The former is "', title2, '", and the ',
123 'latter is "', title1, '". If that is intentional, you may set options(', o,
124 ' = FALSE) to suppress this check.', call. = FALSE
125 )
126 NULL
127 }